Leetcode刷题记——34. Search for a Range(查找一个范围)
2016-12-29 18:10
591 查看
一、题目叙述:
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return
For example,
Given
return
Subscribe to see which companies asked this question
二、解题思路:
关键字:二分查找。
1、本题给定一个已经排序好的数组和一个值,要求返回该值在数组中的一个下标范围,使用二分查找,在找不到的情况下,直接返回-1,-1。
2、在找到这个值后,根据返回的位置,分别从返回下标的位置左半边二分查找该值,直到找不到为止,以确定范围最左边的下标;同理去右半边二分查找,以确定范围右边的下标。
三、源码:
import java.util.Arrays;
public class Solution
{
public int[] searchRange(int[] nums, int target)
{
int[] result = {-1, -1};
if (search(0, nums.length - 1, nums, target) < 0)
return result;
else
{
int left, right;
left = right = search(0, nums.length - 1, nums, target);
//result[0] = result[1] = mid;
while (left >= 0)
{
result[0] = left;
left = search(0, left - 1, nums, target);
}
while (right >= 0)
{
result[1] = right;
right = search(right + 1, nums.length - 1, nums, target);
}
}
return result;
}
public int search(int lo, int hi, int[] nums, int target)
{
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (nums[mid] < target) lo = mid + 1;
else if (nums[mid] > target) hi = mid - 1;
else return mid;
}
return -1;
}
public static void main(String args[])
{
int[] nums = {1,1,1,1,1};
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.searchRange(nums, 1)));
}
}
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return
[-1, -1].
For example,
Given
[5, 7, 7, 8, 8, 10]and target value 8,
return
[3, 4].
Subscribe to see which companies asked this question
二、解题思路:
关键字:二分查找。
1、本题给定一个已经排序好的数组和一个值,要求返回该值在数组中的一个下标范围,使用二分查找,在找不到的情况下,直接返回-1,-1。
2、在找到这个值后,根据返回的位置,分别从返回下标的位置左半边二分查找该值,直到找不到为止,以确定范围最左边的下标;同理去右半边二分查找,以确定范围右边的下标。
三、源码:
import java.util.Arrays;
public class Solution
{
public int[] searchRange(int[] nums, int target)
{
int[] result = {-1, -1};
if (search(0, nums.length - 1, nums, target) < 0)
return result;
else
{
int left, right;
left = right = search(0, nums.length - 1, nums, target);
//result[0] = result[1] = mid;
while (left >= 0)
{
result[0] = left;
left = search(0, left - 1, nums, target);
}
while (right >= 0)
{
result[1] = right;
right = search(right + 1, nums.length - 1, nums, target);
}
}
return result;
}
public int search(int lo, int hi, int[] nums, int target)
{
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (nums[mid] < target) lo = mid + 1;
else if (nums[mid] > target) hi = mid - 1;
else return mid;
}
return -1;
}
public static void main(String args[])
{
int[] nums = {1,1,1,1,1};
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.searchRange(nums, 1)));
}
}
相关文章推荐
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
- LeetCode 34. Search for a Range (找到一个范围)
- LeetCode 34. Search for a Range(有序数组,查找给定值范围)
- LeetCode-----34. Search for a Range(查找范围)
- LeetCode 34. Search for a Range(搜索范围)
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
- 【LeetCode-面试算法经典-Java实现】【034-Search for a Range(搜索一个范围)】
- 34. Search for a Range 查找一个区间
- LeetCode 34 Search For A Range 二叉查找相关(二)
- [LeetCode] Search for a Range 搜索一个范围
- [leetcode-二分查找]--34. Search for a Range
- leetcode_34——Search for a Range(二分查找)
- [LeetCode]—Search for a Range 有序数组查找target的下标范围
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
- leetcode题解:Search for a Range (已排序数组范围查找)
- LeetCode 34 Search for a Range(搜索范围)
- LeetCode-Search for a Range-搜索范围-二分查找
- LeetCode | Search for a Range(查找数据出现的范围)
- leetcode 34. Search for a Range 二分查找
- [leetcode] 【查找】 34. Search for a Range