Search for a Range 解答

Question

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].

Solution

Use binary search, first, find left position, then, find right position.

 1 public class Solution {
 2     public int[] searchRange(int[] nums, int target) {
 3         int[] result = new int[2];
 4         result[0] = -1;
 5         result[1] = -1;
 6         if (nums == null || nums.length < 1)
 7             return result;
 8         int start = 0, end = nums.length - 1, mid, first, last;
 9         // Find first position of target
10         while (start + 1 < end) {
11             mid = (end - start) / 2 + start;
12             if (nums[mid] >= target)
13                 end = mid;
14             else
15                 start = mid;
16         }
17         if (nums[start] == target)
18             result[0] = start;
19         else if (nums[end] == target)
20             result[0] = end;
21
22         // Find last position of target
23         start = 0;
24         end = nums.length - 1;
25         while (start + 1 < end) {
26             mid = (end - start) / 2 + start;
27             if (nums[mid] <= target)
28                 start = mid;
29             else
30                 end = mid;
31         }
32         if (nums[end] == target)
33             result[1] = end;
34         else if (nums[start] == target)
35             result[1] = start;
36         return result;
37     }
38 }
时间: 2024-11-06 14:10:14

Search for a Range 解答的相关文章

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium 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(l

【LeetCode】Search for a Range

Search for a Range 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 ex

leetcode 二分查找 Search for a Range

Search for a Range Total Accepted: 21480 Total Submissions: 78454My Submissions 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

[LeetCode] Search for a Range(二分法)

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,

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数

[leetcode]Search for a Range @ Python

原题地址:https://oj.leetcode.com/problems/search-for-a-range/ 题意: 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

Search for a Range问题

Search for a Range问题 leetcode java 二分查找 1. 问题描述 Given an array of integers sorted in ascending order, 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

LeetCode: Search for a Range [033]

[题目] 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 [

[LeetCode][JavaScript]Search for a Range

Search for a Range 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 ex