Search in Rotated Sorted Array @ LeetCode

Source: Internet
Author: User

A variant of the binary method

Package Level4;/*** Search in Rotated Sorted Array ** Suppose a sorted array is rotated at some unknown to you beforehand. ** (I. e ., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ). ** You are given a target value to search. if found in the array return its * index, otherwise return-1. ** You may assume no duplicate exists in the array. **/public class S33 {public static void main (String [] args) {int [] A = {2, 3, 4, 5, 6, 0, 1 }; system. out. println (search (A, 0);} public static int search (int [] A, int target) {return rec (A, target, 0,. length-1);} // recursive search for public static int rec (int [] A, int target, int low, int high) {if (low> high) {// return-1;} int mid = low + (high-low)/2; if (A [mid] = target) {// return mid;} int res = rec (A, target, low, mid-1) is found; // search for if (res =-1) on the left) {// if not found on the left, continue to search for res = rec (A, target, mid + 1, high);} return res ;}} on the right ;}}

 

 

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.