najimCseJu

OPT02

Sep 22nd, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. class Solution {
  2.     StringBuilder add(StringBuilder a, StringBuilder b) {
  3.         int p1 = a.length() - 1;
  4.         int p2 = b.length() - 1;
  5.         int c = 0;
  6.         StringBuilder sb = new StringBuilder();
  7.         while (p1 >= 0 && p2 >= 0) {
  8.             int d1 = a.charAt(p1) - '0';
  9.             int d2 = b.charAt(p2) - '0';
  10.             int s = (d1 + d2 + c);
  11.             int d3 = (s % 10);
  12.             c = s / 10;
  13.             sb.append((char) (d3 + '0'));
  14.             p1--;
  15.             p2--;
  16.         }
  17.         while (p1 >= 0) {
  18.             int d1 = a.charAt(p1) - '0';
  19.             int s = (d1 + c);
  20.             int d = s % 10;
  21.             c = s / 10;
  22.             sb.append((char) (d + '0'));
  23.             p1--;
  24.         }
  25.         while (p2 >= 0) {
  26.             int d1 = b.charAt(p2) - '0';
  27.             int s = (d1 + c);
  28.             int d = s % 10;
  29.             c = s / 10;
  30.             sb.append((char) (d + '0'));
  31.             p2--;
  32.         }
  33.         if (c > 0) sb.append((char) (c + '0'));
  34.         int sbLen = sb.length() - 1;
  35.         while (sbLen > 0 && sb.charAt(sbLen) == '0') {
  36.             sb.deleteCharAt(sbLen);
  37.             sbLen--;
  38.         }
  39.         if (sb.length() == 0) return new StringBuilder("0");
  40.         sb.reverse();
  41.         return sb;
  42.     }
  43.  
  44.     public String multiply(String num1, String num2) {
  45.         StringBuilder ans = new StringBuilder("0");
  46.         int pos = 0;
  47.         for (int i = num1.length() - 1; i >= 0; i--) {
  48.             StringBuilder currentResult = new StringBuilder();
  49.             for (int x = 0; x < pos; x++) {
  50.                 currentResult.append("0");
  51.             }
  52.             int md = num1.charAt(i) - '0';
  53.             int c = 0;
  54.             for (int j = num2.length() - 1; j >= 0; j--) {
  55.                 int cd = num2.charAt(j) - '0';
  56.                 int s = (cd * md) + c;
  57.                 int ad = (s % 10); // ans digit
  58.                 c = s / 10;
  59.                 currentResult.append((char) (ad + '0'));
  60.             }
  61.             if (c > 0) {
  62.                 currentResult.append((char) (c + '0'));
  63.             }
  64.             int sbLen = currentResult.length() - 1;
  65.             while (sbLen > 0 && currentResult.charAt(sbLen) == '0') {
  66.                 currentResult.deleteCharAt(sbLen);
  67.                 sbLen--;
  68.             }
  69.             if (currentResult.length() == 0) currentResult = new StringBuilder("0");
  70.             currentResult.reverse();
  71.             ans = add(ans, currentResult);
  72.             pos++;
  73.         }
  74.         return ans.toString();
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment