public String addStrings(String num1, String num2) { StringBuilder res = new StringBuilder(); StringBuilder s1 = new StringBuilder(num1); StringBuilder s2 = new StringBuilder(num2); int flag = 0; while (s1.length()>0||s2.length()>0||flag!=0) { int cur = 0; if (s1.length()>0) { cur+=s1.charAt(s1.length()-1)-'0'; s1.deleteCharAt(s1.length()-1); } if (s2.length()>0) { cur+=s2.charAt(s2.length()-1)-'0'; s2.deleteCharAt(s2.length()-1); } cur+=flag; if (cur>9) flag = 1; else flag = 0; res.append(cur%10); } return res.reverse().toString(); }
直接对字符串进行操作