[LeetCode] 33. Search in Rotated Sorted Array

</br>


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.


</br>

Solution

Since the sorted array is rotated somehow, we should first compare whether the target is in the first half or the second half of the array.

After comparing the target with the nums[0], we can decide whether we start the for loop from the first element of the array or the last one.

The code is shown as below.

Java

public class Solution {
    public int search(int[] nums, int target) {
        
        if (nums.length == 0)
            return -1;
            
        int lo = 0, hi = nums.length;
        if(target < nums[0]){
            for(int i=nums.length-1;i>=0;i--)
                if(nums[i] == target)
                    return i;
        }
        else{
            for(int i=0;i<nums.length;i++)
                if(nums[i] == target)
                    return i;
        }
        return -1;
    }
}

</br>
</br>

Solution 2

Also, we can implement binary search.

However, since the array is not sorted, we have to fix the array before implementing binary search in normal way.

To restore the array to sorted one, we can first compare the target with the first element of the array. If the target is smaller than the first element, it means the target should be at the second half of the array.

And in order to implement binary search, we can then set every element in the first half of the array to Integer.MIN_VALUE, and in this way, the array turns into a sorted array.

Example:

For nums looks like this:

[12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

If target is let's say 14, then we adjust nums to this, where "inf" means infinity:

[12, 13, 14, 15, 16, 17, 18, 19, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf]

If target is let's say 7, then we adjust nums to this:

[-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Other than this little trick, there is nothing much to discuss. The code is shown as below.

Java

public class Solution {
    public int search(int[] nums, int target) {
        
        int lo = 0, hi = nums.length;
        
        while (lo < hi) {
        int mid = (lo + hi) / 2;
        
        double num = (nums[mid] < nums[0]) == (target < nums[0]) ? nums[mid]
                   : target < nums[0] ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                   
        if (num < target)
            lo = mid + 1;
        else if (num > target)
            hi = mid;
        else
            return mid;
        }
        return -1;
    }
}

</br>

However, the run time of this two method does not seem to vary dramatically.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌减宣,老刑警劉巖矫渔,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件猎物,死亡現場離奇詭異淆衷,居然都是意外死亡验残,警方通過查閱死者的電腦和手機捂贿,發(fā)現死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門纠修,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人厂僧,你說我怎么就攤上這事扣草。” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵德召,是天一觀的道長白魂。 經常有香客問我,道長上岗,這世上最難降的妖魔是什么福荸? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮肴掷,結果婚禮上敬锐,老公的妹妹穿的比我還像新娘。我一直安慰自己呆瞻,他們只是感情好台夺,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著痴脾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪赞赖。 梳的紋絲不亂的頭發(fā)上滚朵,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音前域,去河邊找鬼辕近。 笑死,一個胖子當著我的面吹牛匿垄,可吹牛的內容都是我干的移宅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼椿疗,長吁一口氣:“原來是場噩夢啊……” “哼漏峰!你這毒婦竟也來了?” 一聲冷哼從身側響起变丧,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤芽狗,失蹤者是張志新(化名)和其女友劉穎绢掰,沒想到半個月后痒蓬,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡滴劲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年攻晒,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片班挖。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡鲁捏,死狀恐怖,靈堂內的尸體忽然破棺而出萧芙,到底是詐尸還是另有隱情给梅,我是刑警寧澤假丧,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站动羽,受9級特大地震影響包帚,放射性物質發(fā)生泄漏。R本人自食惡果不足惜运吓,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一渴邦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拘哨,春花似錦谋梭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至产镐,卻和暖如春纤垂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磷账。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工峭沦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人逃糟。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓吼鱼,卻偏偏與公主長得像,于是被迫代替她去往敵國和親绰咽。 傳聞我的和親對象是個殘疾皇子菇肃,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容