当前位置:网站首页>字节面试算法题
字节面试算法题
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("一百三十四亿一千零二十三万六千六百零九"));
}
}
边栏推荐
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- C foundation in-depth study I
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- JVM系列——栈与堆、方法区day1-2
- 阿里云有奖体验:用PolarDB-X搭建一个高可用系统
- Introduction to XML II
- C语言小型商品管理系统
- 读《认知觉醒》
- 2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
- n++也不靠谱
猜你喜欢

Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing

Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article

Runc hang causes the kubernetes node notready

Practice: fabric user certificate revocation operation process

Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Three schemes to improve the efficiency of MySQL deep paging query

Reinforcement learning - learning notes 1 | basic concepts

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

Meituan Ali's Application Practice on multimodal recall

Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
随机推荐
CTF competition problem solution STM32 reverse introduction
C語言宿舍管理查詢軟件
Is the outdoor LED screen waterproof?
Excuse me, have you encountered this situation? CDC 1.4 cannot use timestamp when connecting to MySQL 5.7
C array supplement
Optional values and functions of the itemized contenttype parameter in the request header
Efficient! Build FTP working environment with virtual users
CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021
C语言小型商品管理系统
In 2022, it will be es2022 soon. Do you only know the new features of ES6?
WPF double slider control and forced capture of mouse event focus
爬虫练习题(一)
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
Using nsproxy to forward messages
go-zero微服务实战系列(九、极致优化秒杀性能)
7、 Software package management
6 分钟看完 BGP 协议。
读《认知觉醒》
Database lock table? Don't panic, this article teaches you how to solve it
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话