🚩传送门:牛客题目
🍗鸡腿知识点:
BigInteger bigInteger1 = new BigInteger(s);- StringBuffer res=new StringBuffer();
res.reverse().toString()自带反转函数
题目
以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回。
数据范围:,字符串仅由
0 ~ 9构成
要求: 空间复杂度
仅在传入字符串上操作),时间复杂度
![]()
解题思路:大数类型
整理代码
public class Solution {public String solve (String s, String t) {BigInteger bigInteger1 = new BigInteger(s);BigInteger bigInteger2 = new BigInteger(t);return bigInteger1.add(bigInteger2).toString();}}
解题思路:加法模拟
我的代码
public class Solution {public String solve (String s, String t) {int s1=s.length()-1;int t1=t.length()-1;int sum=0;//1.使用 StringBuffer 是因其自带 reverse 函数StringBuffer res=new StringBuffer();while(s1>=0||t1>=0){if(s1>=0)sum+=(s.charAt(s1--)-'0');if(t1>=0)sum+=(t.charAt(t1--)-'0');res.append(sum%10);sum/=10;}return sum>0?(res.append(sum%10)).reverse().toString():res.reverse().toString();}}
