当前位置:网站首页>字节面试算法题
字节面试算法题
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("一百三十四亿一千零二十三万六千六百零九"));
}
}
边栏推荐
- Besides, rsync+inotify realizes real-time backup of data
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- 再说rsync+inotify实现数据的实时备份
- CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
- The only core indicator of high-quality software architecture
- Configure WebDAV server on Apache
- Three schemes to improve the efficiency of MySQL deep paging query
- Database lock table? Don't panic, this article teaches you how to solve it
- 上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
- DGraph: 大规模动态图数据集
猜你喜欢
2022年中国移动阅读市场年度综合分析
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
一个数据人对领域模型理解与深入
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
n++也不靠谱
Valentine's Day confession code
Reptile exercises (I)
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer
Database lock table? Don't panic, this article teaches you how to solve it
随机推荐
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
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
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
Using scrcpy projection
DGraph: 大规模动态图数据集
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
模块化笔记软件综合评测:Craft、Notion、FlowUs
JVM系列——栈与堆、方法区day1-2
HAProxy高可用解决方案
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
Etcd storage, watch and expiration mechanism
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
老掉牙的 synchronized 锁优化,一次给你讲清楚!
Introduction to XML II
XML入门三
PostgreSQL 9.1 飞升之路
聊聊支付流程的设计与实现逻辑