Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f791e2f

Browse files
add some code
1 parent 81335bc commit f791e2f

10 files changed

+257
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238

239239

240240
## LeetCode 1~400顺序题解
241-
- [python版本](https://github.com/huxiaoman7/leetcodebook/tree/master/python)
241+
- [python版本](https://github.com/huxiaoman7/leetcodebook/tree/master/python) | 已更新1~60
242242
- [java版本](https://github.com/huxiaoman7/leetcodebook/tree/master/java)
243243
- [c++版本](https://github.com/huxiaoman7/leetcodebook/tree/master/c++)
244244

‎python/10_RegularExpressionMatching.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,41 @@
55
# @File : 10_RegularExpressionMatching.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
import re
11+
# 方法一:正则匹配
12+
class Solution1(object):
13+
def isMatch(self, s, p):
14+
"""
15+
:type s: str
16+
:type p: str
17+
:rtype: bool
18+
"""
19+
return re.match('^'+p+'$',s) != None
20+
21+
22+
#方法二:dp
23+
24+
class Solution2(object):
25+
26+
def isMatch(self, s, p):
27+
dp = [[False] * (len(s) + 1) for _ in range(len(p) + 1)]
28+
print dp
29+
dp[0][0] = True
30+
for i in range(1, len(p)):
31+
dp[i + 1][0] = dp[i - 1][0] and p[i] == '*'
32+
for i in range(len(p)):
33+
for j in range(len(s)):
34+
if p[i] == '*':
35+
dp[i + 1][j + 1] = dp[i - 1][j + 1] or dp[i][j + 1]
36+
if p[i - 1] == s[j] or p[i - 1] == '.':
37+
dp[i + 1][j + 1] |= dp[i + 1][j]
38+
else:
39+
dp[i + 1][j + 1] = dp[i][j] and (p[i] == s[j] or p[i] == '.')
40+
return dp[-1][-1]
41+
42+
43+
if __name__ == '__main__':
44+
s = Solution2()
45+
print s.isMatch('aa','a')

‎python/11_ContainerWithMostWater.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,29 @@
55
# @File : 11_ContainerWithMostWater.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
# two pointers
11+
class Solution(object):
12+
def maxArea(self, height):
13+
"""
14+
:type height: List[int]
15+
:rtype: int
16+
"""
17+
area = 0
18+
l = 0
19+
r = len(height)-1
20+
21+
while l < r:
22+
area = max(area,min(height[l],height[r])*(r-l))
23+
if height[r] > height[l]:
24+
l += 1
25+
else:
26+
r -=1
27+
return area
28+
29+
30+
31+
if __name__ == '__main__':
32+
s = Solution()
33+
print s.maxArea([1,1,3,4,5])

‎python/12_IntegerToRoman.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,20 @@
55
# @File : 12_IntegerToRoman.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def intToRoman(self, num):
12+
"""
13+
:type num: int
14+
:rtype: str
15+
"""
16+
M = ["", "M", "MM", "MMM"]
17+
C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
18+
X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
19+
I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
20+
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]
21+
22+
if __name__ == '__main__':
23+
s = Solution()
24+
print s.intToRoman(3)

‎python/13_RomanToInteger.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,27 @@
55
# @File : 13_RomanToInteger.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def romanToInt(self, s):
12+
"""
13+
:type s: str
14+
:rtype: int
15+
"""
16+
digits = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
17+
sum = 0
18+
maxDigit = 1
19+
for i in xrange(len(s)-1, -1, -1):
20+
if digits[s[i]] >= maxDigit:
21+
maxDigit = digits[s[i]]
22+
sum += digits[s[i]]
23+
else:
24+
sum -= digits[s[i]]
25+
26+
return sum
27+
28+
29+
if __name__=='__main__':
30+
s = Solution()
31+
print s.romanToInt("XC")

‎python/14_LongestCommonPrefix.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,44 @@
55
# @File : 14_LongestCommonPrefix.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
# 常规解法
11+
class Solution1(object):
12+
def longestCommonPrefix(self, strs):
13+
"""
14+
:type strs: List[str]
15+
:rtype: str
16+
"""
17+
18+
if not strs:
19+
return ''
20+
res=''
21+
for i in xrange(len(strs[0])):
22+
for j in xrange(1,len(strs)):
23+
if i >=len(strs[j]) or strs[j][i] !=strs[0][i]:
24+
return res
25+
res += strs[0][i]
26+
print res
27+
return res
28+
29+
30+
# 非常直观的一个解法,以最小的str去一一比对
31+
class Solution2(object):
32+
def longestCommonPrefix(self, strs):
33+
"""
34+
:type strs: List[str]
35+
:rtype: str
36+
"""
37+
if not strs:
38+
return ""
39+
shortest = min(strs,key=len)
40+
for i, ch in enumerate(shortest):
41+
for other in strs:
42+
if other[i] != ch:
43+
return shortest[:i]
44+
return shortest
45+
46+
if __name__ == '__main__':
47+
s = Solution2()
48+
print s.longestCommonPrefix(['flower','flight','floor'])

‎python/15_3Sum.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,29 @@
55
# @File : 15_3Sum.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def threeSum(self, nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: List[List[int]]
15+
"""
16+
if len(nums) < 3:
17+
return []
18+
nums.sort()
19+
res = set()
20+
for i, v in enumerate(nums[:-2]):
21+
if i >= 1 and v == nums[i-1]:
22+
continue
23+
d = {}
24+
for x in nums[i+1:]:
25+
if x not in d:
26+
d[-v-x] = 1
27+
else:
28+
res.add((v, -v-x, x))
29+
return map(list, res)
30+
31+
if __name__ =='__main__':
32+
s = Solution()
33+
print s.threeSum([-1,0,1,2,-1,-4])

‎python/16_3SumCloset.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,34 @@
55
# @File : 16_3SumCloset.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
import itertools
10+
class Solution(object):
11+
def threeSumClosest(self, nums, target):
12+
"""
13+
:type nums: List[int]
14+
:type target: int
15+
:rtype: int
16+
"""
17+
minval = 100000
18+
nums.sort()
19+
for i in range(len(nums)):
20+
left = i+1
21+
right = len(nums)-1
22+
while left<right:
23+
val=nums[i]+nums[left]+nums[right]
24+
if abs(val-target) <minval:
25+
minval = abs(val-target)
26+
result = val
27+
if val == target:
28+
return target
29+
if val <= target:
30+
left +=1
31+
else:
32+
right -= 1
33+
return result
34+
35+
36+
if __name__=='__main__':
37+
s = Solution()
38+
print s.threeSumClosest([-1,2,1,-4],1)

‎python/17_LetterCombinationsofaPhoneNumbr.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@
55
# @File : 17_LetterCombinationsofaPhoneNumbr.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
class Solution:
10+
"""
11+
:type digits: str
12+
:rtype: List[str]
13+
"""
14+
def letterCombinations(self, digits):
15+
phonedict = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
16+
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
17+
if len(digits) == 0:
18+
return []
19+
if len(digits) == 1:
20+
return list(phonedict[digits[0]])
21+
prev = self.letterCombinations(digits[:-1])
22+
additional = phonedict[digits[-1]]
23+
return [s + c for s in prev for c in additional]
24+
25+
if __name__ =='__main__':
26+
s = Solution()
27+
print s.letterCombinations("23")

‎python/18_4Sum.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,36 @@
55
# @File : 18_4Sum.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
#two-pointers
11+
class Solution(object):
12+
def fourSum(self, nums, target):
13+
def findNsum(l, r, target, N, result, results):
14+
if r-l+1 < N or N < 2 or target < nums[l]*N or target > nums[r]*N: # early termination
15+
return
16+
if N == 2: # two pointers solve sorted 2-sum problem
17+
while l < r:
18+
s = nums[l] + nums[r]
19+
if s == target:
20+
results.append(result + [nums[l], nums[r]])
21+
l += 1
22+
while l < r and nums[l] == nums[l-1]:
23+
l += 1
24+
elif s < target:
25+
l += 1
26+
else:
27+
r -= 1
28+
else:
29+
for i in range(l, r+1):
30+
if i == l or (i > l and nums[i-1] != nums[i]):
31+
findNsum(i+1, r, target-nums[i], N-1, result+[nums[i]], results)
32+
33+
nums.sort()
34+
results = []
35+
findNsum(0, len(nums)-1, target, 4, [], results)
36+
return results
37+
38+
if __name__=='__main__':
39+
s = Solution()
40+
print s.fourSum([1,0,-1,0,-2,2],0)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /