1. 程式人生 > >Search in Rotated Sorted Array:在有轉折的升序數列中搜索

Search in Rotated Sorted Array:在有轉折的升序數列中搜索

Suppose an array sorted in ascending order is rotated at some pivot 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.

思路:二分搜尋,只不過因為有轉折,所以判斷那一部分是正常的順序,那一部分有轉折。如果target在正常部分,就正常搜尋。

注意邊界的判斷,比如[1,3] target = 3,[1,2],target = 0,[1] target = 1這種。

class Solution {
    public int search(int[] nums, int target) {
        if(nums.length==0) return -1;
        int s = 0;
        int e = nums.length-1;
        int m;
        while(s<=e){
            m = (s+e)/2;
            System.out.println("nums[m]="+nums[m]+";m="+m);
            if(nums[m]==target) {
                return m;
            }
                //System.out.println("m="+m+";s="+s+";e="+e);
                if(nums[s]<=nums[m]){//s到m之間正常有序,如果target在此區間內,正常二分,等號的目的是考慮[3,1]target=1的情況
                    if(nums[m]>target&&target>=nums[s]){
                        e = m-1;
                    }else{
                        s = m+1;
                    }                       
                }else{//m到e之間正常有序,如果target在此區間內,正常二分
                    if(nums[e]>=target&&target>nums[m]){
                        s = m+1;
                    }else{
                        e = m-1;
                    }    

                        
            }            
        }
        
        return -1;
    }
}
注意,這幾個邊界條件我是硬試出來的
。。。暈了

相關推薦

[LeetCode] Search in Rotated Sorted Array 在旋轉有序陣列

Suppose a sorted array is rotated at some pivot 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 val

[CareerCup] 11.3 Search in Rotated Sorted Array 在旋轉有序矩陣

11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that

Search in Rotated Sorted Array 在旋轉的陣列查詢元素

class Solution { public:     int search(int A[], int n, int target) {         // IMPORTANT: Please reset any member data you declared, as         // the sa

Search in Rotated Sorted Array:在轉折升序數列

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.

Search in Rotated Sorted Array

earch search ted int turn urn div cnblogs || O(logN) Important Point: Once the target is in one section, use the point in that section as

Search in Rotated Sorted Array II

any int param repeat loop return i++ turn through O(N) if element can repeat, the worst case, you cannot throw away any section. eg. [1,

【LeetCode-面試算法經典-Java實現】【033-Search in Rotated Sorted Array(在旋轉數組)】

class con 旋轉 rip target ext addclass 返回 rotated 【033-Search in Rotated Sorted Array(在旋轉數組中搜索)】 【LeetCode-面試算法經典-Java實現】【全部題目

[LeetCode] Search in Rotated Sorted Array ii

become duplicate 代碼 elf end sort medium etc 判斷 題目內容 Suppose an array sorted in ascending order is rotated at some pivot unknown to you b

33. Search in Rotated Sorted Array

get sort ota solution leet pan cor bsp 分享 https://leetcode.com/problems/search-in-rotated-sorted-array/#/description Suppose an array s

leetcode筆記:Search in Rotated Sorted Array

else div ren pub search 數值 src iostream dsm 一.題目描寫敘述 二.解題技巧 因為這道題出現了旋轉的情況,即比第一個元素小的元素可能出如今數值的後半段或者不出現。因此。能夠考慮採用變種的二分查找,即在比較

LeetCode 33. Search in Rotated Sorted Array(在旋轉有序序列

one cheng http ati .com order hal 幫助 列表 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (

[LintCode] Search in Rotated Sorted Array

start private ane pre logs rotated ger value str Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1

LeetCode:33. Search in Rotated Sorted Array(Medium)

可能 要求 時間 idt stat ems oid pri 折半查找法 1. 原題鏈接 https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 2. 題目要求 給定一個按升序排列的數

LeetCode-33. Search in Rotated Sorted Array

二分法 ear 二分查找 bsp 兩種 問題解決 img ret color 一、問題描述      一個有序數組,將它截成兩部分,然後兩部分換位置,得到數組nums。比如對於01234567這個數組,分成012和4567這兩個部分,然後把這兩個部分調換位置,012放在後,

LeetCode-81. Search in Rotated Sorted Array II

解決 sea ++ ddl 調整 .html 重復 lee clas 一、問題描述   這是問題33的升級版,在數組中有重復元素。 二、問題解決   思路和問題33一樣,不同點在於判斷nums[left]==nums[middle]的時候不能得出左右哪個是單調遞增,哪個是循

LC.33. Search in Rotated Sorted Array

ota ont style 變化 sort not sea otherwise img https://leetcode.com/problems/search-in-rotated-sorted-array/description/Suppose an array s

【Leetcode】33. Search in Rotated Sorted Array

null duplicate rotated 是否 == pre ray else while Question: Suppose an array sorted in ascending order is rotated at some pivot unknown t

Search in Rotated Sorted Array II LeetCode Java

ret RM In arr duplicate true rst array AS 描述Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?Would this aff

33.Search in Rotated Sorted Array---二分變形

ted display LG public splay nbsp opened IV return 題目鏈接 題目大意:在一個旋轉數組中,判斷給定的target是否存在於該旋轉數組中。 法一:二分。確定中間元素之後,就要判斷下一步是遍歷左數組還是遍歷右數組。如果左數組有序,

[Swift]LeetCode33. 旋轉排序數組 | Search in Rotated Sorted Array

else 如果 spa print pre array ted art d+ Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i