leetcode刷題第13天

leetcode刷題第13天


1.第一題:
在這裏插入圖片描述
這一道題使用暴力解法,我覺得的暴力哈,當ii<=x and (i+1)(i+1)>x時,返回i就是答案,我是直接遍歷,但我看了題解其實可以用二分查找,效率更高,但是我懶得寫了hhh,我的代碼如下:

class Solution:    
	def mySqrt(self, x: int) -> int:        
		if x==0 or x==1:            
			return x        
		n=x//2        
		for i in range(1,n+1):            
			if i*i<=x and (i+1)*(i+1)>x:                
				return i                
				break            
			else:                
				continue 

2.第二題:
在這裏插入圖片描述
這道題很簡單,我是用的雙指針,代碼如下:

class Solution:    
	def deleteDuplicates(self, head: ListNode) -> ListNode:        
		if head==None:            
			return None        
		p=head        
		while p.next!=None:            
			s=p.next            
			vals=p.val            
			if s.val==vals:                
				p.next=s.next#刪除重複節點            
			else:                
				p=p.next        
		return head
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章