1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)153:尋找旋轉(zhuǎn)排序數(shù)組中的最小值

        共 792字,需瀏覽 2分鐘

         ·

        2021-01-14 14:25

        算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問(wèn)題叫做?尋找旋轉(zhuǎn)排序數(shù)組中的最小值,我們先來(lái)看題面:
        https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/

        Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

        [4,5,6,7,0,1,2] if it was rotated 4 times.


        Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].


        Given the sorted rotated array nums, return the minimum element of this array.


        題意


        假設(shè)按照升序排序的數(shù)組在預(yù)先未知的某個(gè)點(diǎn)上進(jìn)行了旋轉(zhuǎn)。例如,數(shù)組 [0,1,2,4,5,6,7] 可能變?yōu)?[4,5,6,7,0,1,2] 。

        請(qǐng)找出其中最小的元素。

        提示:

        1 <= nums.length <= 5000
        -5000 <= nums[i] <= 5000
        nums 中的所有整數(shù)都是 唯一 的
        nums 原來(lái)是一個(gè)升序排序的數(shù)組,但在預(yù)先未知的某個(gè)點(diǎn)上進(jìn)行了旋轉(zhuǎn)

        樣例

        示例 1:

        輸入:nums = [3,4,5,1,2]
        輸出:1

        示例 2:

        輸入:nums = [4,5,6,7,0,1,2]
        輸出:0

        示例 3:

        輸入:nums = [1]
        輸出:1



        解題


        思路:二分查找


        本題要明確的一個(gè)要點(diǎn)是最小值一定出現(xiàn)在有旋轉(zhuǎn)點(diǎn)的那一側(cè)
        那么每次搜索我們都需要找到被旋轉(zhuǎn)的那一側(cè)區(qū)間,然后比較選擇元素小的那一側(cè)區(qū)間,那么可以將這兩個(gè)條件合并nums[mid] < nums[right],當(dāng)此條件符合時(shí),被旋轉(zhuǎn)區(qū)間一定在左側(cè),小的元素也一定在左側(cè)
        終止條件:當(dāng)搜索區(qū)間大小為1時(shí),停止搜索,并返回此元素 。

        public?int?findMin(int[] nums)?{
        ????int?left = 0;
        ????int?right = nums.length - 1;
        ????while(left < right){
        ????????int?mid = left + (right - left) / 2;
        ????????if(nums[mid] < nums[right]){
        ????????????right = mid;
        ????????}else{
        ????????????left = mid + 1;
        ????????}
        ????}
        ????return?nums[left];
        }



        好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力。

        上期推文:

        LeetCode1-140題匯總,希望對(duì)你有點(diǎn)幫助!
        LeetCode刷題實(shí)戰(zhàn)141:環(huán)形鏈表
        LeetCode刷題實(shí)戰(zhàn)142:環(huán)形鏈表 II
        LeetCode刷題實(shí)戰(zhàn)143:重排鏈表
        LeetCode刷題實(shí)戰(zhàn)144:二叉樹的前序遍歷
        LeetCode刷題實(shí)戰(zhàn)145:二叉樹的后序遍歷
        LeetCode刷題實(shí)戰(zhàn)146:LRU 緩存機(jī)制
        LeetCode刷題實(shí)戰(zhàn)147:對(duì)鏈表進(jìn)行插入排序
        LeetCode刷題實(shí)戰(zhàn)148:排序鏈表
        LeetCode刷題實(shí)戰(zhàn)149:直線上最多的點(diǎn)數(shù)
        LeetCode刷題實(shí)戰(zhàn)150:逆波蘭表達(dá)式求值
        LeetCode刷題實(shí)戰(zhàn)151:翻轉(zhuǎn)字符串里的單詞
        LeetCode刷題實(shí)戰(zhàn)152:乘積最大子數(shù)組


        瀏覽 10
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            av日日操av | 操骚女 | 一级AAA毛片 | 日本熟妇性爱视频 | 国产精品久久久久久久久夜色 | 免费看v片| 久久伊人五月 | 操操你毞| 男女羞羞视频在线观看 | 男女 视频免费 |