solution.md 5.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 搜索二维矩阵
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3
<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p><ul>	<li>每行中的整数从左到右按升序排列。</li>	<li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>m == matrix.length</code></li>	<li><code>n == matrix[i].length</code></li>	<li><code>1 <= m, n <= 100</code></li>	<li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

每日一练社区's avatar
每日一练社区 已提交
6
## aop
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10 11 12 13
```cpp
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
14

每日一练社区's avatar
每日一练社区 已提交
15
### after
F
fix bug  
feilong 已提交
16

每日一练社区's avatar
每日一练社区 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
```cpp
int main()
{
    Solution sol;
    int a = 3, b = 4;

    vector<vector<int>> matrix = vector<vector<int>>(a, vector<int>(b)) = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}};
    int target = 3;

    bool res;
    res = sol.searchMatrix(matrix, target);

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

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

每日一练社区's avatar
每日一练社区 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
```cpp
class Solution
{
public:
    bool searchMatrix(vector<vector<int>> &matrix, int target)
    {
        if (matrix.size() == 0 || matrix[0].size() == 0 || target < matrix[0][0] || target > matrix[matrix.size() - 1][matrix[0].size() - 1])
            return false;
        int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
        while (up < down)
        {
            int mid = up + (down - up + 1) / 2;
            if (matrix[mid][0] == target)
                return true;
            else if (matrix[mid][0] > target)
                down = mid - 1;
            else
                up = mid + 1;
        }
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (matrix[up][mid] == target)
                return true;
            else if (matrix[up][mid] > target)
                right = mid - 1;
            else
                left = mid + 1;
        }
        if (matrix[up][left] == target)
            return true;
        return false;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
73

每日一练社区's avatar
每日一练社区 已提交
74
### A
F
fix bug  
feilong 已提交
75

每日一练社区's avatar
每日一练社区 已提交
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 123 124 125 126 127
```cpp
class Solution
{
public:
    bool searchMatrix(vector<vector<int>> &matrix, int target)
    {
        if (matrix.size() == 0 || matrix[0].size() == 0)
            return false;
        decltype(matrix.size()) row_front = 0, c_front = 0, row_back = matrix.size(), c_back = matrix[0].size();
        while (row_front < row_back)
        {
            auto k = row_front + (row_back - row_front) / 2;
            if (matrix[k][0] == target)
            {
                return true;
            }
            else if (matrix[k][0] < target)
            {
                row_front = k + 1;
            }
            else if (matrix[k][0] > target)
            {
                row_back = k;
            }
        }
        if (row_front == 0)
        {
            return false;
        }
        decltype(matrix.size()) target_line = row_front - 1;
        while (c_front < c_back)
        {
            auto j = c_front + (c_back - c_front) / 2;
            if (matrix[target_line][j] == target)
            {
                return true;
            }
            else if (matrix[target_line][j] < target)
            {
                c_front = j + 1;
            }
            else if (matrix[target_line][j] > target)
            {
                c_back = j;
            }
        }
        return false;
    }
};
```

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

每日一练社区's avatar
每日一练社区 已提交
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
```cpp
class Solution
{
public:
    bool searchMatrix(vector<vector<int>> &matrix, int target)
    {
        if (matrix.empty())
            return false;

        int m = matrix.size();
        int n = matrix[0].size();

        int left = 0;
        int right = m * n - 1;
        while (left <= right)
        {
            int middle = (left + right) / 2;
            int middle_element = matrix[middle / n][middle % n];
            if (middle_element == target)
                return true;
            else if (middle_element < target)
                left = middle + 1;
            else
                right = middle - 1;
        }
        return false;
    }
};
```

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

每日一练社区's avatar
每日一练社区 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
```cpp
class Solution
{
public:
    bool searchMatrix(vector<vector<int>> &matrix, int target)
    {
        if (matrix.empty())
            return false;
        int m = matrix.size();
        int n = matrix[0].size();
        int row = 0, col = n - 1;
        while (row < m && col >= 0)
        {
            if (matrix[row][col] < target && col == n - 1)
                row++;
            else if (matrix[row][col] == target)
                return true;
            else
                col--;
        }
        return false;
    }
};
```