融合有序数组or链表的一个思路
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
p1 = m
p2 = n
for i in range(m + n - len(nums1)):
nums1[i + m + n] = 0
while p1 > 0 or p2 > 0:
if p1 == 0:
nums1[p2+p1-1] = nums2[p2-1]
p2 -= 1
elif p2 == 0:
nums1[p2+p1-1] = nums1[p1-1]
p1 -= 1
elif nums1[p1-1] > nums2[p2-1]:
nums1[p2+p1-1] = nums1[p1-1]
p1 -= 1
elif nums1[p1-1] <= nums2[p2-1]:
nums1[p2+p1-1] = nums2[p2-1]
p2 -= 1
对角线遍历一个答案,思路非常清晰,有一个小细节可以排除副对角线超出范围问题
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == []:
return []
m, n = len(matrix), len(matrix[0])
a = 0
b = 0
i = 0
result = []
for j in range(m*n):
result.append(matrix[a][b])
if i % 2 == 0:
#先检测b再检测a,可以排除右上角超出范围问题,左下角同理
if b == n-1:
i += 1
a += 1
continue
elif a == 0:
i += 1
b += 1
continue
else:
a -= 1
b += 1
continue
else:
if a == m-1:
i += 1
b += 1
continue
elif b == 0:
i += 1
a += 1
continue
else:
a += 1
b -= 1
continue
return result
对角线遍历,利用对称性可以减少方阵的一半运算量(m*n/2),但是非方阵还是要m*n
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
side = True
x = len(matrix)
if x:
y = len(matrix[0])
else:
y = 0
if x == 1:
p = matrix[0]
return p
if y == 1:
p = zip(*matrix)[0]
return p
a, b = 0, 0
p = [0] * (x * y)
if (x == y):
if int(x * y / 2) * 2 < x * y:
p[int(x * y / 2)] = matrix[int(x / 2)][int(y / 2)]
for i in range(int(x * y / 2)):
p[i] = matrix[a][b]
p[x * y - i - 1] = matrix[x - 1 - a][y - 1 - b]
if side == True:
a, b = a - 1, b + 1
if a < 0 or b >= y:
a = a + 1
side = False
else:
a, b = a + 1, b - 1
if b < 0 or a >= x:
b = b + 1
side = True
return p
else:
for i in range(x * y):
p[i] = matrix[a][b]
if side == True:
a, b = a - 1, b + 1
if a < 0 or b >= y:
a = a + 1
if b >= y:
a, b = a + 1, b - 1
side = False
else:
a, b = a + 1, b - 1
if b < 0 or a >= x:
b = b + 1
if a >= x:
a, b = a - 1, b + 1
side = True
return p
打印螺旋数组的答案一,三种答案中私以为最好的,思路非常清晰
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix or not matrix[0]:
return []
m = len(matrix)
n = len(matrix[0])
directions = [[0,1],[1,0],[0,-1],[-1,0]]
canVisit = [[True for _ in range(n)] for _ in range(m)]
ans = []
x=y=d=0
while len(ans) < m*n:
ans.append(matrix[x][y])
canVisit[x][y] = False
nextX, nextY = x+directions[d][0], y+directions[d][1]
if 0<=nextX<m and 0<=nextY<n and canVisit[nextX][nextY]:
x, y = nextX, nextY
else :
d = (d+1) % 4
x+=directions[d][0]
y+=directions[d][1]
return ans
打印螺旋数组答案二
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if len(matrix)==0 or len(matrix[0])==0:
return[]
ret=[]
left,right,up,down=0,len(matrix[0])-1,0,len(matrix)-1
while left<=right and up<=down:
for i in range(left,right+1):
ret.append(matrix[up][i])
up+=1
for i in range(up,down+1):
ret.append(matrix[i][right])
right-=1
for i in reversed(range(left,right+1)):
ret.append(matrix[down][i])
down-=1
for i in reversed(range(up,down+1)):
ret.append(matrix[i][left])
left+=1
return ret[:(len(matrix) * len(matrix[0]))]
打印螺旋数组答案三
def spiralOrder(self, matrix):
def cyc(row, column, ri, ci, case):
if row == 0 or column == 0:
return
if case == 0:
endci = ci + column
for i in xrange(ci, endci, 1):
re.append(matrix[ri][i])
row = row - 1;ri = ri + 1;ci = endci -1
case = (case+1)%4
cyc(row, column, ri, ci ,case)
elif case == 1:
endri = ri + row
for i in xrange(ri, endri, 1):
re.append(matrix[i][ci])
column = column - 1; ri = endri -1; ci = ci -1
case = (case+1)%4
cyc(row, column, ri, ci, case)
elif case == 2:
for i in xrange(ci, ci - column, -1):
re.append(matrix[ri][i])
row = row - 1; ri = ri - 1; ci = ci - column + 1
case = (case + 1)%4
cyc(row, column, ri, ci, case)
elif case == 3:
for i in xrange(ri, ri -row, -1):
re.append(matrix[i][ci])
column = column - 1; ri = ri - row + 1; ci = ci + 1
case = (case + 1)%4
cyc(row, column, ri, ci, case)
row = len(matrix)
if row == 0: return []
column = len(matrix[0])
re = []
cyc(row, column, 0, 0, 0)
return re
两数之和II-输入有序数组,一种O(n)算法,且不需要要求输入有序数组即可达到求解的目的
class Solution:
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
if len(numbers) ==1 :return None
d = dict()
for k in range(len(numbers)):
r = target - numbers[k]
if r not in d:
d[numbers[k]] = k
else:
return [d[r]+1,k+1]