当前位置:网站首页>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 "));
}
}
边栏推荐
- Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
- c#数组补充
- Database lock table? Don't panic, this article teaches you how to solve it
- Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
- 在 Apache 上配置 WebDAV 服务器
- Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
- Configure WebDAV server on Apache
- 基于链表管理的单片机轮询程序框架
- Introduction to XML III
- 安装trinity、解决报错
猜你喜欢

基于STM32+华为云IOT设计的酒驾监控系统

面试官:Redis中哈希数据类型的内部实现方式是什么?

CTF competition problem solution STM32 reverse introduction

一次 Keepalived 高可用的事故,让我重学了一遍它

【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源

ASP. Net core introduction I

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

Oracle was named the champion of Digital Innovation Award by Ventana research

C#/VB. Net to add text / image watermarks to PDF documents

Node の MongoDB安装
随机推荐
ViewBinding和DataBinding的理解和区别
Samsung's mass production of 3nm products has attracted the attention of Taiwan media: whether it can improve the input-output rate in the short term is the key to compete with TSMC
字节面试算法题
N++ is not reliable
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
Fs4056 800mA charging IC domestic fast charging power IC
Scrapy 框架学习
Apache server access log access Log settings
CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
使用宝塔部署halo博客
分布式BASE理论
Getting started with microservices
C#/VB. Net to add text / image watermarks to PDF documents
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
一次 Keepalived 高可用的事故,让我重学了一遍它
C language staff management system
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
The old-fashioned synchronized lock optimization will make it clear to you at once!
C语言集合运算
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements