<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Search for a Range ### Source - leetcode: [Search for a Range | LeetCode OJ](https://leetcode.com/problems/search-for-a-range/) - lintcode: [(61) Search for a Range](http://www.lintcode.com/en/problem/search-for-a-range/) ### Problem Given a sorted array of *n* integers, find the starting and ending position ofa given target value. If the target is not found in the array, return `[-1, -1]`. #### Example Given `[5, 7, 7, 8, 8, 10]` and target value `8`, return `[3, 4]`. #### Challenge O(log *n*) time. ### 題解 lower/upper bound 的結合,做兩次搜索即可。 ### Java ~~~ public class Solution { /** *@param A : an integer sorted array *@param target : an integer to be inserted *return : a list of length 2, [index1, index2] */ public int[] searchRange(int[] A, int target) { int[] result = new int[]{-1, -1}; if (A == null || A.length == 0) return result; int lb = -1, ub = A.length; // lower bound while (lb + 1 < ub) { int mid = lb + (ub - lb) / 2; if (A[mid] < target) { lb = mid; } else { ub = mid; } } // whether A[lb + 1] == target, check lb + 1 first if ((lb + 1 < A.length) && (A[lb + 1] == target)) { result[0] = lb + 1; } else { result[0] = -1; result[1] = -1; // target is not in the array return result; } // upper bound, since ub >= lb, we do not reset lb ub = A.length; while (lb + 1 < ub) { int mid = lb + (ub - lb) / 2; if (A[mid] > target) { ub = mid; } else { lb = mid; } } // target must exist in the array result[1] = ub - 1; return result; } } ~~~ ### 源碼分析 1. 首先對輸入做異常處理,數組為空或者長度為0 1. 分 lower/upper bound 兩次搜索,注意如果在 lower bound 階段未找到目標值時,upper bound 也一定找不到。 1. 取`A[lb + 1]` 時一定要注意判斷索引是否越界! ### 復雜度分析 兩次二分搜索,時間復雜度仍為 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>

                              哎呀哎呀视频在线观看