当前位置:网站首页>Byte interview algorithm question
Byte interview algorithm question
2022-07-04 13:51:00 【Zijin xiaofeixia】
Triangle shortest path and ( Dynamic programming )
Triangle shortest path and ( Dynamic programming )
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
// Processing of a number
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];
/* When it is the first one, it should be added directly, and the minimum value can be judged in the rest cases, because there are two paths to reach this position, and these two paths need to be compared */
if (j == 0){
//j == 0 The situation of
rec[i + 1][j] = first + cur;
rec[i + 1][j + 1] = second + cur;
}else {
//j > 1 The previous values need to be compared
rec[i + 1][j] = Math.min(rec[i + 1][j], cur + first);
// For the second position, add it directly, because next time, this position will turn into the first position, and the minimum value will be compared. It is easy to understand to draw a figure on the paper
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;
}
}
Binary tree traversal
Chinese characters and numbers transformation 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("^.* Billion .* ten thousand .*$", money);
for (int i = 0; i < money.length(); i++) {
switch (money.charAt(i)) {
case ' zero ':
break;
case ' One ':
c = 1;
break;
case ' Two ':
c = 2;
break;
case ' 3、 ... and ':
c = 3;
break;
case ' Four ':
c = 4;
break;
case ' 5、 ... and ':
c = 5;
break;
case ' 6、 ... and ':
c = 6;
break;
case ' 7、 ... and ':
c = 7;
break;
case ' 8、 ... and ':
c = 8;
break;
case ' Nine ':
c = 9;
break;
case ' Ten ':
result += (c == 0 ? 10 : c * 10);
c = 0;
break;
case ' hundred ':
result += c * 100;
c = 0;
break;
case ' thousand ':
result += c * 1000;
c = 0;
break;
case ' ten thousand ':
result = (result + c) * 10000;
c = 0;
break;
case ' Billion ':
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(" 13.4 billion, 10.2 million, 26.69 million "));
}
}
边栏推荐
- 上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- 三星量产3纳米产品引台媒关注:能否短期提高投入产出率是与台积电竞争关键
- Read the BGP agreement in 6 minutes.
- Dgraph: large scale dynamic graph dataset
- The old-fashioned synchronized lock optimization will make it clear to you at once!
- Runc hang causes the kubernetes node notready
- 一次 Keepalived 高可用的事故,让我重学了一遍它
- 嵌入式编程中五个必探的“潜在错误”
猜你喜欢

一个数据人对领域模型理解与深入

Comparative study of the gods in the twilight Era

A data person understands and deepens the domain model

Reptile exercises (I)

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

OPPO Find N2产品形态首曝:补齐各项短板

Meituan Ali's Application Practice on multimodal recall

从0到1建设智能灰度数据体系:以vivo游戏中心为例

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍

上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
随机推荐
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
ASP. Net core introduction I
Go 语言入门很简单:Go 实现凯撒密码
Optional values and functions of the itemized contenttype parameter in the request header
Practice: fabric user certificate revocation operation process
. Net using redis
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
mysql三级分销代理关系存储
Annual comprehensive analysis of China's mobile reading market in 2022
诸神黄昏时代的对比学习
XML入门三
Rsyslog配置及使用教程
嵌入式编程中五个必探的“潜在错误”
担心“断气” 德国正修改《能源安全法》
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Efficient! Build FTP working environment with virtual users
XML入门一
实时云交互如何助力教育行业发展
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
C语言图书租赁管理系统