LeetCode Online Judge
https://leetcode.com/
1. Minimum Path Sum [88ms]
#方法1:dfs,第一想法,注意保护现场,但是超时
#方法2:dp,很简单,但是就是没想到
class Solution(object):
def minPathSum(self, grid):
#注意递归传列表是应用类型,所以能实时更改
#数值类不会修改原值
result = [0, 100001]
self.dfs(0, 0, result, grid)
return result[1]
def dfs(self, i, j, result, grid) :
if i == len(grid)-1 and j == len(grid[0])-1 :
result[0] += grid[i][j]
result[1] = min(result[0], result[1])
result[0] -= grid[i][j] #保护现场
return
for k in range(2) :
result[0] += grid[i][j]
if result[0] >= result[1] :
result[0] -= grid[i][j] #保护现场
return
if k==0 and i+1 < len(grid) :
self.dfs(i+1, j, result, grid)
if k==1 and j+1 < len(grid[0]) :
self.dfs(i, j+1, result, grid)
if k==0 and i+1 == len(grid) :
self.dfs(i, j+1, result, grid)
if k==1 and j+1 == len(grid[0]) :
self.dfs(i+1, j, result, grid)
result[0] -= grid[i][j] #保护现场
class Solution(object):
def minPathSum(self, grid):
for i in range(len(grid)) :
for j in range(len(grid[0])) :
if i == 0 and j == 0 :
continue
if i ==0 :
grid[i][j] = grid[i][j-1] + grid[i][j]
elif j == 0 :
grid[i][j] = grid[i-1][j] + grid[i][j]
else :
grid[i][j] = min(grid[i-1][j], grid[i][j-1]) + grid[i][j]
return grid[-1][-1]
2. Search 2D Martix [44ms]
#方法1:先确定哪一行,再二分
class Solution(object):
def searchMatrix(self, matrix, target):
r = [matrix[i][0] for i in range(len(matrix))]
s = 0
for i in range(len(r)) :
if target >= r[i]:
s = i
low, high = 0, len(matrix[0])-1
while low <= high :
mid = int((low + high) / 2)
if matrix[s][mid] == target :
return True
if target > matrix[s][mid] :
low = mid + 1
else :
high = mid - 1
return False
3. Set Matrix Zero [180ms]
#方法1:如果是mn,直接另开数组,m+n则是记录位置
#常数的方法就是利用第一行
#先检查第一行有没有0
#然后再在中间的矩阵找0,找到0则在第一行第一列标记
#最后再处理回第一行第一列
class Solution(object):
def setZeroes(self, matrix):
first_row, first_col = False, False
r, c = len(matrix), len(matrix[0])
for i in range(r) :
if matrix[i][0] == 0 :
first_col = True
for i in range(c) :
if matrix[0][i] == 0 :
first_row = True
for i in range(1, r) :
for j in range(1, c) :
if matrix[i][j] == 0 :
matrix[i][0] = 0
matrix[0][j] = 0
for i in range(1, r) :
for j in range(1, c) :
if matrix[i][0] == 0 or matrix[0][j] == 0 :
matrix[i][j] = 0
for i in range(r) :
if first_col :
matrix[i][0] = 0
for i in range(c) :
if first_row :
matrix[0][i] =0
4. Sort Color [72ms]
#方法1:双指针遍历,遇到0交换start,遇到1跳过,遇到2交换end [72ms]
#需要注意交换end后还要再判断一次,因为不知道是0还是1
class Solution(object):
def sortColors(self, nums):
start, end = 0, len(nums) - 1
i = 0
while i <= end :
if nums[i] == 0 :
nums[start], nums[i] = nums[i], nums[start]
i += 1
start += 1
elif nums[i] == 1 :
i += 1
else :
nums[end], nums[i] = nums[i], nums[end]
end -= 1
5. Spiral Matrix II [56ms]
#方法一:注意每次向前进时到倒数第二格就停下,最后再补上最后一个元素
class Solution(object):
def generateMatrix(self, n):
matrix = [ [0 for i in range(n)] for i in range(n) ]
start, end = 0, n-1
num = 1
while start < end :
for j in range(start, end) :
matrix[start][j] = num
num += 1
for i in range(start, end) :
matrix[i][end] = num
num += 1
for j in range(end, start, -1) :
matrix[end][j] = num
num += 1
for i in range(end, start, -1) :
matrix[i][start] = num
num += 1
start += 1
end -= 1
if start == end :
matrix[start][end] = num
return matrix
6. Unique Paths [48ms]
#思路:选择排列问题,设 m*n的矩阵,路径长度有 m+n,
#就是在 m+n里选择 m个为向下,其余向右,C[m+n, m]
#方法一:dp,每个格子有上面和左面的格子值相加[48ms]
#优化一:dp,只是优化储存空间[56ms]
class Solution(object):
def uniquePaths(self, m, n):
dp = [[1 for j in range(n)] for i in range(m) ]
for i in range(1, m) :
for j in range(1, n) :
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[-1][-1]
class Solution(object):
def uniquePaths(self, m, n):
dp = [1 for i in range(m)]
for i in range(1, n) :
for j in range(1,m) :
dp[j] += dp[j-1]
return dp[-1]
7. Unique Paths II [56ms]
#方法1:跟之前的题目类似,就是需要多点判断
#用了python版的三目运算
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
row, col = len(obstacleGrid), len(obstacleGrid[0])
dp = [ [1 for i in range(col)] for j in range(row)]
dp[0][0] = 1 if obstacleGrid[0][0] != 1 else 0
for i in range(1, col) :
dp[0][i] = dp[0][i-1] if obstacleGrid[0][i] != 1 else 0
for i in range(1, row) :
dp[i][0] = dp[i-1][0] if obstacleGrid[i][0] != 1 else 0
for i in range(1, row) :
for j in range(1, col) :
dp[i][j] = dp[i-1][j] + dp[i][j-1] if obstacleGrid[i][j] != 1 else 0
return dp[-1][-1]