8000 update · Mowmowj/leetcode@04a8683 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04a8683

Browse files
committed
update
1 parent faf5a4a commit 04a8683

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

problems/013. Roman to Integer/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 13. Roman to Integer
22

3+
## #1
4+
35
```java
46
class Solution {
57
public int romanToInt(String s) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 43. Multiply Strings
2+
3+
## #1 转化为字符串做乘法[AC]
4+
5+
### 思路
6+
7+
我们需要弄清楚如何做乘法运算,只有弄清楚了乘法运算之后才能比较好的求解这道题目。对于长度为`m`和长度为`n`的两个字符串数字相乘,我们得到的结果字符串的长度最多为`m+n`,然后我们考虑`num[i]``num[j]`相乘的结果应该放在哪个位置呢?相乘后的结果取余数和取模应该会被放在`[i+j,i+j+1]`的位置,看下图就明白了:
8+
9+
![stock-photo-130178585](../../../../../../../Desktop/stock-photo-130178585.jpg)
10+
11+
### 算法
12+
13+
```java
14+
class Solution {
15+
public String multiply(String num1, String num2) {
16+
int n = num1.length();
17+
int m = num2.length();
18+
int[] res = new int[m+n];
19+
for(int i=n-1; i>=0; i--){
20+
for(int j=m-1; j>=0; j--){
21+
int pos1 = i+j;
22+
int pos2 = i+j+1;
23+
int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
24+
int sum = mul + res[pos2];
25+
res[pos2] = sum % 10;
26+
res[pos1] += sum / 10;
27+
}
28+
}
29+
StringBuffer sb = new StringBuffer();
30+
for(int val : res){
31+
if(!(sb.length() == 0 && val == 0))
32+
sb.append(val);
33+
}
34+
return sb.length() == 0 ? "0" : sb.toString();
35+
}
36+
}
37+
```
38+
39+
### 复杂度分析:
40+
41+
- 时间复杂度:$O(nm)$
42+
- 空间复杂度:$O(m+n)$
43+

0 commit comments

Comments
 (0)
0