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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                **一. 題目描述** Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of?`O(logn)`. If the target is not found in the array, return?`[-1, -1]`. For example, Given?`[5, 7, 7, 8, 8, 10]`?and target value?`8`, return?`[3, 4]`. **二. 題目分析** 題目大意是,給定一個已排序的序列和一個目標數字`target`,在這個序列中尋找等于`target`的**元素的下標范圍**。由于序列已經排好序,直接用二分查找,分別求等于`target`的最靠左的元素下標`left`和最靠右的元素下標`right`即可。 **三. 示例代碼** ~~~ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int n = nums.size(); int left = searchRangeIndex(nums, target, 0, n - 1, true); int right = searchRangeIndex(nums, target, 0, n - 1, false); vector<int> result; result.push_back(left); result.push_back(right); return result; } private: int searchRangeIndex(vector<int>& nums, int target, int low, int high, bool isLeft) { while (low <= high) { int midIndex = (low + high) >> 1; if (nums[midIndex] == target) { int temp = -1; if (isLeft) { if (nums[midIndex] == nums[midIndex - 1] && low < midIndex) temp = searchRangeIndex(nums, target, low, midIndex - 1, true); } else { if (nums[midIndex] == nums[midIndex + 1] && high > midIndex) temp = searchRangeIndex(nums, target, midIndex + 1, high, false); } return temp == -1 ? midIndex : temp; // temp == -1時表示只有中間一個值等于target } else if (nums[midIndex] > target) high = midIndex - 1; else low = midIndex + 1; } return -1; // 找不到target,輸出-1 } }; ~~~ ![](https://box.kancloud.cn/2016-01-05_568bb5f1c162b.jpg) **四. 小結** 注意題目要求`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>

                              哎呀哎呀视频在线观看