File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 题目
2
+
3
+ 给定两个用字符串num1和num2表示的整数,返回它们相乘的结果。
4
+
5
+ ** 注意:**
6
+
7
+ - num1和num2的长度均小于110
8
+ - num1和num2只包含0-9的数字
9
+ - num1和num2不以0开头
10
+ - 不能使用内联的BigInteger库,不能上来直接将num1和num2转换为int型
11
+
12
+ ** 举例:**
13
+
14
+ ```
15
+ 输入:
16
+ '12', '11'
17
+ 输出:
18
+ '132'
19
+ ```
20
+
21
+ # 分析
10000
22
+
23
+ 这题如果这么做就非常简单:
24
+
25
+ ``` python
26
+ class Solution (object ):
27
+ def multiply (self , num1 , num2 ):
28
+ """
29
+ :type num1: str
30
+ :type num2: str
31
+ :rtype: str
32
+ """
33
+ return str (int (num1) * int (num2))
34
+ ```
35
+
36
+ 可以直接运行,也能出结果,Beats 95%以上。那是因为你没有按照人家要求玩,没意义。
37
+
38
+ 这道题,我们可以使用一个列表,统计它们的计算结果。思路简单,直接上代码看吧。enumerate()很好用啊。
39
+
40
+ # 代码
41
+
42
+ ``` python
43
+ class Solution (object ):
44
+ def multiply (self , num1 , num2 ):
45
+ """
46
+ :type num1: str
47
+ :type num2: str
48
+ :rtype: str
49
+ """
50
+ res = [0 ] * (len (num1) + len (num2))
51
+ for i, e1 in enumerate (reversed (num1)):
52
+ for j, e2 in enumerate (reversed (num2)):
53
+ res[i+ j] += int (e1) * int (e2)
54
+ res[i+ j+ 1 ] += res[i+ j] // 10
55
+ res[i+ j] %= 10
56
+ print (res)
57
+ while len (res) > 1 and res[- 1 ] == 0 :
58
+ res.pop()
59
+ return ' ' .join(map (str , res[::- 1 ]))
60
+ ```
61
+
62
+
You can’t perform that action at this time.
0 commit comments