<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Find Minimum in Rotated Sorted Array ### Source - leetcode: [Find Minimum in Rotated Sorted Array | LeetCode OJ](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) - lintcode: [(159) Find Minimum in Rotated Sorted Array](http://www.lintcode.com/en/problem/find-minimum-in-rotated-sorted-array/) ### Problem Suppose a sorted array 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`). Find the minimum element. #### Example Given `[4, 5, 6, 7, 0, 1, 2]` return `0` #### Note You may assume no duplicate exists in the array. ### 題解 如前節所述,對于旋轉數組的分析可使用畫圖的方法,如下圖所示,升序數組經旋轉后可能為如下兩種形式。 ![Rotated Array](https://box.kancloud.cn/2015-10-24_562b1f42782f9.png) 最小值可能在上圖中的兩種位置出現,如果仍然使用數組首部元素作為target去比較,則需要考慮圖中右側情況。**使用逆向思維分析,如果使用數組尾部元素分析,則無需圖中右側的特殊情況。**不過考慮在內的話也算是一種優化。 ### C++ ~~~ class Solution { public: /** * @param num: a rotated sorted array * @return: the minimum number in the array */ int findMin(vector<int> &num) { if (num.empty()) { return -1; } vector<int>::size_type start = 0; vector<int>::size_type end = num.size() - 1; vector<int>::size_type mid; while (start + 1 < end) { mid = start + (end - start) / 2; if (num[mid] < num[end]) { end = mid; } else { start = mid; } } if (num[start] < num[end]) { return num[start]; } else { return num[end]; } } }; ~~~ ### Java ~~~ public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] num) { if (num == null || num.length == 0) return Integer.MIN_VALUE; int lb = 0, ub = num.length - 1; // case1: num[0] < num[num.length - 1] // if (num[lb] < num[ub]) return num[lb]; // case2: num[0] > num[num.length - 1] or num[0] < num[num.length - 1] while (lb + 1 < ub) { int mid = lb + (ub - lb) / 2; if (num[mid] < num[ub]) { ub = mid; } else { lb = mid; } } return Math.min(num[lb], num[ub]); } } ~~~ ### 源碼分析 僅需注意使用`num[end]`(使用 num[lb]不是那么直觀)作為判斷依據即可,由于題中已給無重復數組的條件,故無需處理`num[mid] == num[end]`特殊條件。 ### 復雜度分析 由于無重復元素,平均情況下復雜度為 O(logn)O(\log n)O(logn).
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看