leetcode第14天

leetcode刷题第14天


1.第一题:
在这里插入图片描述今天很烦躁,只是一个代码的搬运工:

def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:        
	"""        
	Do not return anything, modify nums1 in-place instead.        
	"""        
	p1=m-1        
	p2=n-1        
	while p1>=0 and p2>=0:            
		if nums1[p1]>nums2[p2]:                
			nums1[p1+p2+1]=nums1[p1]                
			p1-=1            
		else:                
			nums1[p1+p2+1]=nums2[p2]                
			p2-=1        
	if p2>0:            
		nums1[:p1+p2+2]=nums2[:p2+1]        
	if p2==0:            
		nums1[0]=nums2[p2]

2.第二题:
在这里插入图片描述
用递归,代码:

class Solution:    
	def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:        
		if p == None and q == None:            
			return True        
		if p == None or q == None:            
			return False        
		if p.val != q.val:            
			return False        
		return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章