Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb2a15e

Browse files
solves multiply strings
1 parent ccf18ab commit bb2a15e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | [![Java](assets/java.png)](src/CountAndSay.java) [![Python](assets/python.png)](python/count_and_say.py) | |
4545
| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum) | [![Java](assets/java.png)](src/CombinationSum.java) | |
4646
| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii) | [![Java](assets/java.png)](src/CombinationSumII.java) | |
47+
| 43 | [Multiply Strings](https://leetcode.com/problems/multiply-strings) | [![Java](assets/java.png)](src/MultiplyStrings.java) | |
4748
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | [![Java](assets/java.png)](src/MaximumSubArray.java) [![Python](assets/python.png)](python/maximum_sum_subarray.py) | |
4849
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | [![Java](assets/java.png)](src/LengthOfLastWord.java) [![Python](assets/python.png)](python/length_of_last_word.py) | |
4950
| 66 | [Plus One](https://leetcode.com/problems/plus-one) | [![Java](assets/java.png)](src/PlusOne.java) [![Python](assets/python.png)](python/plus_one.py) | |

‎src/MultiplyStrings.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// https://leetcode.com/problems/multiply-strings
2+
// T: O(|num1| * |num2|)
3+
// S: O(|num1| + |num2|)
4+
5+
public class MultiplyStrings {
6+
private static final String ZERO = "0";
7+
private static final String ONE = "1";
8+
9+
public String multiply(String num1, String num2) {
10+
if (ZERO.equals(num1) || ZERO.equals(num2)) return ZERO;
11+
if (ONE.equals(num1)) return num2;
12+
if (ONE.equals(num2)) return num1;
13+
14+
StringBuilder result = new StringBuilder(ZERO);
15+
for (int i = num2.length() - 1 ; i >= 0 ; i--) {
16+
result = add(result, multiply(num1, num2.charAt(i) - '0', num2.length() - i - 1));
17+
}
18+
return result.toString();
19+
}
20+
21+
private StringBuilder multiply(String number, int digit, int factor) {
22+
final StringBuilder result = new StringBuilder();
23+
int carry = 0;
24+
for (int i = number.length() - 1 ; i >= 0 ; i--) {
25+
int val = (number.charAt(i) - '0') * digit + carry;
26+
result.append(val % 10);
27+
carry = val / 10;
28+
}
29+
if (carry > 0) result.append(carry);
30+
return result.reverse().append(ZERO.repeat(factor));
31+
}
32+
33+
private StringBuilder add(StringBuilder a, StringBuilder b) {
34+
final StringBuilder result = new StringBuilder();
35+
int carry = 0;
36+
for (int i = a.length() - 1, j = b.length() - 1 ; i >= 0 || j >= 0 ; i--, j--) {
37+
int val = getDigit(a, i) + getDigit(b, j) + carry;
38+
result.append(val % 10);
39+
carry = val / 10;
40+
}
41+
if (carry > 0) result.append(carry);
42+
return result.reverse();
43+
}
44+
45+
private int getDigit(StringBuilder string, int index) {
46+
if (index < 0) return 0;
47+
return string.charAt(index) - '0';
48+
}
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /