Leetcode日常刷题: 数组(1) 二分查找; 快慢指针

Leetcode Q 704 Binary search

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Constraints:

  • 1 <= nums.length <= 104
  • -104 < nums[i], target < 104
  • All the integers in nums are unique.
  • nums is sorted in ascending order.

Key points:

        1. The array is an ordered array

        2. There are no duplicate elements in the array

In this case, we can use the Binary search method to address this question.

Two binary search styles depend on the interval choosing:

        1.[left, right]:

That means we define the target in the full close interval. This situation, we must consider in the while(left <= right), since left == right needs to assign a value.

if (nums[middle] > target) right to assign middle - 1, because the current nums[middle] will not be target, then the next to find the left interval end subscript position is middle - 1.

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)-1

        while left <= right :
            mid = (left + right)//2
            if nums[mid] > target:
                right = mid -1
            elif nums[mid] < target:
                left = mid +1
            else:
                return mid
        return -1
        

         2.[left, right):

In the loop while (left < right), here use < , because left == right in the interval [left, right) is not meaningful
if (nums[middle] > target) right update to the middle, because the current nums[middle] is not equal to target, go to the left interval to continue to look for, and looking for the interval is left closed right open interval, so right update to the middle, that is: the next query interval will not go to compare nums[middle]

Time: O(logN)

Space: O(n)

Leetcode Q 27 Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Key points:

       1. Two pointers Approach: Fast pointer and Slow pointer

First, the Fast pointer is used to explore the target and find the elements of the new array, which does not contain the target elements.

Then the slow pointer follows immediately to update the position of the subindex.

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

        for fast in range(l):
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            else :
                fast += 1


        return slow

Time: O(N)

Space: O(1)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值