solution.md 5.9 KB
Newer Older
1
# 螺旋矩阵
F
fix bug  
feilong 已提交
2

3
<p>给你一个 <code>m</code> 行 <code>n</code> 列的矩阵 <code>matrix</code> ,请按照 <strong>顺时针螺旋顺序</strong> ,返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral1.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[1,2,3,6,9,8,7,4,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]<strong><br />输出:</strong>[1,2,3,4,8,12,11,10,9,5,6,7]</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 <= 10</code></li>	<li><code>-100 <= matrix[i][j] <= 100</code></li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

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

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

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

15
### after
F
fix bug  
feilong 已提交
16

每日一练社区's avatar
每日一练社区 已提交
17
```c
18 19 20 21

```

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

每日一练社区's avatar
每日一练社区 已提交
23
```c
24 25 26 27 28 29 30 31 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
class Solution
{
public:
    vector<int> printMatrix(vector<vector<int>> matrix)
    {
        vector<int> res;
        if (matrix.size() == 0)
            return res;
        int r1 = 0, r2 = matrix.size() - 1;
        int c1 = 0, c2 = matrix[0].size() - 1;
        int i = 0, j = 0;
        int sum = matrix.size() * matrix[0].size();
        while (res.size() < sum)
        {
            if (j == c1 && i == r1)
            {
                while (j <= c2)
                    res.push_back(matrix[i][j++]);
                ++r1;
                --j;
                ++i;
            }
            else if (j == c2 && i == r1)
            {
                while (i <= r2)
                    res.push_back(matrix[i++][j]);
                ++c2;
                --i;
                --j;
            }
            else if (j == c2 && i == r2)
            {
                while (j >= c1)
                    res.push_back(matrix[i][j--]);
                --r2;
                ++j;
                --i;
            }
            else
            {
                while (i >= r1)
                    res.push_back(matrix[i--][j]);
                ++c1;
                ++i;
                ++j;
            }
        }
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
77

78
### A
F
fix bug  
feilong 已提交
79

每日一练社区's avatar
每日一练社区 已提交
80
```c
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 128 129 130 131 132 133 134 135
class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int>> &matrix)
    {
        vector<int> res;
        int hor_top = 0;
        int hor_bottom = matrix.size() - 1;
        int ver_left = 0;
        int ver_right = matrix[0].size() - 1;
        int direction = 0;
        while (hor_top <= hor_bottom && ver_left <= ver_right)
        {
            switch (direction)
            {
            case 0:
                for (int i = ver_left; i <= ver_right; i++)
                {
                    res.push_back(matrix[hor_top][i]);
                }
                hor_top++;
                break;
            case 1:
                for (int i = hor_top; i <= hor_bottom; i++)
                {
                    res.push_back(matrix[i][ver_right]);
                }
                ver_right--;
                break;
            case 2:
                for (int i = ver_right; i >= ver_left; i--)
                {
                    res.push_back(matrix[hor_bottom][i]);
                }
                hor_bottom--;
                break;
            case 3:
                for (int i = hor_bottom; i >= hor_top; i--)
                {
                    res.push_back(matrix[i][ver_left]);
                }
                ver_left++;
                break;
            default:
                break;
            }
            direction++;
            direction %= 4;
        }
        return res;
    }
};
```

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

每日一练社区's avatar
每日一练社区 已提交
137
```c
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
class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int>> &matrix)
    {
        vector<int> res;
        if (matrix.size() == 0)
            return res;
        int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
        while (up <= down && left <= right)
        {
            for (int i = left; i <= right; i++)
                res.push_back(matrix[up][i]);
            for (int i = up + 1; i <= down; i++)
                res.push_back(matrix[i][right]);
            if (up < down && left < right)
            {
                for (int i = right - 1; i > left; i--)
                    res.push_back(matrix[down][i]);
                for (int i = down; i > up; i--)
                    res.push_back(matrix[i][left]);
            }
            up++;
            down--;
            left++;
            right--;
        }
        return res;
    }
};
```

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

每日一练社区's avatar
每日一练社区 已提交
172
```c
173 174 175 176 177 178 179 180 181 182 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
class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int>> &matrix)
    {
        vector<int> path;
        if (matrix.empty())
            return path;
        int rows = matrix.size(), cols = matrix[0].size();
        int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
        while (true)
        {
            for (int col = left; col <= right; ++col)
                path.push_back(matrix[top][col]);
            ++top;
            if (top > bottom)
                break;

            for (int row = top; row <= bottom; ++row)
                path.push_back(matrix[row][right]);
            --right;
            if (right < left)
                break;

            for (int col = right; col >= left; --col)
                path.push_back(matrix[bottom][col]);
            --bottom;
            if (top > bottom)
                break;

            for (int row = bottom; row >= top; --row)
                path.push_back(matrix[row][left]);
            ++left;
            if (right < left)
                break;
        }
        return path;
    }
};
```