DAY1 - 数组理论基础,704. 二分查找,27. 移除元素

Binary Search (Leetcode 704):

To solve the binary search problem, we cut a sorted array into halves recursively thus achevie a time complexity of N(nlog(n)).

left closed right closed

class Solution:

    def search(self, nums: List[int], target: int) -> int:

        start = 0
        end = len(nums) - 1

        while(start<=end):
            mid = (start + end )//2
            if nums[mid] == target:
                return mid
            
            if nums[mid] < target:
                start = mid +1
            if nums[mid] > target:
                end = mid - 1
                
            

        return -1

left closed right open

in this case, the last index is a phamton, when update end point, use mid instead of mid -1, because the value is at end -1.

class Solution:

    def search(self, nums: List[int], target: int) -> int:

        start, end = 0, len(nums)

        while(start < end):
            mid = start + (end - start)//2
            if nums[mid] == target:
                return mid
            if nums[mid] < target:
                start = mid + 1
            if nums[mid] > target:
                end = mid
        return -1 

Remove Element (LeetCode 27)

The brutal way to solve the problem will be implementing two iterations. The first one find the target value, and the second iteration move all of the rest elements forward by one. This will give a time complexity of N(n^2)

to implement a sliding window, we need to have two pointers. The fast pointer iterate trough the the array, the slow pointer is updated only when the fast pointer is not at the target value. in such a case the slow pointer will be assigned a value from the fatser pointer. if the fast pointer is at the target value. then the slow pointer doesn't update. This will give a time complexity of N(n)

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        fast = 0
        slow = 0
        length = len(nums)

        while fast<length:
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1

        return slow

Another way to solve this problem is iterate through the list. if find val, replace the last element with val and shrink the valid array size by one. (mark off the last elements so no repeat). We can solve the problem this way only because the problem definition only asking for the length of the the valid array and assume that we can swap the elements orders.

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        size = len(nums)
        i = 0
        while(i < size):
            if nums[i] == val:
                nums[i] = nums[size-1]
                size = size -1
            else:
                i = i + 1
        return size

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值