LeetCode033- Search in Rotated Sorted Array

Search in Rotated Sorted Array

Question:

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.

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

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

解法代碼

public class LeetCode33 {
    /**
     * 二分查找姑躲,遞歸
     * 理清思路莱褒,列舉所有可能的情況娩怎,
     * 每一次二分可能有一半是有序集合一半是反轉集合萍恕,也有可能兩邊都是有序集合
     * @param nums
     * @param target
     * @return
     */
    public int search(int[] nums, int target) {
        // 空數組伪阶,直接返回-1
        if(nums == null || nums.length == 0) {
            return -1;
        }
        return search(nums, 0, nums.length - 1, target);
    }
    
    private int search(int[] nums, int start, int end ,int target) {
        if(nums[start] == target) {
            return start;
        }
        if(nums[end] == target) {
            return end;
        }
        // 未找到結果
        if(end - start <= 1) {
            return -1;
        }
        int mid = (start + end + 1) / 2;
        if(nums[mid] == target) {
            return mid;
        }
        // 結果在左半部分有序集中
        if(nums[start] < nums[mid] && target > nums[start] && target < nums[mid]) {
            return search(nums, start, mid, target);
        }
        
        // 結果在右半部分有序集中
        if(nums[mid] < nums[end] && target > nums[mid] && target < nums[end]) {
            return search(nums, mid, end, target);
        }
        
        // 結果在左半部分反轉集中
        if(nums[start] > nums[mid]) {
            return search(nums, start, mid, target);
        }
        
        // 結果在有半部分反轉集中
        return search(nums, mid, end, target);
    }
}

解法代碼

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import com.kay.pro.alog.LeetCode33;

@RunWith(Parameterized.class)
public class LeetCode33Test {
    private LeetCode33 leetCode;
    private int[] nums;
    private int target;
    private int index;
    
    public LeetCode33Test(int[] nums, int target, int index) {
        this.nums = nums;
        this.target = target;
        this.index = index;
    }
    
    @Before
    public void before() {
        leetCode = new LeetCode33();
    }
    
    @Parameters
    public static Collection<?> reverserArrays() {
        return Arrays.asList(new Object[][]{
            {new int[]{4, 5, 6, 7, 0, 1, 2}, 0, 4},
            {new int[]{4, 5, 6, 7, 0, 1, 2}, 3, -1},
            {new int[]{4, 5, 6, 7, 0, 1, 2}, 6, 2},
            {new int[]{8, 7, 6, 5, 4, 3, 2, 1, 0}, 6, 2}
        });
    }
    
    @Test
    public void leetCode33() {
        assertEquals(index, leetCode.search(nums, target));
    }
}

Output:

Time And Space Complexity

Time: O(logn) 二分查找時間復雜度
Space:O(1) 不需要使用額外的存儲空間,原地替換

Tips

二分查找争涌,遞歸
理清思路损俭,列舉所有可能的情況痹栖,每一次二分可能有一半是有序集合一半是反轉集合亿汞,也有可能兩邊都是有序集合

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市揪阿,隨后出現的幾起案子疗我,更是在濱河造成了極大的恐慌,老刑警劉巖南捂,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吴裤,死亡現場離奇詭異,居然都是意外死亡溺健,警方通過查閱死者的電腦和手機麦牺,發(fā)現死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鞭缭,“玉大人剖膳,你說我怎么就攤上這事×肜保” “怎么了潮秘?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長易结。 經常有香客問我,道長,這世上最難降的妖魔是什么搞动? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任躏精,我火速辦了婚禮,結果婚禮上鹦肿,老公的妹妹穿的比我還像新娘矗烛。我一直安慰自己,他們只是感情好箩溃,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布瞭吃。 她就那樣靜靜地躺著,像睡著了一般涣旨。 火紅的嫁衣襯著肌膚如雪歪架。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天霹陡,我揣著相機與錄音和蚪,去河邊找鬼。 笑死烹棉,一個胖子當著我的面吹牛攒霹,可吹牛的內容都是我干的。 我是一名探鬼主播浆洗,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼催束,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了伏社?” 一聲冷哼從身側響起抠刺,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎洛口,沒想到半個月后矫付,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡第焰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年买优,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挺举。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡杀赢,死狀恐怖,靈堂內的尸體忽然破棺而出湘纵,到底是詐尸還是另有隱情脂崔,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布梧喷,位于F島的核電站砌左,受9級特大地震影響脖咐,放射性物質發(fā)生泄漏。R本人自食惡果不足惜汇歹,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一屁擅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧产弹,春花似錦派歌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至斤斧,卻和暖如春早抠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背折欠。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工贝或, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人锐秦。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓咪奖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親酱床。 傳聞我的和親對象是個殘疾皇子羊赵,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354