Skip to content

Commit 04d937a

Browse files
committed
complete 43 big data multiply and 415 big data add by python
1 parent c40eaad commit 04d937a

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Keep thinking, keep alive
2-
Until 2018-01-25 19:54:00, I have solved **63** / **686** problems while **126** are still locked.
2+
Until 2018-01-27 14:23:21, I have solved **65** / **686** problems while **126** are still locked.
33

44
Completion statistic:
55
1. JavaScript: 59
6-
2. Python: 6
6+
2. Python: 8
77
3. C++: 1
88
4. Java: 1
99

@@ -55,7 +55,7 @@ Note: :lock: means you need to buy a book from LeetCode
5555
|040|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/description/) |Medium|[JavaScript](https://github.com/hey-bruce/algorithms_and_oj/blob/master/leetcode-algorithms/040.%20Combination%20Sum%20II/combinationSumTwo.js)|To Do|To Do|To Do|
5656
|041|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/description/) |Hard|[JavaScript](https://github.com/hey-bruce/algorithms_and_oj/blob/master/leetcode-algorithms/041.%20First%20Missing%20Positive/firstMissingPositive.js)|To Do|To Do|To Do|
5757
|042|[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) |Hard|To Do|To Do|To Do|To Do|
58-
|043|[Multiply Strings](https://leetcode.com/problems/multiply-strings/description/) |Medium|To Do|To Do|To Do|To Do|
58+
|043|[Multiply Strings](https://leetcode.com/problems/multiply-strings/description/) |Medium|To Do|[Python](https://github.com/hey-bruce/algorithms_and_oj/blob/master/leetcode-algorithms/043.%20Multiply%20Strings/solution.py)|To Do|To Do|
5959
|044|[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/description/) |Hard|To Do|To Do|To Do|To Do|
6060
|045|[Jump Game II](https://leetcode.com/problems/jump-game-ii/description/) |Hard|To Do|To Do|To Do|To Do|
6161
|046|[Permutations](https://leetcode.com/problems/permutations/description/) |Medium|To Do|To Do|To Do|To Do|
@@ -410,7 +410,7 @@ Note: :lock: means you need to buy a book from LeetCode
410410
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/) |Easy|To Do|To Do|To Do|To Do|
411411
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/description/) |Medium|To Do|To Do|To Do|To Do|
412412
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/description/) |Easy|[JavaScript](https://github.com/hey-bruce/algorithms_and_oj/blob/master/leetcode-algorithms/414.%20Third%20Maximum%20Number/thirdMax.js)|To Do|To Do|To Do|
413-
|415|[Add Strings](https://leetcode.com/problems/add-strings/description/) |Easy|To Do|To Do|To Do|To Do|
413+
|415|[Add Strings](https://leetcode.com/problems/add-strings/description/) |Easy|To Do|[Python](https://github.com/hey-bruce/algorithms_and_oj/blob/master/leetcode-algorithms/415.%20Add%20Strings/solution.py)|To Do|To Do|
414414
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) |Medium|To Do|To Do|To Do|To Do|
415415
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) |Medium|To Do|To Do|To Do|To Do|
416416
|418|[Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/description/) :lock:|Medium|To Do|To Do|To Do|To Do|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# Created by Bruce yuan on 18-1-27.
3+
4+
5+
class Solution:
6+
def multiply(self, num1, num2):
7+
"""
8+
:type num1: str
9+
:type num2: str
10+
:rtype: str
11+
"""
12+
# 因为题目说了不会用 0 开头,所以 0 开头一定是 0
13+
if num1[0] == '0' or num2[0] == '0':
14+
return '0'
15+
# 用一个数组来表示结果
16+
result = [0] * (len(num1) + len(num2))
17+
cur_pos = len(result) - 1
18+
19+
for first_str in reversed(num1):
20+
# 第一个乘数
21+
int1 = int(first_str)
22+
tmp_pos = cur_pos
23+
carry = 0
24+
for second_str in reversed(num2):
25+
# 第二个乘数
26+
int2 = int(second_str)
27+
tmp_result = int1 * int2
28+
# 返回进位和当前位置的数字,因为两个数相乘最多等于 81
29+
result[tmp_pos] += tmp_result
30+
result[tmp_pos - 1] += result[tmp_pos] // 10
31+
result[tmp_pos] = result[tmp_pos] % 10
32+
# print(result)
33+
tmp_pos -= 1
34+
cur_pos -= 1
35+
for index, item in enumerate(result):
36+
if item != 0:
37+
pos = index
38+
break
39+
return ''.join(str(item) for item in result[pos:])
40+
41+
42+
def main():
43+
a = Solution()
44+
result = a.multiply('999', '999')
45+
print(result)
46+
47+
if __name__ == '__main__':
48+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# Created by Bruce yuan on 18-1-27.
3+
4+
5+
class Solution:
6+
def addStrings(self, num1, num2):
7+
"""
8+
:type num1: str
9+
:type num2: str
10+
:rtype: str
11+
"""
12+
carry = 0
13+
result = []
14+
num1, num2 = list(num1), list(num2)
15+
while len(num2) > 0 or len(num1) > 0:
16+
n1 = int(num1.pop()) if len(num1) > 0 else 0
17+
n2 = int(num2.pop()) if len(num2) > 0 else 0
18+
tmp = n1 + n2 + carry
19+
result.append(tmp % 10)
20+
carry = tmp // 10
21+
if carry == 0:
22+
result.append(carry)
23+
return ''.join(str(x) for x in result[::-1])

0 commit comments

Comments
 (0)