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.

答案

方法1

二分思路

If nums[mid] >= nums[start], we know that nums[start…mid] is definitely sorted
    If target is inside start…mid, that would be great, we can just perform a binary search on the interval start…mid

    If target is outside of start…mid, meaning it could be greater than nums[mid] or it could be smaller than nums[start]
    In this case we reduce the search space from start…end to mid + 1…end

If nums[mid] < nums[start], we know that nums[start…mid] is definitely not sorted, which means nums[mid+1…end] is definitely sorted
    If target is inside mid+1…end, that would be great, we can just perform a binary search on the interval mid+1…end

    If target is outside of mid+1…end, meaning it could be greater than nums[end] or it could be smaller than nums[mid+1]
    In this case we reduce the search space from start…end to start…mid - 1

代碼

class Solution {
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while(l <= r) {
            int m = (l + r) / 2;
            if(target == nums[m]) return m;

            if(nums[l] <= nums[m]) {
                if(target >= nums[l] && target <= nums[m]) {
                    r = m - 1;
                }
                else {
                    l = m + 1;
                }
            }
            else {
                if(target >= nums[m] && target <= nums[r]) {
                    l = m + 1;
                }
                else {
                    r = m - 1;
                }
            }
        }
        return -1;
    }
}

方法2
先用binary search找最小元素的index爽雄,以這個index把array分成兩邊芽腾,然后對兩邊分別做binary search

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末进宝,一起剝皮案震驚了整個濱河市坯墨,隨后出現(xiàn)的幾起案子古涧,更是在濱河造成了極大的恐慌测砂,老刑警劉巖洲炊,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異扫茅,居然都是意外死亡,警方通過查閱死者的電腦和手機育瓜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門葫隙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人躏仇,你說我怎么就攤上這事恋脚。” “怎么了焰手?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵糟描,是天一觀的道長。 經(jīng)常有香客問我书妻,道長船响,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任驻子,我火速辦了婚禮灿意,結果婚禮上,老公的妹妹穿的比我還像新娘崇呵。我一直安慰自己缤剧,他們只是感情好,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布域慷。 她就那樣靜靜地躺著荒辕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪犹褒。 梳的紋絲不亂的頭發(fā)上抵窒,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機與錄音叠骑,去河邊找鬼李皇。 笑死,一個胖子當著我的面吹牛宙枷,可吹牛的內(nèi)容都是我干的掉房。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼慰丛,長吁一口氣:“原來是場噩夢啊……” “哼卓囚!你這毒婦竟也來了?” 一聲冷哼從身側響起诅病,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤哪亿,失蹤者是張志新(化名)和其女友劉穎粥烁,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蝇棉,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡讨阻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了篡殷。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片变勇。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖贴唇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情飞袋,我是刑警寧澤戳气,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站巧鸭,受9級特大地震影響瓶您,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜纲仍,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一呀袱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧郑叠,春花似錦夜赵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至沸版,卻和暖如春嘁傀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背视粮。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工细办, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蕾殴。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓笑撞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親区宇。 傳聞我的和親對象是個殘疾皇子娃殖,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容