Skip to content

Commit 8787c83

Browse files
committed
leetcode 43
python
1 parent a04fb92 commit 8787c83

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
1010

1111
| # | Difficulty | Problem | Solution |
1212
| -- | ---------- | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
13-
| 1 | Easy | [Two Sum](leetcode/1-Easy-Two-Sum/problem.md) | [Java](leetcode/1-Easy-Two-Sum/answer.java), [Ruby](leetcode/1-Easy-Two-Sum/answer.rb)|
13+
| 1 | Easy | [Two Sum](leetcode/1-Easy-Two-Sum/problem.md) | [Java](leetcode/1-Easy-Two-Sum/answer.java), [Python](leetcode/1-Easy-Two-Sum/answer.py), [Ruby](leetcode/1-Easy-Two-Sum/answer.rb)|
1414
| 2 | Medium | [Add Two Numbers](leetcode/2-Medium-Add-Two-Numbers/problem.md) | [C](leetcode/2-Medium-Add-Two-Numbers/answer.c) |
1515
| 3 | Medium | [Longest Substring Without Repeating Characters](leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/problem.md) | [Python](leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/answer.py) |
1616
| 4 | Hard | [Median of Two Sorted Arrays](leetcode/4-Hard-Median-Of-Two-Sorted-Arrays/problem.md) | [Python](leetcode/4-Hard-Median-Of-Two-Sorted-Arrays/answer.py) |
@@ -22,7 +22,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
2222
| 10 | Hard | [Regular Expression Matching](leetcode/10-Hard-Regular-Expression-Matching/problem.md) | [Python](leetcode/10-Hard-Regular-Expression-Matching/answer.py) |
2323
| 11 | Medium | [Container With Most Water](leetcode/11-Medium-Container-With-Most-Water/problem.md) | [Python](leetcode/11-Medium-Container-With-Most-Water/answer.py) |
2424
| 12 | Medium | [Integer to Roman](leetcode/12-Medium-Integer-To-Roman/problem.md) | [Python](leetcode/12-Medium-Integer-To-Roman/answer.py) |
25-
| 13 | Easy | [Roman to Integer](leetcode/13-Easy-Roman-To-Integer/problem.md) | [C](leetcode/13-Easy-Roman-To-Integer/answer.c) |
25+
| 13 | Easy | [Roman to Integer](leetcode/13-Easy-Roman-To-Integer/problem.md) | [C](leetcode/13-Easy-Roman-To-Integer/answer.c), [Python](leetcode/13-Easy-Roman-To-Integer/answer.py)|
2626
| 14 | Easy | [Longest Common Prefix](leetcode/14-Easy-Longest-Common-Prefix/problem.md) | [Python](leetcode/14-Easy-Longest-Common-Prefix/answer.py) |
2727
| 15 | Medium | [3Sum](leetcode/15-Medium-3Sum/problem.md) | [Python](leetcode/15-Medium-3Sum/answer.py) |
2828
| 16 | Medium | [3Sum Closest](leetcode/16-Medium-3Sum-Closest/problem.md) | [Python](leetcode/16-Medium-3Sum-Closest/answer.py) |
@@ -46,6 +46,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
4646
| 39 | Medium | [Combination Sum](leetcode/39-Medium-Combination-Sum/problem.md) | [Python](leetcode/39-Medium-Combination-Sum/answer.py) |
4747
| 40 | Medium | [Combination Sum II](leetcode/40-Medium-Combination-Sum-II/problem.md) | [Python](leetcode/40-Medium-Combination-Sum-II/answer.py) |
4848
| 42 | Hard | [Trapping Rain Water](leetcode/42-Hard-Trapping-Rain-Water/problem.md) | [Python](leetcode/42-Hard-Trapping-Rain-Water/answer.py) |
49+
| 43 | Medium | [Multiply Strings](leetcode/43-Medium-Multiply-Strings/problem.md) | [Python](leetcode/43-Medium-Multiply-Strings/answer.py) |
4950
| 44 | Hard | [Wildcard Matching](leetcode/44-Hard-Wildcard-Matching/problem.md) | [Python](leetcode/44-Hard-Wildcard-Matching/answer.py) |
5051
| 46 | Medium | [Permutations](leetcode/46-Medium-Permutations/problem.md) | [Python](leetcode/46-Medium-Permutations/answer.py) |
5152
| 47 | Medium | [Permutations II](leetcode/47-Medium-Permutations-II/problem.md) | [Python](leetcode/47-Medium-Permutations-II/answer.py) |

leetcode/1-Easy-Two-Sum/answer.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python3
2+
3+
#-------------------------------------------------------------------------------
4+
5+
class Solution:
6+
def twoSum(self, nums, target):
7+
"""
8+
:type nums: List[int]
9+
:type target: int
10+
:rtype: List[int]
11+
"""
12+
need = {}
13+
for i in range(len(nums)):
14+
if nums[i] in need:
15+
return [need[nums[i]], i]
16+
else:
17+
need[target-nums[i]] = i
18+
19+
#-------------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
5+
class Solution:
6+
def romanToInt(self, s):
7+
"""
8+
:type s: str
9+
:rtype: int
10+
"""
11+
result = 0
12+
for i in range(len(s)):
13+
if s[i] =='M':
14+
result += 1000
15+
elif s[i] == 'D':
16+
result += 500
17+
elif s[i] == 'C':
18+
if (i+1) < len(s) and (s[i+1] == 'D' or s[i+1] == 'M'):
19+
result -= 100
20+
else:
21+
result += 100
22+
elif s[i] == 'L':
23+
result += 50
24+
elif s[i] == 'X':
25+
if (i+1) < len(s) and (s[i+1] == 'C' or s[i+1] == 'L'):
26+
result -= 10
27+
else:
28+
result += 10
29+
elif s[i] == 'V':
30+
result += 5
31+
elif s[i] == "I":
32+
if (i+1) < len(s) and (s[i+1] == 'X' or s[i+1] == 'V'):
33+
result -= 1
34+
else:
35+
result += 1
36+
37+
return result
38+
39+
#------------------------------------------------------------------------------
40+
#Testing
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
5+
class Solution(object):
6+
def multiply(self, num1, num2):
7+
"""
8+
:type num1: str
9+
:type num2: str
10+
:rtype: str
11+
"""
12+
if num1 == "0" or num2 == "0":
13+
return "0"
14+
15+
# Our products list must have a leading 0 because a product may carry over
16+
products = [0]*(len(num1)+len(num2))
17+
for i in range(len(num1)-1, -1, -1):
18+
for j in range(len(num2)-1, -1, -1):
19+
products[i+j+1] += int(num2[j])*int(num1[i])
20+
21+
# Calculate each digit and each carry
22+
carry = 0
23+
for i in range(len(products)-1, -1, -1):
24+
products[i], carry = (products[i] + carry) % 10, (products[i] + carry) // 10
25+
26+
# Just put the digits in a string!
27+
result = ""
28+
for digit in products:
29+
result += str(digit)
30+
31+
return result.lstrip("0")
32+
33+
#------------------------------------------------------------------------------
34+
#Testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Problem 43: Multiply Strings
2+
3+
#### Difficulty: Medium
4+
5+
#### Problem
6+
7+
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
8+
9+
**Note:**
10+
11+
1. The length of both num1 and num2 is < 110.
12+
2. Both num1 and num2 contain only digits 0-9.
13+
3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
14+
4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
15+
16+
#### Example
17+
18+
<pre>
19+
Example 1:
20+
21+
Input: num1 = "2", num2 = "3"
22+
Output: "6"
23+
24+
Example 2:
25+
26+
Input: num1 = "123", num2 = "456"
27+
Output: "56088"
28+
</pre>

0 commit comments

Comments
 (0)