一、题目内容

image.png

二、题解

解法1:

思路

代码

  1. public class Solution {
  2. public static String solve(String s, String t) {
  3. // write code here
  4. if (s.length() == 0 || t.length() == 0) {
  5. return null;
  6. }
  7. if (s.length() < t.length()) {
  8. String temp = s;
  9. s = t;
  10. t = temp;
  11. }
  12. int si = s.length() - 1;
  13. int ti = t.length() - 1;
  14. StringBuilder sb = new StringBuilder();
  15. int c = 0;
  16. while (ti >= 0) {
  17. int temp = (s.charAt(si) - '0') + (t.charAt(ti) - '0') + c;
  18. sb.append(temp % 10);
  19. c = temp / 10;
  20. ti--;
  21. si--;
  22. }
  23. while (si >= 0) {
  24. int temp = c + (s.charAt(si) - '0');
  25. sb.append(temp % 10);
  26. c = temp / 10;
  27. si--;
  28. }
  29. if (c != 0) {
  30. sb.append(c);
  31. }
  32. return sb.reverse().toString();
  33. }
  34. }