-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.py
171 lines (143 loc) · 3.92 KB
/
leetcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def isPalindrome(x):
"""
:type x: int
:rtype: bool
"""
if x < 0 or (x % 10 == 0 and x > 0):
return False
re = 0
while x > re:
re = re * 10 + x % 10
x = x//10
return x==re or x==re//10
def letterCombinations(digits):
"""
:type digits: str
:rtype: List[str]
"""
dict_ = {'1':'','2':"abc",'3':'def','4':'ghi','5':'jkl',
'6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
all_result = list(dict_[digits[0]])
for i in range(1,len(digits)):
temp = []
for char_a in all_result:
for char_b in dict_[digits[i]]:
temp.append(char_a+char_b)
all_result = temp
return all_result
def isValid(s):
dict = {')':'(',']':'[','}':'{'}
slist = list(s)
stack = []
for i in range(len(slist)):
if len(stack)>0 and dict.get(slist[i],0) == stack[-1]:
stack.pop()
else:
stack.append(slist[i])
if len(stack)>0:
return False
else:
return True
def fourSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
nums = sorted(nums)
length = len(nums)
result = []
if length < 3:
return result
last_i = 0
for i in range(length-3):
if not i == 0 and nums[last_i] == nums[i]:
continue
# 当前i和上一个i相同 跳过
last_i = i
last_j = i+1
for j in range(i+1, length-2):
# 当前j和上一个j相同 跳过
if not j == i+1 and nums[last_j] == nums[j]:
continue
last_j = j
two_sum = nums[i]+nums[j]
l = j + 1
r = length - 1
while l < r:
four_sum = two_sum + nums[l] + nums[r]
if four_sum == target:
temp = [nums[i], nums[j], nums[l], nums[r]]
result.append(temp)
while l<r and nums[l] == nums[l+1]:
l += 1
while l<r and nums[r] == nums[r-1]:
r -= 1
l += 1
r -= 1
if four_sum > target:
r -= 1
if four_sum < target:
l += 1
return result
# # print(fourSum([1, 0, -1, 0, -2, 2],target = 0))
# print(fourSum([-1,0,-5,-2,-2,-4,0,1,-2],-9))
# print(fourSum([0,0,0,0],0))
def threeSumClosest(nums, target):
"""
:type nums: List[int] nums = [-1,2,1,-4], 和 target = 1.
:type target: int
:rtype: int
"""
nums = sorted(nums)
length = len(nums)
def sum(a,b,c):
return nums[a]+nums[b]+nums[c]
import sys
min_dist = sys.maxsize
result = 0
for i in range(length-2):
l = i + 1
r = length - 1
while l < r:
sum_3 = sum(i,l,r)
if min_dist > abs(sum_3 - target):
min_dist = abs(sum_3 - target)
result = sum_3
if sum_3 < target:
l += 1
elif sum_3 > target:
r -= 1
else:
return sum_3
return result
def twoSum(nums, target):
list = []
for i in range(len(nums)):
to_find = target - nums[i]
temp = nums[i]
nums[i] = to_find + 1
if to_find in nums:
j = nums.index(to_find)
list.append(i)
list.append(j)
nums[i] = temp
return list
return list
def strStr(haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(needle) < 1:
return 0
for i in range(len(haystack)-len(needle)+1):
if haystack[i] == needle[0]:
j = 0
while haystack[i + j] == needle[j]:
if j == len(needle)-1:
return i
j += 1
return -1
print(strStr('','aa'))