solution.md 6.0 KB
Newer Older
1
# 在排序数组中查找元素的第一个和最后一个位置
F
fix bug  
feilong 已提交
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<p>给定一个按照升序排列的整数数组 <code>nums</code>,和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>
<p>如果数组中不存在目标值 <code>target</code>,返回 <code>[-1, -1]</code></p>
<p><strong>进阶:</strong></p>
<ul>
    <li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗?</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出:</strong>[3,4]</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出:</strong>[-1,-1]</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[-1,-1]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
    <li><code>0 <= nums.length <= 10<sup>5</sup></code></li>
    <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
    <li><code>nums</code> 是一个非递减数组</li>
    <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
每日一练社区's avatar
每日一练社区 已提交
24
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
25

26
## aop
F
fix bug  
feilong 已提交
27

28
### before
F
fix bug  
feilong 已提交
29

每日一练社区's avatar
每日一练社区 已提交
30
```cpp
31 32 33
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
34

35
### after
F
fix bug  
feilong 已提交
36

每日一练社区's avatar
每日一练社区 已提交
37
```cpp
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
int main()
{
    Solution sol;
    vector<int> res;
    vector<int> nums{5, 7, 7, 8, 8, 10};
    int target = 8;

    res = sol.searchRange(nums, target);

    for (auto i : res)
        cout << i << " ";
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
54

每日一练社区's avatar
每日一练社区 已提交
55
```cpp
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
class Solution
{
public:
    int left(vector<int> &nums, int target)
    {
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] == target)
            {
                if (mid == 0 || nums[mid - 1] < target)
                {
                    return mid;
                }
                right = mid + 1;
            }
            else if (nums[mid] > target)
            {
                right = mid + 1;
            }
            else
            {
                left = mid - 1;
            }
        }
        return -1;
    }
    int right(vector<int> &nums, int target)
    {
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] == target)
            {
                if (mid == nums.size() - 1 || nums[mid + 1] > target)
                {
                    return mid;
                }
                left = mid - 1;
            }
            else if (nums[mid] > target)
            {
                right = mid + 1;
            }
            else
            {
                left = mid - 1;
            }
        }
        return -1;
    }

    vector<int> searchRange(vector<int> &nums, int target)
    {
        vector<int> result;
        result.push_back(left(nums, target));
        result.push_back(right(nums, target));
        return result;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
123

124
### A
F
fix bug  
feilong 已提交
125

每日一练社区's avatar
每日一练社区 已提交
126
```cpp
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
class Solution
{
public:
    vector<int> searchRange(vector<int> &nums, int target)
    {
        vector<int> res(2, -1);
        int i = 0, j = nums.size();
        int mid = (i + j) / 2;

        int p = -1;
        while (i < j)
        {
            if (nums[mid] == target)
            {
                p = mid;
                break;
            }
            if (nums[mid] > target)
            {
                if (j == mid)
                    break;
                j = mid;
                mid = (i + j) / 2;
            }
            else
            {
                if (i == mid)
                    break;
                i = mid;
                mid = (i + j) / 2;
            }
        }

        if (p == -1)
        {
            return res;
        }
        else
        {
            int a = p, b = p;
            while (a > 0 && nums[a - 1] == target)
                a--;
            while (b < nums.size() - 1 && nums[b + 1] == target)
                b++;
            vector<int> h;
            h.push_back(a);
            h.push_back(b);
            return h;
        }
    }
};
```

### B
F
fix bug  
feilong 已提交
181

每日一练社区's avatar
每日一练社区 已提交
182
```cpp
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
class Solution
{
public:
    vector<int> searchRange(vector<int> &nums, int target)
    {
        vector<int> res;
        res.push_back(binary_search_begin(nums, target));
        res.push_back(binary_search_end(nums, target));
        return res;
    }

private:
    int binary_search_begin(vector<int> nums, int target)
    {
        int lo = -1;
        int hi = nums.size();
        while (lo + 1 < hi)
        {
            int mid = lo + (hi - lo) / 2;
            if (target > nums[mid])
            {
                lo = mid;
            }
            else
            {
                hi = mid;
            }
        }
        if (hi == nums.size() || nums[hi] != target)
        {
            return -1;
        }
        else
        {
            return hi;
        }
    }
    int binary_search_end(vector<int> nums, int target)
    {
        int lo = -1;
        int hi = nums.size();
        while (lo + 1 < hi)
        {
            int mid = lo + (hi - lo) / 2;
            if (target < nums[mid])
            {
                hi = mid;
            }
            else
            {
                lo = mid;
            }
        }
        if (lo == -1 || nums[lo] != target)
        {
            return -1;
        }
        else
        {
            return lo;
        }
    }
};
```

### C
F
fix bug  
feilong 已提交
249

每日一练社区's avatar
每日一练社区 已提交
250
```cpp
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
class Solution
{
public:
    vector<int> searchRange(vector<int> &nums, int target)
    {
        int start = -1, end = -1;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == target)
            {
                if (start == -1)
                    start = i;
                end = i;
            }
        }
        vector<int> ans;
        ans.push_back(start);
        ans.push_back(end);
        return ans;
    };
};
```