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, 7, 8, 8, 10] and target value 8,
return [3, 4].

  • 说明:


              1)已排序数组查找,二分查找

  • 实现:


  1. STL实现
 1 class Solution {
 2 public:
 3     vector<int> searchRange(int A[], int n, int target) {
 4        auto low_bound=lower_bound(A,A+n,target);//第一个大于等于>=target元素的指针位置
 5        auto up_bound=upper_bound(low_bound,A+n,target);//第一个大于>target元素的指针位置
 6        if(*low_bound==target)//target是否存在于A[]
 7        {
 8            return vector<int>{distance(A,low_bound),distance(A,prev(up_bound))};
 9        }
10        else return vector<int>{-1,-1};
11
12     }
13 };

     2.  常规实现

 1 class Solution {
 2 public:
 3     vector<int> searchRange(int A[], int n, int target) {
 4         int low=0,high=n-1,middle;
 5         bool isFind=false;
 6         vector<int> vec;
 7         while(low<=high)//二分查找,直至找到,并置标志true
 8         {
 9             middle=(low+high)/2;
10             if(A[middle]==target)
11             {
12                 isFind=true;
13                 break;
14             }
15             else if(A[middle]<target)
16                 low=middle+1;
17             else
18                 high=middle-1;
19         }
20         if(isFind)//如果找到,确定开始、结束与target相等的元素位置
21         {
22             low=middle;
23             high=middle;
24             while(low>=0&&A[low]==target) low--;//下界要>=0
25             low++;
26             while(high<=n-1&&A[high]==target) high++;//上界要<=n-1
27             high--;
28             vec.push_back(low);
29             vec.push_back(high);
30         }
31         else//没有目标值
32         {
33             vec.push_back(-1);
34             vec.push_back(-1);
35         }
36         return vec;
37     }
38 };

leetcode题解:Search for a Range (已排序数组范围查找)

时间: 2024-11-09 05:55:09

leetcode题解:Search for a Range (已排序数组范围查找)的相关文章

C++在已排序数组中查找和值确定的第一次出现的两个数(要求时间复杂度为o(n))

#include <iostream> using namespace std; //输入一个已经按升序排序过的数组和一个数字, //在数组中查找两个数,使得它们的和正好是输入的那个数字. //要求时间复杂度是O(n).如果有多对数字的和等于输入的数字,输出任意一对即可. //例如输入数组1.2.4.7.11.15和数字15.由于4+11=15,因此输出4和11. void Grial(int a[],int x,int y) { int j=x-1; int i=0; while(a[j]&

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode 题解:Merge Sorted Array(两个已排序数组归并)

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A

[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第[88]题(Java):Merge Sorted Array(合并已排序数组)

题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 翻译: 给定两个排序的整数数组nums1和nums2,将nums2合并到nums1中作为一个排序数组. 注意: nums1和nums2中初始化的元素数量分别为m和n. nums1有足够的空间(大小大于或等于m+n)来容纳nums2中的额外元素. 我的思路:此处和归

[leetcode] 34. 在排序数组中查找元素的第一个和最后一个位置(Java)

34. 在排序数组中查找元素的第一个和最后一个位置 题目要求用O(logn),明显要用二分. 其实二分不难,难的是要处理好边界 class Solution { public int[] searchRange(int[] nums, int target) { int i = 0, j = nums.length; int mid = (i + j) / 2; int p = -1; while (i < j) { if (nums[mid] == target) { p = mid; bre

【LeetCode】34. 在排序数组中查找元素的第一个和最后一个位置

题目 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是?O(log n) 级别. 如果数组中不存在目标值,返回?[-1, -1]. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: [3,4] 示例?2: 输入: nums = [5,7,7,8,8,10], target = 6 输出: [-1,-1] 本题同[剑指Offer]面试题53 - I. 在排序数组