Skip to content

Commit d8f85e8

Browse files
committed
refacotr
1 parent 4242f7c commit d8f85e8

File tree

186 files changed

+1040
-620
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+1040
-620
lines changed

README.md

+215-161
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from typing import List
2+
3+
24
class Solution:
3-
def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]: # type: ignore
46
"""
57
:type nums: List[int]
68
:type target: int
79
:rtype: List[int]
810
"""
9-
numMap = {} # value: index
11+
numMap = {} # value: index
1012
for index, value in enumerate(nums):
1113
diff = target - value
1214
if diff in numMap:
@@ -17,4 +19,4 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
1719
if __name__ == '__main__':
1820
nums = [2, 3, 5, 7, 11, 15]
1921
target = 9
20-
print(Solution().twoSum(nums, target))
22+
print(Solution().twoSum(nums, target))

leetcode/python/0002-Add-Two-Numbers/AddTwoNumbers.py leetcode/python/0002-Add-Two-Numbers/solution.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Optional
22

33
# Definition for singly-linked list.
4-
# class ListNode:
5-
# def __init__(self, val=0, next=None):
6-
# self.val = val
7-
# self.next = next
4+
5+
6+
class ListNode:
7+
def __init__(self, val=0, next=None):
8+
self.val = val
9+
self.next = next
810

911

1012
class Solution:

leetcode/python/0008-String-to-Integer/StringtoInteger.py leetcode/python/0008-String-to-Integer/solution.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import math
2+
import re
3+
4+
15
class Solution:
26
def myAtoi(self, s: str) -> int:
37
s = s.strip()
4-
8+
59
if not s:
610
return 0
7-
11+
812
match_str = re.findall(r"^[+-]?\d+", s)
913

1014
if not match_str:
@@ -19,4 +23,4 @@ def myAtoi(self, s: str) -> int:
1923
else:
2024
res = int(match_str[0])
2125

22-
return res
26+
return res
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution:
22
def intToRoman(self, num: int) -> str:
3-
symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
3+
symbols = ["M", "CM", "D", "CD", "C", "XC",
4+
"L", "XL", "X", "IX", "V", "IV", "I"]
45
bases = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
56
res = ""
67
for symbol, base in zip(symbols, bases):
78
if num // base:
89
count = num // base
910
res += symbol * count
1011
num = num % base
11-
return res
12+
return res
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution:
22
def romanToInt(self, s: str) -> int:
3-
dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
3+
dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
4+
'C': 100, 'D': 500, 'M': 1000}
45
ans = 0
56
for i in range(len(s)):
67
if i < len(s)-1 and dict[s[i]] < dict[s[i+1]]:
@@ -9,6 +10,7 @@ def romanToInt(self, s: str) -> int:
910
ans += dict[s[i]]
1011
return ans
1112

13+
1214
if __name__ == '__main__':
1315
s = "MCMXCIV"
14-
print(Solution().romanToInt(s))
16+
print(Solution().romanToInt(s))

leetcode/python/0014-Longest-Common-Prefix/LongestCommonPrefix.py leetcode/python/0014-Longest-Common-Prefix/solution.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from typing import List
2+
3+
24
class Solution:
35
def longestCommonPrefix(self, strs: List[str]) -> str:
46
last = strs.pop()
@@ -8,6 +10,7 @@ def longestCommonPrefix(self, strs: List[str]) -> str:
810
return last[:index]
911
return last
1012

13+
1114
if __name__ == '__main__':
1215
strs = ["flower", "flow", "flight"]
13-
print(Solution().longestCommonPrefix(strs))
16+
print(Solution().longestCommonPrefix(strs))

leetcode/python/0015-3Sum/3Sum.py leetcode/python/0015-3Sum/solution.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def threeSum(self, nums: List[int]) -> List[List[int]]:
2727
right -= 1
2828
return res
2929

30+
3031
if __name__ == '__main__':
31-
nums = [-1,0,1,2,-1,-4]
32-
print(Solution().threeSum(nums))
32+
nums = [-1, 0, 1, 2, -1, -4]
33+
print(Solution().threeSum(nums))

leetcode/python/0019-Remove-Nth-Node-From-End-of-List/solution.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
from typing import Optional
12
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
611
class Solution:
712
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
813
if not head:

leetcode/python/0021-Merge-Two-Sorted-Lists/MergeTwoSortedLists.py leetcode/python/0021-Merge-Two-Sorted-Lists/solution.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
62
from typing import Optional
73

84

5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
911
class Solution:
1012
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
1113
dummy = temp = ListNode(0)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def countAndSay(self, n: int) -> str:
33
s = "1"
4-
4+
55
for _ in range(1, n):
66
prev, count = "#", 0
77
currStr = ""
@@ -12,8 +12,8 @@ def countAndSay(self, n: int) -> str:
1212
currStr += str(count) + prev
1313
count = 1
1414
prev = char
15-
15+
1616
currStr += str(count) + prev
1717
s = currStr
18-
19-
return s
18+
19+
return s

leetcode/python/0042-Trapping-Rain-Water/TrappingRainWater.py leetcode/python/0042-Trapping-Rain-Water/solution.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from typing import List
2+
3+
14
class Solution:
25
def trap(self, height: List[int]) -> int:
36
if not height:
47
return 0
5-
8+
69
res = 0
710
left, right = 0, len(height) - 1
811
leftMax, rightMax = height[left], height[right]
@@ -15,5 +18,5 @@ def trap(self, height: List[int]) -> int:
1518
right -= 1
1619
rightMax = max(rightMax, height[right])
1720
res += rightMax - height[right]
18-
19-
return res
21+
22+
return res

leetcode/python/0048-Rotate–Image/solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def rotate(self, matrix: List[List[int]]) -> None:
1818
matrix[top+i][right] = topLeft
1919
right -= 1
2020
left += 1
21-
return matrix
21+
return matrix # type: ignore
2222

2323

2424
if __name__ == '__main__':

leetcode/python/0057-Insert-Interval/InsertInterval.py

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
6+
res = []
7+
8+
for interval in intervals:
9+
if newInterval[1] < interval[0]:
10+
res.append(newInterval)
11+
newInterval = interval
12+
elif newInterval[0] > interval[1]:
13+
res.append(interval)
14+
else:
15+
newInterval = [min(newInterval[0], interval[0]),
16+
max(newInterval[1], interval[1])]
17+
18+
res.append(newInterval)
19+
return res
20+
21+
22+
# class Solution:
23+
# def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
24+
# res = []
25+
26+
# for i in range(len(intervals)):
27+
# if newInterval[1] < intervals[i][0]:
28+
# res.append(newInterval)
29+
# return res + intervals[i:]
30+
# elif newInterval[0] > intervals[i][1]:
31+
# res.append(intervals[i])
32+
# else:
33+
# newInterval = [min(newInterval[0], intervals[i][0]), max(
34+
# newInterval[1], intervals[i][1])]
35+
36+
# res.append(newInterval)
37+
# return res
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
from typing import Optional
12
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
611
class Solution:
712
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
813
if not head or k == 0:
914
return head
10-
15+
1116
curr, length = head, 0
1217
while curr:
1318
length += 1
1419
prev, curr = curr, curr.next
15-
else:
20+
else:
1621
prev.next = head
1722

1823
count = length - (k % length)
1924

2025
for _ in range(count):
2126
prev, head = head, head.next
2227
prev.next = None
23-
return head
28+
return head
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def addBinary(self, a: str, b: str) -> str:
3-
return bin(int(a, 2) + int(b, 2))[2:]
3+
return bin(int(a, 2) + int(b, 2))[2:]

leetcode/python/0071-Simplify-Path/SimplifyPath.py leetcode/python/0071-Simplify-Path/solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ def simplifyPath(self, path: str) -> str:
1212

1313
if __name__ == '__main__':
1414
path = "/home/"
15-
print(Solution().simplifyPath(path))
15+
print(Solution().simplifyPath(path))

leetcode/python/0075-Sort-Colors/SortColors.py leetcode/python/0075-Sort-Colors/solution.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import List
2+
3+
14
class Solution:
25
def sortColors(self, nums: List[int]) -> None:
36
"""
@@ -12,5 +15,5 @@ def sortColors(self, nums: List[int]) -> None:
1215
elif nums[p1] == 2:
1316
nums[p1], nums[p2] = nums[p2], nums[p1]
1417
p2 -= 1
15-
else:
18+
else:
1619
p1 += 1

leetcode/python/0076-Minimum-Window-Substring/MinimumWindowSubstring.py leetcode/python/0076-Minimum-Window-Substring/solution.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import collections
2+
3+
14
class Solution:
25
def minWindow(self, s: str, t: str) -> str:
36
if not t or not s:
@@ -8,7 +11,7 @@ def minWindow(self, s: str, t: str) -> str:
811
res, res_len = [None, None], float("inf")
912
window_count = {}
1013
left, right = 0, 0
11-
14+
1215
while right < len(s):
1316
char = s[right]
1417
window_count[char] = 1 + window_count.get(char, 0)
@@ -21,8 +24,8 @@ def minWindow(self, s: str, t: str) -> str:
2124

2225
if right - left + 1 < res_len:
2326
res = [left, right]
24-
res_len = right - left + 1
25-
27+
res_len = right - left + 1
28+
2629
window_count[char] -= 1
2730
if char in t_count and window_count[char] < t_count[char]:
2831
have -= 1
@@ -33,7 +36,4 @@ def minWindow(self, s: str, t: str) -> str:
3336

3437
left, right = res
3538

36-
return s[left : right + 1] if res_len != float("inf") else ""
37-
38-
39-
39+
return s[left: right+1] if res_len != float("inf") else ""

0 commit comments

Comments
 (0)