当前位置:网站首页>字节面试算法题
字节面试算法题
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("一百三十四亿一千零二十三万六千六百零九"));
}
}
边栏推荐
- After installing vscode, the program runs (an include error is detected, please update the includepath, which has been solved for this translation unit (waveform curve is disabled) and (the source fil
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- Node の MongoDB 安装
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- The only core indicator of high-quality software architecture
- C language Dormitory Management Query Software
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- 爬虫练习题(一)
- 模块化笔记软件综合评测:Craft、Notion、FlowUs
- Database lock table? Don't panic, this article teaches you how to solve it
猜你喜欢
上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
The only core indicator of high-quality software architecture
高效!用虚拟用户搭建FTP工作环境
PostgreSQL 9.1 soaring Road
When MDK uses precompiler in header file, ifdef is invalid
Oracle 被 Ventana Research 评为数字创新奖总冠军
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Comparative study of the gods in the twilight Era
随机推荐
一个数据人对领域模型理解与深入
MySQL three-level distribution agent relationship storage
Use fail2ban to prevent password attempts
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer
Runc hang causes the kubernetes node notready
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
读《认知觉醒》
Deploy halo blog with pagoda
Node の MongoDB 安装
Scripy framework learning
WPF double slider control and forced capture of mouse event focus
C#基础深入学习一
Apache服务器访问日志access.log设置
Three schemes to improve the efficiency of MySQL deep paging query
Efficient! Build FTP working environment with virtual users
JVM系列——栈与堆、方法区day1-2
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Read the BGP agreement in 6 minutes.