Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- StringBuilder add(StringBuilder a, StringBuilder b) {
- int p1 = a.length() - 1;
- int p2 = b.length() - 1;
- int c = 0;
- StringBuilder sb = new StringBuilder();
- while (p1 >= 0 && p2 >= 0) {
- int d1 = a.charAt(p1) - '0';
- int d2 = b.charAt(p2) - '0';
- int s = (d1 + d2 + c);
- int d3 = (s % 10);
- c = s / 10;
- sb.append((char) (d3 + '0'));
- p1--;
- p2--;
- }
- while (p1 >= 0) {
- int d1 = a.charAt(p1) - '0';
- int s = (d1 + c);
- int d = s % 10;
- c = s / 10;
- sb.append((char) (d + '0'));
- p1--;
- }
- while (p2 >= 0) {
- int d1 = b.charAt(p2) - '0';
- int s = (d1 + c);
- int d = s % 10;
- c = s / 10;
- sb.append((char) (d + '0'));
- p2--;
- }
- if (c > 0) sb.append((char) (c + '0'));
- int sbLen = sb.length() - 1;
- while (sbLen > 0 && sb.charAt(sbLen) == '0') {
- sb.deleteCharAt(sbLen);
- sbLen--;
- }
- if (sb.length() == 0) return new StringBuilder("0");
- sb.reverse();
- return sb;
- }
- public String multiply(String num1, String num2) {
- StringBuilder ans = new StringBuilder("0");
- int pos = 0;
- for (int i = num1.length() - 1; i >= 0; i--) {
- StringBuilder currentResult = new StringBuilder();
- for (int x = 0; x < pos; x++) {
- currentResult.append("0");
- }
- int md = num1.charAt(i) - '0';
- int c = 0;
- for (int j = num2.length() - 1; j >= 0; j--) {
- int cd = num2.charAt(j) - '0';
- int s = (cd * md) + c;
- int ad = (s % 10); // ans digit
- c = s / 10;
- currentResult.append((char) (ad + '0'));
- }
- if (c > 0) {
- currentResult.append((char) (c + '0'));
- }
- int sbLen = currentResult.length() - 1;
- while (sbLen > 0 && currentResult.charAt(sbLen) == '0') {
- currentResult.deleteCharAt(sbLen);
- sbLen--;
- }
- if (currentResult.length() == 0) currentResult = new StringBuilder("0");
- currentResult.reverse();
- ans = add(ans, currentResult);
- pos++;
- }
- return ans.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment