资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Search for a Range [左右边界法/一次循环

zhangrxiang / 639人阅读

摘要:首先,建立二元结果数组,起点,终点。二分法求左边界当中点小于,移向中点,否则移向中点先判断起点,再判断终点是否等于,如果是,赋值给。

Problem

Given a sorted array of n integers, find the starting and ending position of a given target value.

If the target is not found in the array, return [-1, -1].

Example

Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

Challenge

O(log n) time.

Note

首先,建立二元结果数组res,起点start,终点end。
二分法求左边界:
当中点小于target,start移向中点,否则end移向中点;
先判断起点,再判断终点是否等于target,如果是,赋值给res[0]。
二分法求右边界:
当中点大于target,end移向中点,否则start移向中点;
先判断终点,再判断起点是否等于target,如果是,赋值给res[1]。
返回res。

Solution
public class Solution {
    public int[] searchRange(int[] A, int target) {
        int []res = {-1, -1};
        if (A == null || A.length == 0) return res;
        int start = 0, end = A.length - 1;
        int mid;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] < target) start = mid;
            else end = mid;
        }
        if (A[start] == target) res[0] = start;
        else if (A[end] == target) res[0] = end;
        else return res;
        start = 0;
        end = A.length - 1;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] > target) end = mid;
            else start = mid;
        }
        if (A[end] == target) res[1] = end;
        else if (A[start] == target) res[1] = start;
        else return res;
        return res;
    }
}
Another Binary Search Method
public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int n = nums.length;
        int[] res = {-1, -1};
        int start = 0, end = n-1;
        while (nums[start] < nums[end]) {
            int mid = start + (end - start) / 2;
            if (nums[mid] > target) end = mid - 1;
            else if (nums[mid] < target) start = mid + 1;
            else {
                if (nums[start] < target) start++;
                if (nums[end] > target) end--;
            }
        }
        if (nums[start] == target) {
            res[0] = start;
            res[1] = end;
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65938.html

相关文章

  • [LintCode/LeetCode] Rotate Image

    摘要:两种方法,转置镜像法和公式法。首先看转置镜像法原矩阵为转置后水平镜像翻转后所以,基本的思路是两次遍历,第一次转置,第二次水平镜像翻转变换列坐标。公式法是应用了一个翻转的公式如此翻转四次即可。二者均可,并无分别。 Problem You are given an n x n 2D matrix representing an image.Rotate the image by 90 de...

    BenCHou 评论0 收藏0
  • [LintCode/LeetCode] Search in Rotated Sorted Arra

    摘要:找中点若起点小于中点,说明左半段没有旋转,否则说明右半段没有旋转。在左右半段分别进行二分法的操作。只判断有无,就容易了。还是用二分法优化 Search in Rotated Sorted Array Problem Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 ...

    U2FsdGVkX1x 评论0 收藏0
  • [LintCode/LeetCode] Implement Trie

    摘要:首先,我们应该了解字典树的性质和结构,就会很容易实现要求的三个相似的功能插入,查找,前缀查找。既然叫做字典树,它一定具有顺序存放个字母的性质。所以,在字典树的里面,添加,和三个参数。 Problem Implement a trie with insert, search, and startsWith methods. Notice You may assume that all i...

    付永刚 评论0 收藏0
  • [LintCode/LeetCode] Lowest Common Ancestor of BST/

    摘要:递归左右子树,若左右子树都有解,那么返回根节点若仅左子树有解,返回左子树若仅右子树有解,返回右子树若都无解,返回。对于而言,更为简单公共祖先一定是大于等于其中一个结点,小于等于另一个结点。 Problem Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the ...

    dinfer 评论0 收藏0
  • [LintCode/LeetCode] Candy

    摘要:保证高的小朋友拿到的糖果更多,我们建立一个分糖果数组。首先,分析边界条件如果没有小朋友,或者只有一个小朋友,分别对应没有糖果,和有一个糖果。排排坐,吃果果。先往右,再往左。右边高,多一个。总和加上小朋友总数,就是要准备糖果的总数啦。 Problem There are N children standing in a line. Each child is assigned a rat...

    baishancloud 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<