19.2.2 [LeetCode 34] Find First and Last Position of Element in Sorted Array

Given an array of integers nums 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 not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

题意

找已排序数组中某个值的范围

题解

 1 class Solution {
 2 public:
 3     vector<int> searchRange(vector<int>& nums, int target) {
 4         int size = nums.size(), s = 0, e = size - 1;
 5         vector<int>ans;
 6         if (size <= 0||target<nums[0]||target>nums[size-1]) {
 7             ans.push_back(-1);
 8             ans.push_back(-1);
 9             return ans;
10         }
11         while (s < e) {
12             int mid = (s + e) / 2;
13             if (nums[mid] < target)
14                 s = mid + 1;
15             else
16                 e = mid;
17         }
18         if (nums[s] != target) {
19             ans.push_back(-1);
20             ans.push_back(-1);
21             return ans;
22         }
23         ans.push_back(s);
24         s = 0, e = size - 1;
25         while (s <= e) {
26             int mid = (s + e) / 2;
27             if (nums[mid] <= target)
28                 s = mid + 1;
29             else
30                 e = mid - 1;
31         }
32         ans.push_back(e);
33         return ans;
34     }
35 };

两次二分

原文地址:https://www.cnblogs.com/yalphait/p/10349393.html

时间: 2024-08-30 14:48:54

19.2.2 [LeetCode 34] Find First and Last Position of Element in Sorted Array的相关文章

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target v

[leetcode][34] Find First and Last Position of Element in Sorted Array

34. Find First and Last Position of Element in Sorted Array Given an array of integers nums 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)

[二分搜索] leetcode 34 Find First and Last Position of Element in Sorted Array

problem:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 经典二分搜索题.要点是改变low或high的时候把当前数字mid也包含进来,因为它也可能是结果. class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int

刷题34. Find First and Last Position of Element in Sorted Array

一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题目的难度是Medium! 二.我的解答 这个题目还是二分查找(折半查找),稍微变化一下.target==nums[mid]后,需要找前面.后面的值是否=target. 一次写出来,bug free,熟能生巧!怎一个爽字了得! #include<iostream> #include<vecto

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity

【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array

描述: Given an array of integers nums 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 not found in the array, return [-1, -

[LeetCode]题解(python):081 - Search in Rotated Sorted Array II

题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ 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

LeetCode 1060. Missing Element in Sorted Array

原题链接在这里:https://leetcode.com/problems/missing-element-in-sorted-array/ 题目: Given a sorted array A of unique numbers, find the K-th missing number starting from the leftmost number of the array. Example 1: Input: A = [4,7,9,10], K = 1 Output: 5 Explan