-
Notifications
You must be signed in to change notification settings - Fork 171
/
MultiplyStrings.java
37 lines (36 loc) · 1.23 KB
/
MultiplyStrings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Author: King, [email protected]
Date: Dec 20, 2014
Problem: Multiply Strings
Difficulty: Easy
Source: https://oj.leetcode.com/problems/multiply-strings/
Notes:
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
Solution: Just like what we do when multiplying integers.
*/
public class Solution {
public String multiply(String num1, String num2) {
int l1 = num1.length(), l2 = num2.length();
if (l1 == 0 || l2 == 0) return new String("");
if (num1.charAt(0) == '0' || num2.charAt(0) == '0') return new String("0");
StringBuffer sb = new StringBuffer();
int[] res = new int[l1+l2];
for (int i = 0; i < l1; ++i) {
for (int j = 0; j < l2; ++j) {
res[i+j+1] += (num1.charAt(i)-'0') *(num2.charAt(j)-'0');
}
}
int c = 0;
for (int i = res.length - 1; i>=1; --i) {
res[i] += c;
c = res[i] / 10;
res[i] = res[i] % 10;
sb.insert(0,res[i]);
}
if (c !=0 || res[0] != 0) {
sb.insert(0,c+res[0]);
}
return sb.toString();
}
}