iT邦幫忙

2025 iThome 鐵人賽

DAY 27
0

https://ithelp.ithome.com.tw/upload/images/20251011/20177944ndBLkZvKzf.jpg

感謝

跟33比起來 親民很多

轉幾次都是一個切點除非轉到原順序
int m = l + ((r - l) >> 1); // 通用,免 l+r 溢位
(l + r) / 2 在這題ok

1 就是 右移一位,等同於整數除以 2(取整數部分)

class Solution{//153 O(log n) O(1)
public:
    int findMin(vector<int>& a){
        int l=0,r=(int)a.size()-1;
        while(l<r){
            int m = l + ((r - l) >> 1);
            if(a[m]<=a[r]) r=m; //
            else l=m+1;
        }
        return a[l];
    }
};

上一篇
I have memorized it 33 & 國慶與鐵人快樂 希望趕快結束XD
系列文
轉職仔之Data Science and ai master後的持續精進技術之路27
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言