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