NC22 合并两个有序的数组
A: [4,5,6,0,0,0],m=3
B: [1,2,3],n=3
合并过后A为:
A: [1,2,3,4,5,6]
偷懒写法:
class Solution:
def merge(self , A, m, B, n):
A[m:] = B
A.sort()
return
标准写法:
class Solution:
def merge(self , A, m, B, n):
while m>0 and n>0:
if A[m-1]>B[n-1]:
A[m+n-1] = A[m-1]
m-=1
else:
A[m+n-1] = B[n-1]
n-=1
if n >0:
A[:n] = B[:n]
return
NC3 链表中环的入口结点
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,返回null。
class Solution:
def EntryNodeOfLoop(self, pHead):
if pHead is None or pHead.next is None:
return None
tem = []
while pHead:
tem.append(pHead)
pHead = pHead.next
if pHead in tem:
return pHead
return None
WC137 单链表的排序
给定一个无序单链表,实现单链表的排序(按升序排序)。
class Solution:
def sortInList(self , head ):
p_head = head
tem_val = []
while head:
tem_val.append(head.val)
head = head.next
tem_val.sort()
new_head = p_head
for i in tem_val:
p_head.val = i
p_head = p_head.next
return new_head
NC52 括号序列
给出一个仅包含字符’(’,’)’,’{’,’}’,’[‘和’]’,的字符串,判断给出的字符串是否是合法的括号序列 括号必须以正确的顺序关闭,"()“和”()[]{}“都是合法的括号序列,但”(]“和”([)]"不合法。
class Solution:
def isValid(self , s ):
dic = {'(':')','{' :"}",'[':']'}
first = []
for ss in s:
if ss in dic.keys():
first.append(ss)
if ss in dic.values():
if len(first) == 0:
return False
tem = first.pop()
if ss == dic[tem]:
continue
else:
return False
return True if len(first)==0 else False
NC53 删除链表的倒数第n个节点
思路:先求长度,再遍历到倒数n+1个节点处,删除它的下一个节点
class Solution:
def removeNthFromEnd(self , head , n ):
if head is None or n ==0:
return head
if head.next is None :
if n == 1:
return None
if n==0:
return head
head_r = head
l = 0
while head:
l+=1
head = head.next
head = head_r
if l==n:
return head_r.next
for i in range(l-n-1):
head = head.next
head.next = head.next.next
return head_r
NC1 大数加法
class Solution:
def solve(self , s , t ):
full = 0
length_max = max(len(s),len(t))
s = list(s)
t = list(t)
result = ''
for i in range(length_max):
if len(s)>0:
x1 = int(s.pop())
else:
x1 = 0
if len(t)>0:
x2 = int(t.pop())
else:
x2 = 0
tem = x1+x2+full
if tem >=10:
full =1
result += (str(tem-10))
else:
full = 0
result += (str(tem))
if full == 1:
result += '1'
return result[::-1]
NC14 按之字形顺序打印二叉树
给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)
思路:采用层序遍历,并设置指示变量,每隔一层,反向
class Solution:
def Print(self, pRoot):
if pRoot is None:
return []
if pRoot.left is None and pRoot.right is None:
return [[pRoot.val]]
result = []
tem = [pRoot]
point = True
while len(tem)>0:
if point:
tem_val = [x.val for x in tem]
result.append(tem_val)
tem2 = []
for node in tem:
if node.left:
tem2.append(node.left)
if node.right:
tem2.append(node.right)
point = False
tem = tem2
else:
tem_val = [x.val for x in tem[::-1]]
result.append(tem_val)
tem2 = []
for node in tem:
if node.left:
tem2.append(node.left)
if node.right:
tem2.append(node.right)
point = True
tem = tem2
return result
NC127 最长公共子串
输入:“1AB2345CD”,“12345EF”
输出:“2345”
思路:动态规划,在长字符串中,设置移动窗口,若遇到相同子串,最长长度+1,滑动窗口左边不动,右边长度+1,否则滑动窗口长度不变,同时向右移动1位
设置变量maxlen = 0 来记录当前最长子串的长度;用i来遍历较长字符串,i-maxlen表示滑动窗口左侧,i+1表示滑动窗口右侧
class Solution:
def LCS(self,str1 , str2 ):
if len(str1) < len(str2):
str1, str2 = str2, str1
res = ''
max_len = 0
for i in range(len(str1)):
if str1[i - max_len : i+1] in str2:
res = str1[i - max_len : i+1]
max_len += 1
return res
NC38 螺旋矩阵
输入:
[[1,2,3],[4,5,6],[7,8,9]]
返回值:
[1,2,3,6,9,8,7,4,5]
class Solution:
def spiralOrder(self , matrix ):
if len(matrix)==0:
return matrix
m = 0
n = len(matrix)-1
a = 0
b = len(matrix[0])-1
result = []
if n ==0 and b==0:
return matrix[0]
elif n==0:
return matrix[0]
elif b==0:
return [x[0] for x in matrix]
while m<n and a < b:
result.extend(self.circle(matrix, m, n, a, b))
a+=1
m+=1
n-=1
b-=1
if m==n and a==b:
result.append(matrix[m][a])
return result
elif m>n and a>b:
pass
elif m==n:
result.extend(matrix[m][a:b+1])
elif a==b:
result.extend([x[a] for x in matrix[m:n+1]])
return result
def circle(self,matrix,m,n,a,b):
'''
读取m,n行与a,b列交叉形成的矩形边的值
m<n,a<b
'''
tem = []
tem.extend(matrix[m][a:b+1])
for i in range(m+1,n):
tem.append(matrix[i][b])
tem.extend(reversed(matrix[n][a:b+1]))
for j in range(n-1,m,-1):
tem.append(matrix[j][a])
return tem
大佬解法:
zip实现行列变换,每次取完第一行,列变成行,然后逆序,就相当于长方形削掉最上面一层,然后向左旋转90度
class Solution:
def spiralOrder(self , matrix ):
res = []
while matrix:
res += matrix[0]
matrix = list(zip(*matrix[1:]))[::-1]
return res
|