Day 1- Leetcode 704二分查找binary search | Leetcode 27移除元素

文章介绍了如何使用二分查找法解决升序数组中目标值的搜索问题,并提供了查找左边界和右边界的模板代码。此外,还讨论了在LeetCode的另一问题中,如何利用双指针策略有效地移除数组中的特定元素,包括两种不同的双指针方法。所有解决方案都注重保持O(logn)的时间复杂度。
摘要由CSDN通过智能技术生成

二分查找

二分可以分为查找左边界&查找右边界
对应模板为:

  • 左边界
//search左边界
int searchLeft (int l, int r) {
    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid))
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
  • 右边界
int searchRight (int l, int r) {
	while (l < r) {
		int mid = l + r + 1 >> 1;
		if (check(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}

leetcode 704

题目

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.

找升序数组中的某个元素

class Solution {
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (nums[mid] >= target)
                r = mid;
            else
                l = mid + 1;
        }
        if (nums[l] != target)
            return -1;
        else
            return l;
    }
}

双指针

leetcode27 移除元素

题目

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.

思路:

  • two pointers
    两个指针p, q;只需要遍历一遍就可以;
    note: 题目最后要返回的是修改后的数组长度,而非Index
    assert k == expectedNums.length;
    通过&引用来读取新的array

  • 双向双指针
    思路有点类似快排
    找到 右边!=val 的和 左边==val 的进行交换
    这样移动次数最少
    要注意边界情况

感觉有了思路后,代码实现很简单

思路1:快慢pointer

class Solution {
    public int removeElement(int[] nums, int val) {
        int p = 0, q = 0;
        for (p = 0; p < nums.length; ++p) {
            if (nums[p] != val) {
                nums[q++] = nums[p];
            }
        }
        return q;
    }
}

思路2:双向指针

class Solution {
    public int removeElement(int[] nums, int val) {
        int p = 0, q = nums.length - 1;
        //move q to the place where the 1st value from the right is not val
        while (q >= 0 && nums[q] == val)
            --q;
        
        while (p <= q) {
            if (nums[p] == val) { //replace the value in p with nums[q]
                nums[p] = nums[q--];
            }
            ++p;
            while (q >= 0 && nums[q] == val)
                --q;
        }
        return p;
    }
}

ps: 要尝试给代码写注释

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值