当前位置:网站首页>字节面试算法题
字节面试算法题
2022-07-04 12:44:00 【紫金小飞侠】
三角形最短路径和(动态规划)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
//对于一个数字的处理
if (triangle.size() == 1) return triangle.get(0).get(0);
int min = Integer.MAX_VALUE;
int rec[][] = new int[triangle.size()][];
for (int i = 0; i < triangle.size(); ++i){
rec[i] = new int[i + 1];
}
rec[0][0] = triangle.get(0).get(0);
for (int i = 0; i < triangle.size() - 1; ++i){
List<Integer> curLine = triangle.get(i);
List<Integer> nextLine = triangle.get(i + 1);
for (int j = 0; j < triangle.get(i).size(); ++j){
int first = nextLine.get(j);
int second = nextLine.get(j + 1);
int cur = rec[i][j];
/*当为第一个的时候应该是直接相加即可其余的情况进行最小值的判断因为到达这个位置存在着两条路径需要对这两条路径进行比较*/
if (j == 0){
//j == 0的情况
rec[i + 1][j] = first + cur;
rec[i + 1][j + 1] = second + cur;
}else {
//j > 1的情况需要对之前得到的值进行比较
rec[i + 1][j] = Math.min(rec[i + 1][j], cur + first);
//对于第二个位置直接相加因为在下一次的时候这个位置会转为第一个位置会进行最小值的比较在纸上画一下图是很好理解的
rec[i + 1][j + 1] = second + cur;
}
}
}
for (int i = 0; i < triangle.size(); ++i){
min = Math.min(min, rec[triangle.size() - 1][i]);
}
return min;
}
}
二叉树遍历
汉字数字 转换int

import java.util.regex.Pattern;
public class MoneyTest {
public static long parse(String money) {
long result = 0;
char c = 0;
boolean flag = Pattern.matches("^.*亿.*万.*$", money);
for (int i = 0; i < money.length(); i++) {
switch (money.charAt(i)) {
case '零':
break;
case '一':
c = 1;
break;
case '二':
c = 2;
break;
case '三':
c = 3;
break;
case '四':
c = 4;
break;
case '五':
c = 5;
break;
case '六':
c = 6;
break;
case '七':
c = 7;
break;
case '八':
c = 8;
break;
case '九':
c = 9;
break;
case '十':
result += (c == 0 ? 10 : c * 10);
c = 0;
break;
case '百':
result += c * 100;
c = 0;
break;
case '千':
result += c * 1000;
c = 0;
break;
case '万':
result = (result + c) * 10000;
c = 0;
break;
case '亿':
if (flag){
result = (result + c) * 10000;
}else{
result = (result + c) * 100000000;
}
c = 0;
break;
default:
c = 0;
}
}
if (c != 0)
result += c;
return result;
}
public static void main(String args[]) {
System.out.println(MoneyTest.parse("一百三十四亿一千零二十三万六千六百零九"));
}
}
边栏推荐
- When MDK uses precompiler in header file, ifdef is invalid
- Read the BGP agreement in 6 minutes.
- CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
- C语言职工管理系统
- Reading cognitive Awakening
- CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- 8个扩展子包!RecBole推出2.0!
- Runc hang causes the kubernetes node notready
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
猜你喜欢

PostgreSQL 9.1 soaring Road

Valentine's Day confession code

It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der

DGraph: 大规模动态图数据集

CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021

Is the outdoor LED screen waterproof?

Annual comprehensive analysis of China's mobile reading market in 2022

Practice: fabric user certificate revocation operation process

Master the use of auto analyze in data warehouse

When MDK uses precompiler in header file, ifdef is invalid
随机推荐
7、 Software package management
XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
C语言个人通讯录管理系统
. Net using redis
XML入门一
Apache server access log access Log settings
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Rsyslog配置及使用教程
Alibaba cloud award winning experience: build a highly available system with polardb-x
Comprehensive evaluation of modular note taking software: craft, notation, flowus
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
PostgreSQL 9.1 飞升之路
eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误
Backgroundworker usage example
Database lock table? Don't panic, this article teaches you how to solve it
MySQL three-level distribution agent relationship storage
游戏启动后提示安装HMS Core,点击取消,未再次提示安装HMS Core(初始化失败返回907135003)
C#/VB. Net to add text / image watermarks to PDF documents
Practice: fabric user certificate revocation operation process
PostgreSQL 9.1 soaring Road