###### Search for a range
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] result = new int[2];
result[0] = -1;
result[1] = -1;
if (nums == null || nums.length < 1) {
return result;
}
int left = 0;
int right = 0;
//search for left bound.
right = nums.length - 1;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid;
}
}
if (nums[left] == target) {
result[0] = left;
} else if (nums[right] == target) {
result[0] = right;
} else {
result[0] = -1;
result[1] = -1;
return result;
}
//search for right bound.
left = result[0];
right = nums.length - 1;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target) {
right = mid;
} else {
left = mid;
}
}
if (nums[right] == target) {
result[1] = right;
} else {
result[1] = left;
}
return result;
}
}

#### LeetCode解题报告--Search for a Range

2015-12-04 13:32:03

#### Search for a Range -- LeetCode

2014-03-06 03:48:49

#### LeetCode 34 — Search for a Range（C++ Java Python）

2014-04-03 20:53:45

#### LeetCode 34 Search for a Range (C,C++,Java,Python)

2015-05-14 21:28:11

#### leetcode 34 -- Search for a Range

2015-06-23 00:36:45

#### [C语言][LeetCode][34]Search for a Range

2015-08-28 23:10:42

#### [LeetCode34]Search for a Range

2014-06-16 01:31:31

#### leetcode_c++：Search for a Range（034）

2016-05-18 00:45:22

#### lintcode:Search Range in Binary Search Tree

2015-05-16 22:22:15

#### leetcode笔记：Search for a Range

2015-12-25 16:59:29

## 不良信息举报

Search for a range