Skip to content

Commit e4b98cf

Browse files
committed
add some code
1 parent ffbddd3 commit e4b98cf

11 files changed

+278
-51
lines changed

python/34_FindFirstandLastPositionofElementinSortedArray.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,32 @@
77
# @E-mail : charlotte77_hu@sina.com
88

99

10-
class Solution(object):
10+
class Solution:
1111

12+
def searchRange(self, nums, target):
13+
left_idx = self.extreme_insertion_index(nums, target, True)
1214

15+
if left_idx == len(nums) or nums[left_idx] != target:
16+
return [-1, -1]
1317

18+
return [left_idx, self.extreme_insertion_index(nums, target, False)-1]
1419

1520

21+
def extreme_insertion_index(self, nums, target, left):
22+
lo = 0
23+
hi = len(nums)
1624

25+
while lo < hi:
26+
mid = (lo + hi) // 2
27+
if nums[mid] > target or (left and target == nums[mid]):
28+
hi = mid
29+
else:
30+
lo = mid+1
31+
32+
return lo
1733

1834

1935

2036
if __name__=='__main__':
2137
s = Solution()
22-
print
38+
print s.searchRange([5,7,7,8,8,10],8)

python/35_SearchInsertPosition.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@
88

99

1010
class Solution(object):
11-
12-
13-
14-
15-
16-
17-
11+
def searchInsert(self, nums, target):
12+
"""
13+
:type nums: List[int]
14+
:type target: int
15+
:rtype: int
16+
"""
17+
low, high = 0, len(nums)
18+
while low < high:
19+
mid = (low+high)/2
20+
if nums[mid] >= target:
21+
high = mid
22+
else:
23+
low = mid + 1
24+
return low
1825

1926

2027
if __name__=='__main__':
2128
s = Solution()
22-
print
29+
print s.searchInsert([1,3,5,6],5)

python/36_ValidSudoku.py

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,71 @@
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
88

9+
class Solution1(object):
10+
def isValidSudoku(self, board):
11+
"""
12+
:type board: List[List[str]]
13+
:rtype: bool
14+
"""
15+
## 方法一:按照三个规则全部检查一遍
16+
for i in range(9):
17+
if not self.isValidNine(board[i]):
18+
return False
19+
col = [c[i] for c in board]
20+
if not self.isValidNine(col):
21+
return False
922

10-
class Solution(object):
23+
for i in [0,3,6]:
24+
for j in [0,3,6]:
25+
block = [board[s][t] for s in [i,i+1,i+2] for t in [j,j+1,j+2]]
26+
if not self.isValidNine(block):
27+
return False
28+
return True
1129

30+
def isValidNine(self,row):
31+
map = {}
32+
for c in row:
33+
if c!= '.':
34+
if c in map:
35+
return False
36+
else:
37+
map[c] = True
38+
return True
1239

40+
# 方法二:用三个矩阵来分别检查三个规则是否有重复数字,譬如row、col、block分别检查行、列、块是否有重复数字
41+
class Solution2(object):
42+
def isValidSudoku(self,board):
43+
row = [[False for i in range(9)] for j in range(9)]
44+
col = [[False for i in range(9)] for j in range(9)]
45+
block = [[False for i in range(9)] for j in range(9)]
46+
for i in range(9):
47+
for j in range(9):
48+
if board[i][j] != '.':
49+
num = int(board[i][j])-1
50+
k=i/3*3 +j/3
51+
if row[i][num] or col[j][num] or block[k][num]:
52+
return False
53+
row[i][num] = col[j][num] =block[k][num] =True
54+
return True
1355

1456

57+
# 方法三:记录所有出现过的行、列、块的数字及相应的位置,最后判断是否有重复。用set操作精简代码
58+
class Solution3(object):
59+
def isValidSudoku(self,board):
60+
seen=[]
61+
for i,row in enumerate(board):
62+
for j,c in enumerate(row):
63+
# print j,c
64+
if c !='.':
65+
seen += [(c,j),(i,c),(i/3,j/3,c)]
66+
# print seen
67+
return len(seen) == len(set(seen))
68+
1569

1670

1771

1872

1973

2074
if __name__=='__main__':
21-
s = Solution()
22-
print
75+
s = Solution3()
76+
print s.isValidSudoku([["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]])

python/37_SodukoSolver.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,60 @@
77
# @E-mail : charlotte77_hu@sina.com
88

99

10-
class Solution(object):
10+
class Solution:
1111

12+
def solveSudoku(self, board):
13+
self.board = board
14+
self.solve()
1215

16+
def findUnassigned(self):
17+
for row in range(9):
18+
for col in range(9):
19+
if self.board[row][col] == ".":
20+
return row, col
21+
return -1, -1
1322

23+
def solve(self):
24+
row, col = self.findUnassigned()
25+
if row == -1 and col == -1:
26+
return True
27+
for num in ["1","2","3","4","5","6","7","8","9"]:
28+
if self.isSafe(row, col, num):
29+
self.board[row][col] = num
30+
if self.solve():
31+
return True
32+
self.board[row][col] = "."
33+
return False
1434

35+
def isSafe(self, row, col, ch):
36+
boxrow = row - row%3
37+
boxcol = col - col%3
38+
if self.checkrow(row,ch) and self.checkcol(col,ch) and self.checksquare(boxrow, boxcol, ch):
39+
return True
40+
return False
1541

42+
def checkrow(self, row, ch):
43+
for col in range(9):
44+
if self.board[row][col] == ch:
45+
return False
46+
return True
47+
48+
def checkcol(self, col, ch):
49+
for row in range(9):
50+
if self.board[row][col] == ch:
51+
return False
52+
return True
53+
54+
def checksquare(self, row, col, ch):
55+
for r in range(row, row+3):
56+
for c in range(col, col+3):
57+
if self.board[r][c] == ch:
58+
return False
59+
return True
1660

1761

1862

1963

2064
if __name__=='__main__':
2165
s = Solution()
22-
print
66+
print s.solveSudoku([["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]])

python/38_CountandSay.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,24 @@
88

99

1010
class Solution(object):
11-
12-
11+
def countAndSay(self, n):
12+
"""
13+
:type n: int
14+
:rtype: str
15+
"""
16+
s = '1'
17+
for _ in range(n-1):
18+
let, temp, count = s[0], '', 0
19+
for l in s:
20+
if let == l:
21+
count += 1
22+
else:
23+
temp += str(count)+let
24+
let = l
25+
count = 1
26+
temp += str(count)+let
27+
s = temp
28+
return s
1329

1430

1531

@@ -19,4 +35,4 @@ class Solution(object):
1935

2036
if __name__=='__main__':
2137
s = Solution()
22-
print
38+
print s.countAndSay(11)

python/39_CombinationSum.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,31 @@
88

99

1010
class Solution(object):
11+
def combinationSum(self, candidates, target):
12+
"""
13+
:type candidates: List[int]
14+
:type target: int
15+
:rtype: List[List[int]]
16+
"""
17+
# 方法一:DFS
18+
self.resList = []
19+
candidates = sorted(candidates)
20+
self.dfs(candidates,[],target,0)
21+
return self.resList
1122

12-
23+
def dfs(self,candidates,sublist,target,last):
24+
if target == 0:
25+
self.resList.append(sublist[:])
26+
if target < candidates[0]:
27+
return
28+
for n in candidates:
29+
if n >target:
30+
return
31+
if n<last:
32+
continue
33+
sublist.append(n)
34+
self.dfs(candidates,sublist,target-n,n)
35+
sublist.pop()
1336

1437

1538

@@ -19,4 +42,4 @@ class Solution(object):
1942

2043
if __name__=='__main__':
2144
s = Solution()
22-
print
45+
print s.combinationSum([2,3,6,7],7)

python/40_CombinationSumII.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,29 @@
88

99

1010
class Solution(object):
11+
def combinationSum2(self, candidates, target):
12+
"""
13+
:type candidates: List[int]
14+
:type target: int
15+
:rtype: List[List[int]]
16+
"""
17+
#方法一:DFS
18+
candidates.sort()
19+
print candidates
20+
res=[]
21+
self.dfs(candidates, target, 0, res, [])
22+
return res
1123

12-
24+
def dfs(self, nums, target,index,res,path):
25+
if target < 0:
26+
return
27+
elif target == 0:
28+
res.append(path)
29+
return
30+
for i in xrange(index, len(nums)):
31+
if i > index and nums[i] == nums[i-1]:
32+
continue
33+
self.dfs(nums,target-nums[i],i+1,res,path+[nums[i]])
1334

1435

1536

@@ -19,4 +40,4 @@ class Solution(object):
1940

2041
if __name__=='__main__':
2142
s = Solution()
22-
print
43+
print s.combinationSum2([10,1,2,7,6,1,5],8)

python/41_FirstMissingPositive.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,28 @@
88

99

1010
class Solution(object):
11+
def firstMissingPositive(self, nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: int
15+
"""
1116

12-
13-
14-
17+
nums.append(0)
18+
n = len(nums)
19+
for i in range(len(nums)):
20+
if nums[i]<0 or nums[i]>=n:
21+
nums[i]=0
22+
for i in range(len(nums)):
23+
nums[nums[i]%n]+=n
24+
for i in range(1,len(nums)):
25+
if nums[i]/n==0:
26+
return i
27+
return n
1528

1629

1730

1831

1932

2033
if __name__=='__main__':
2134
s = Solution()
22-
print
35+
print s.firstMissingPositive([1,2,0])

python/42_TrappingRainWater.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
88

9-
10-
class Solution(object):
11-
12-
13-
14-
15-
16-
9+
class Solution:
10+
# @param {integer[]} height
11+
# @return {integer}
12+
def trap(self, height):
13+
n = len(height)
14+
l, r, water, minHeight = 0, n - 1, 0, 0
15+
while l < r:
16+
while l < r and height[l] <= minHeight:
17+
water += minHeight - height[l]
18+
l += 1
19+
while r > l and height[r] <= minHeight:
20+
water += minHeight - height[r]
21+
r -= 1
22+
minHeight = min(height[l], height[r])
23+
return water
1724

1825

1926

2027
if __name__=='__main__':
2128
s = Solution()
22-
print
29+
print s.trap([0,1,0,2,1,0,1,3,2,1,2,1])

0 commit comments

Comments
 (0)