大数加法
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475?tpId=196&tqId=37176&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2Fjob-code-total%2Fquestion-ranking&tab=answerKey
public String solve (String s, String t) { if (s==null || t==null) return null; // write code here int i = s.length()-1; int j = t.length()-1; int jw=0; Stack<Integer> stack=new Stack<>(); while (i>=0 && j>=0){ int si = Integer.parseInt(s.charAt(i)+""); int sj = Integer.parseInt(t.charAt(j)+""); int zh = si+sj+jw; jw = zh/10; int res = zh%10; stack.add(res); i--; j--; } //i s stack jw = zz(i,s,stack,jw); jw = zz(j,t,stack,jw); if (jw>0) stack.add(jw); StringBuilder sb = new StringBuilder(); while (stack.size()>0){ sb.append(stack.pop()); } return sb.toString(); } public int zz(int i,String s,Stack<Integer> stack,int jw){ while (i>=0){ int si = Integer.parseInt(s.charAt(i)+""); int zh = si+jw; stack.add(zh%10); jw = zh/10; i--; } return jw; }