[60天lc] Day1: 704,27

704 二分查找

基础,属于复习啦

左闭右闭

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        # 左闭右闭
        left, right = 0, len(nums)-1
        while left <= right:
            mid = (left+right)//2
            if nums[mid] == target:
                return mid 
            elif nums[mid] > target:
                right = mid - 1
            else:
                left = mid + 1
        
        return -1

左闭右开

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        # 左闭右开
        left, right = 0, len(nums)
        
        while left < right:
            mid = (left + right) //2 
            if nums[mid] == target:
                return mid 
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid 

        return -1

 27 移除元素

第一次尝试——失败:

  • 原地修改数组——>使用双指针
  • 不在意顺序+新数组集中在数组前排位置——>交换不符合要求的到数组靠后位置,两个指针相向而行。
class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        if not nums or len(nums) == 0:
            return 0 

        left, right = 0, len(nums) - 1
        while nums[right] == val and right >= 0:
            right -= 1
        while left < right:
            if nums[left] == val:
                nums[left], nums[right] = nums[right], nums[left]
                right -= 1
            left += 1
        
        return right + 1

存在问题:

        如果需要排除的数字一开始就集中在前面,容易漏掉需要排除的数字。 

        e.g. nums =[2,2,3], val = 2 。正确输出:[3], 我的输出:[3,2]

第二次尝试——成功:

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        if not nums or len(nums) == 0:
            return 0 

        left, right = 0, len(nums) - 1
        while nums[right] == val and right >= 0:
            right -= 1

        while left <= right:
            if nums[right] == val:
                right -= 1
                continue
            if nums[left] == val:
                nums[left], nums[right] = nums[right], nums[left]
                right -= 1
            left += 1
        
        return right + 1

 

普遍解法——快慢指针:

保原来数组顺序,更好理解,写法更简洁:

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        if not nums or len(nums) == 0:
            return 0 

        slow = 0
        for fast in range(len(nums)):
            if val != nums[fast]:
                nums[slow] = nums[fast]
                slow += 1
            
        return slow

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值