16、字符串相乘
class Solution: def multiply(self, num1, num2): num1 = num1[::-1] num2 = num2[::-1] length1 = len(num1) length2 = len(num2) temp = [0 for __ in range(length1 + length2)] for i in range(len(num1)): for j in range(len(num2)): temp[i+j] += int(num1[i]) * int(num2[j]) print temp list_ = [] for i in range(len(temp)): digit = temp[i] % 10 carry = temp[i] / 10 if i < len(temp)-1: temp[i+1] += carry list_.insert(0, str(digit)) while list_[0] == '0' and len(list_) > 1: list_.pop(0) return ''.join(list_)
20、螺旋矩阵
class Solution:
def spiralOrder(self, matrix):
res=[]
m=len(matrix)
if m==0:
return res
else:
n=len(matrix[0])
if n==0:
return res
count=(m+1)//2
k=0
su=m*n
cc=0
while k<count and cc<su:
for i in range(k,n-k):
res.append(matrix[k][i])
cc+=1
for i in range(k+1,m-1-k):
res.append(matrix[i][n-1-k])
cc+=1
if k!=m-1-k:
for i in range(k,n-k):
res.append(matrix[m-1-k][n-1-i])
cc+=1
if k!=n-1-k:
for i in range(k+1,m-1-k):
res.append(matrix[m-1-i][k])
cc+=1
k+=1
return res
21、旋转链表
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head:
return None
length = 0
index = head
while index.next:
index = index.next
length += 1
index.next = head
length = length + 1
k = k % length
for i in range(length - k):
head = head.next
index = index.next
index.next = None
return head