当前位置:网站首页>字节面试算法题
字节面试算法题
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("一百三十四亿一千零二十三万六千六百零九"));
}
}
边栏推荐
- Oracle 被 Ventana Research 评为数字创新奖总冠军
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
- Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- Alibaba cloud award winning experience: build a highly available system with polardb-x
- Scripy framework learning
- 2022年中国移动阅读市场年度综合分析
- 再说rsync+inotify实现数据的实时备份
- Database lock table? Don't panic, this article teaches you how to solve it
猜你喜欢
PostgreSQL 9.1 soaring Road
AI painting minimalist tutorial
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++也不靠谱
Annual comprehensive analysis of China's mobile reading market in 2022
ASP.NET Core入门一
Oracle was named the champion of Digital Innovation Award by Ventana research
Meituan Ali's Application Practice on multimodal recall
SQL statement syntax error in test SQL statement deletion in eclipse linked database
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
随机推荐
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Three schemes to improve the efficiency of MySQL deep paging query
Introduction to XML III
N++ is not reliable
爬虫练习题(一)
Apache服务器访问日志access.log设置
比量子化学方法快六个数量级,一种基于绝热状态的绝热人工神经网络方法,可加速对偶氮苯衍生物及此类分子的模拟
C language Dormitory Management Query Software
C array supplement
游戏启动后提示安装HMS Core,点击取消,未再次提示安装HMS Core(初始化失败返回907135003)
C#基础补充
8个扩展子包!RecBole推出2.0!
Scripy framework learning
使用宝塔部署halo博客
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
Valentine's Day confession code
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
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
[cloud native | kubernetes] in depth understanding of ingress (12)
Meituan Ali's Application Practice on multimodal recall