solution.md 2.8 KB
Newer Older
1
# 搜索插入位置
F
fix bug  
feilong 已提交
2

3
<p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 5<strong><br />输出:</strong> 2</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 2<strong><br />输出:</strong> 1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 7<strong><br />输出:</strong> 4</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 0<strong><br />输出:</strong> 0</pre>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<font color="red">错误</font>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
```cpp
int main()
{
    Solution sol;
    int res;
    vector<int> nums{1, 3, 5, 6};
    int target = 5;

    res = sol.searchInsert(nums, target);

    cout << res;
    return 0;
}
```

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

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
```cpp
class Solution
{
public:
    int searchInsert(vector<int> &nums, int target)
    {

        int len = nums.size();
        if (target <= nums[0])
            return 0;

        for (int i = 0; i < len; i++)
        {
            if (nums[i] == target)
                return i - 1;
            else if (target < nums[i])
                return i + 1;
        }

        return len;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
58

59
### A
F
fix bug  
feilong 已提交
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
```cpp
class Solution
{
public:
    int searchInsert(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;
            }
        }
        return hi;
    }
};
```

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

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
```cpp
class Solution
{
public:
    int searchInsert(vector<int> &nums, int target)
    {
        int len = nums.size();
        if (len == 0)
            return 0;
        for (int i = 0; i < len; i++)
        {
            if (nums[i] >= target)
                return i;
        }
        return len;
    }
};
```

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

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
```cpp
class Solution
{
public:
    int searchInsert(vector<int> &nums, int target)
    {
        int mid = 0;
        int head = 0;
        int last = nums.size() - 1;
        while (head < last)
        {
            mid = (last - head) / 2 + head;
            if (target > nums[mid])
            {
                head = mid + 1;
            }
            else if (target < nums[mid])
            {
                last = mid - 1;
            }
            else
                return mid;
        }
        if (target <= nums[head])
            return head;
        return head + 1;
    }
};
```