关闭

Leetcode 34. Search for a Range

86人阅读 评论(0) 收藏 举报
分类:
/**
 * Two binary search. 
 * When isLeft is true search the left index of the target, 
 * else search the right index of the index. 
 */
public class Solution {
    public static int helper(int[] nums, int target, boolean isLeft) {
        int low = 0, high = nums.length-1, mid = 0;
        while (low <= high) {                                   // note low <= high not low < high cuz three if else 
            mid = (low+high) >> 1;
            if (nums[mid] == target) {
                if (isLeft) {
                    if (mid == 0 || nums[mid] > nums[mid-1])    // be careful about the corner case when using nums[i+1] or nums[i-1]
                        return mid;
                    high = mid - 1;
                }
                else {
                    if (mid == nums.length-1 || nums[mid] < nums[mid+1])
                        return mid;
                    low = mid + 1;
                }
            }
            else if (nums[mid] > target) high = mid - 1;
            else low = mid + 1;
        }
        return -1;
    }
    
    public int[] searchRange(int[] nums, int target) {
        if (nums.length == 1)
            return (nums[0] == target) ? new int[] {0, 0} : new int[] {-1, -1};
        
        if (helper(nums, target, true) == -1)
            return new int[] {-1, -1};
        
        int left = helper(nums, target, true), right = helper(nums, target, false);
        return new int[] {left, right};
    }
}

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

LeetCode Search for a Range搜索特定数值的范围 三种方法求解

在排序数组中搜索一个值有多少个,并返回其两边下标,没有找到就返回[-1,-1]。注意时间效率是O(logN)。这就肯定要用到二分法的思想了。 主要难度是处理好下标的走势。 有三种方法可以求解: ...
  • kenden23
  • kenden23
  • 2013-12-03 08:22
  • 1172

Search for a Range 有序数组里查找一个数的出现区间 @LeetCode

经典多次二分法! package Level4; import java.util.Arrays; /** * Search for a Range * * Given a sort...
  • hellobinfeng
  • hellobinfeng
  • 2013-11-10 05:59
  • 3314

Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

1.问题描述  写一个高效的算法,从一个m×nm\times n的整数矩阵中查找出给定的值,矩阵具有如下特点:    每一行从左到右递增。 每一列从上到下递增。 2. 方法与思路2.1 二分查找法 ...
  • Jeanphorn
  • Jeanphorn
  • 2015-07-23 20:41
  • 3084

[LeetCode-34] Search for a Range (寻找有序数组中关键值的索引范围)

系统输入参数必须要做判断 输入数组的长度和自己求解出来数组的长度不一致 int numslen= sizeof(nums)/sizeof(int); numsize !=numslen; /*这里特...
  • xy010902100449
  • xy010902100449
  • 2015-09-30 11:27
  • 438

LeetCode OJ-34-Search for a Range

题目:Given a sorted array of integers, find the starting and ending position of a given target value. ...
  • dongtaizl
  • dongtaizl
  • 2016-09-30 11:15
  • 75

leetcode(34). Search for a Range

problem Given an array of integers sorted in ascending order, find the starting and ending posi...
  • PKU_Jade
  • PKU_Jade
  • 2017-07-26 21:29
  • 121

LeetCode OJ-34.Search for a Range

LeetCode OJ-34.Search for a Range题目描述 Given an array of integers sorted in ascending order, find t...
  • oYinGui1
  • oYinGui1
  • 2017-07-16 22:04
  • 145

leetcode 34. Search for a Range

import java.util.Stack; //Given a sorted array of integers, find the starting and ending position o...
  • u011438605
  • u011438605
  • 2016-06-15 10:49
  • 159

leetcode 34. Search for a Range-数组边界|二分查找

Given a sorted array of integers, find the starting and ending position of a given target value. Yo...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-10 14:04
  • 1941

LeetCode Algorithms 34. Search for a Range

题目难度: Medium 原题描述: Given an array of integers sorted in ascending order, find the starting...
  • u010985058
  • u010985058
  • 2017-05-06 14:53
  • 92
    个人资料
    • 访问:15980次
    • 积分:1950
    • 等级:
    • 排名:千里之外
    • 原创:183篇
    • 转载:34篇
    • 译文:0篇
    • 评论:0条
    文章分类