Leetcode Search for a Range (find)

Source: Internet
Author: User

"Problem description"

Given a sorted array of integers, find the starting and ending position of a Given target value.

Your algorithm ' s runtime complexity must is in the order of O(log n).

If the target is not a found in the array, return [-1, -1] .

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
Return [3, 4] .

1. "Basic Knowledge"

1) Master two points to find ideas and source code

See: Data structure based on the search for two points

Detailed Address: http://blog.csdn.net/u013630349/article/details/47101881

2. "Dick Wire Code"

Implementation failed!

Reason not implemented:

1) can not handle the mid, high, low the relationship between the three;

2) can not handle the marginal conditions in the single variable, boundary variable search time.

Solution Ideas:

1) Mid increment and decrement step;

2) Mid in the use of non-recursive algorithm, high as the upper bound value of size (), the use of recursive algorithm when the value of size ()-1;

Class Solution {public:    vector<int> searchrange (vector<int>& nums, int target) {        int i;vector <int> myvec;if (Nums.empty ()) {myvec.push_back ( -1); Myvec.push_back ( -1); return Myvec;} int low (0), High (Nums.size ()-1); if (nums[low]>target| | Nums[high]<target) {myvec.push_back ( -1); Myvec.push_back ( -1); return Myvec;} int mid = Low+high/2,ind;while (High>=low) {if (target = = Nums[high]) {ind = High;break;} if (Target > Nums[mid]) {low = Mid;mid = (low+high)/2;} if (target = = Nums[high]) {high = Mid;mid = (low+high)/2;} if (target = = Nums[mid]) {ind = Mid;break;}}    int MAX (Ind), MIN (Ind); for (i = Ind;i<nums.size () &&nums[i] = = nums[ind];i++) {max = i;} for (i = ind;i>0&&nums[i]==nums[ind];i--) {min = i;} for (i=min;i<=max;i++) {myvec.push_back (i);} For (I=0;i<myvec.size (); i++) {Cout<<myvec[i]<<endl;} return Myvec;};

3. "Source AC"

Class Solution {public:    vector<int> searchrange (vector<int>& nums, int target) {        const int l = di Stance (Nums.begin (), Lower_bound (Nums.begin (), Nums.end (), target));        const int u = Distance (Nums.begin (), Prev (Upper_bound (Nums.begin (), Nums.end (), target));        if (nums[l]! = target)//not found        return vector<int> {-1,-1};        else        return vector<int> {l, u};}    };

The Lower_bound algorithm returns a non-descending sequence [first, last] in a position greater than or equal to Val.

The Upper_bound algorithm returns a non-descending sequence [first, last] in a position greater than val.

The distance algorithm returns a relative physical address of two input parameters;

About the Prev method

If It is a random-access iterator, the function uses just once operator+ or operator-. Otherwise, the function uses repeatedly the increase or decrease operator (operator++ or operator--) on the copied Iterato R until n elements has been advanced.

Translation

if it is accompanied by machine Access iterator to perform only one operator operation at a time, otherwise, perform n continuous decrement or increment operations.

random-access iterators is iterators that can is used to access elements at a arbitrary offset position relative to The elementthey point to, offering the same functionality as pointers.

"Translation"

A random-access iterator is an iterator that accesses an arbitrary offset address element of an element referred to by a distance iterator, which functions like a pointer.

prev Function: returns an iterator to the top n positions of an iterator :

Template <class bidirectionaliterator>  bidirectionaliterator prev (bidirectionaliterator it,       TypeName Iterator_traits<bidirectionaliterator>::d ifference_type n = 1);

Cases:

To return the last element of the vector above:

cout << prev (a.end ()) << Endl;

To return the bottom 3rd element of the vector above:

cout << prev (A.end (), 3) << Endl;

#include <iostream>     //Std::cout#include <iterator>     //Std::next#include <list>         //STD :: List#include <algorithm>    //Std::for_eachint main () {  std::list<int> mylist;  for (int i=0; i<10; i++) Mylist.push_back (i*10);  Std::cout << "The last element is" << *STD::p rev (Mylist.end ()) << ' \ n ';  return 0;}

About the Advance method

#include <iostream>     //Std::cout#include <iterator>     //Std::advance#include <list>         // Std::listint Main () {  std::list<int> mylist;  for (int i=0; i<10; i++) Mylist.push_back (i*10);  Std::list<int>::iterator it = Mylist.begin ();  Std::advance (it,3);    Std::cout << "The sixth element in MyList is:" << *it << ' \ n ';//  it = Mylist.end ();    Std::advance (it,-3);  Std::cout << "The sixth element in MyList is:" << *it << ' \ n ';//  return 0;}

Common functions for iterators:

See: Common functions for C + + standard library value operation iterators

Detailed Address: http://blog.csdn.net/xuqingict/article/details/32730477

4. "Re-Reel"

1) Jam Section
A. Two points to find unfamiliar, knowledge point memory blurred, especially in the processing of mid;

B. In the common situation after the completion, as far as possible to the special situation into the conventional treatment, to ensure that the uniformity of processing, it is also understandable that the addition of fill can be used, the result-oriented is the first!

2) Algorithm idea

A. Find the first index equal to the variable ' l ', and find the first index greater than the variable ' h ' +1;

B. If the first index that equals the variable is not found, return { -1,-1} if the first index larger than the variable is not found, then ' H ' +1=nums.size ();

C. Output {l,h};

3) PostScript

See: Advance and Prev Method of C + + STL iterator method

Detailed Address: http://blog.csdn.net/u013630349/article/details/47105319

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode Search for a Range (find)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.