滴滴面试二轮挂
评语:技术可以,算法不行。
面试官说给你出个简单的吧:两个大数相加
说实话我之前做过,我忘记了,马赛网不能调试,所以只能手撕,当然会部分代码出错,提示了三次才写下来
评语:
不要着急编码
理清思路以及合理性
代码
package com.yang.leetcode.exam;
import java.util.Arrays;
/**
* @author: fudy
* @date: 2021/04/17 下午 03:26
* @Decription:
**/
public class Main {
public static void main(String[] args) {
// 将数值转为string
long a = 3512345678L;
long b = 98776344324244L;
System.out.println(a+b);
String arg1 = String.valueOf(a);
String arg2 = String.valueOf(b);
// 设定结果值
StringBuffer sb = new StringBuffer(Math.max(arg1.length(),arg2.length()));
// 将低位的进位储存起来
int temp = 0;
// 如果其中一个数结束了,结束循环
int arg1Index = arg1.length()-1;
int arg2Index = arg2.length()-1;
while(arg1Index>=0 || arg2Index >=0){
// 遍历两个数进行低位相加
if (arg1Index < 0 || arg2Index < 0){
if (arg1Index > arg2Index){
sb.append(arg1.charAt(arg1Index));
--arg1Index;
}else {
sb.append(arg2.charAt(arg2Index));
--arg2Index;
}
continue;
}
int a1 = Integer.parseInt(arg1.charAt(arg1Index)+"");
int b2 = Integer.parseInt(arg2.charAt(arg2Index)+"");
int resultTemp = a1+b2;
int result2 = resultTemp%10+temp;
sb.append(result2);
temp = resultTemp/10;
--arg1Index;
--arg2Index;
}
// 补充较长数组位
//打印相加值
System.out.println(sb.reverse().toString());
}
}