LeetCode | 704. Binary Search, 27. Remove Element

704. Binary Search

Link: https://leetcode.com/problems/binary-search/
Reference: https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html

Description

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.

Approach

Binary Search

  • Initialize the left and right pointer, which corresponds to the start and end of
    the pointer.
  • While nums[left] < nums[right]:
    • Calculate the middle index: middle = left + (right - left) / 2. It can avoid the overflow problem.
    • if nums[middle] == target: Return middle.
    • If nums[middle] < target: Set left = middle + 1.
    • If nums[middle] > target: Set right = middle - 1
  • If the target is not found, return -1.

Solutions

class Solution {
    public int search(int[] nums, int target) {
        int right = nums.length - 1;
        int mid = 0;
        int left = 0;
        if (nums[right] < target || nums[left] > target)
            return -1;
        while (left <= right) {
        	//(left + right) / 2 can cause overflow problem
            mid = left + (right - left) / 2;  
            if (nums[mid] == target)
                return mid;
            else if (nums[mid] < target)
                left = mid + 1;
            else
                right = mid - 1;
        }
        return -1;
    }
}
class Solution {
    public int search(int[] nums, int target) {
        int right = nums.length;
        int mid = 0;
        int left = 0;
        if (nums[right - 1] < target || nums[left] > target)
            return -1;
        while (left < right) {
            mid = left + (right - left) / 2; 
            System.out.println(mid);
            if (nums[mid] == target)
                return mid;
            else if (nums[mid] < target)
                left = mid + 1;
            else
                right = mid;
        }
        return -1;
    }
}

Remark

Be careful of the boundary.

27. Remove Element

Link: https://leetcode.com/problems/remove-element/
Reference: https://programmercarl.com/0027.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.html

Description

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain
    the elements which are not equal to val. The remaining elements of
    nums are not important as well as the size of nums.
  • Return k.

Approach

Two pointers

  • Initialize a fast pointer and a slow pointer. The fast pointer is used to find element does not equal to k. The slow pointer is used to update nums.
  • Iterate over nums, check whether nums[fast] = val
    • If nums[fast] != val, set nums[slow] = nums[fast]
    • If nums[fast] == val, skip this element
    • slow++, point slow to the next update position
  • Return slow, which represents the length of the non-target array.

Solutions

class Solution {
    public int removeElement(int[] nums, int val) {
        int fast = 0;
        int slow = 0;
        for (fast = 0; fast < nums.length; fast++) {
            if (nums[fast] != val)
                nums[slow++] = nums[fast];
        }
        return slow;
    }
}
class Solution {
    public int removeElement(int[] nums, int val) {
        int right = nums.length - 1;
        int left = 0;
        while (left <= right) {
            while (left <= right && nums[left] != val)
                left++;
            while (right >= left && nums[right] == val)
                right--;
            if (left < right)
                nums[left++] = nums[right--];
        }
        return left;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值