天天看點

LeetCode 59. 螺旋矩陣 II && LeetCode 54. 螺旋矩陣

1. 題目資訊

給定一個正整數 n,生成一個包含 1 到 n2 所有元素,且元素按順時針順序螺旋排列的正方形矩陣。

示例:

輸入: 3
輸出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]           

複制

來源:力扣(LeetCode)

連結:https://leetcode-cn.com/problems/spiral-matrix-ii

著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

2. LeetCode 59 解題

類似題目:LeetCode 885. 螺旋矩陣 III

  • 建立變量top、bottom表示上下行的區間,left、right表示列的區間
LeetCode 59. 螺旋矩陣 II && LeetCode 54. 螺旋矩陣
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int> > v(n, vector<int>(n,0));
        if(n == 0) 
        	return {};
        int i, num = 0, total = n*n, top = 0, bottom = n-1, left = 0, right = n-1;
        while(num < total)
        {
        	for(i = left; i <= right; ++i)
        		v[top][i] = ++num;
        	++top;
        	for(i = top; i <= bottom; ++i)
        		v[i][right] = ++num;
        	--right;
        	for(i = right; i >= left; --i)
        		v[bottom][i] = ++num;
        	--bottom;
        	for(i = bottom; i >= top; --i)
        		v[i][left] = ++num;
        	++left;
        }
        return v;
    }
};           

複制

3. LeetCode 54. 螺旋矩陣

給定一個包含 m x n 個元素的矩陣(m 行, n 列),請按照順時針螺旋順序,傳回矩陣中的所有元素。

LeetCode 59. 螺旋矩陣 II &amp;&amp; LeetCode 54. 螺旋矩陣
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if(matrix.empty()) 
        	return {};
        vector<int> ans;
        int m = matrix.size(), n = matrix[0].size();
        int i, num = 0, total = m*n, top = 0, bottom = m-1, left = 0, right = n-1;
        while(num < total)
        {
        	for(i = left; i <= right; ++i)
        		ans.push_back(matrix[top][i]),num++;
            if(num == total)
                break;
        	++top;
        	for(i = top; i <= bottom; ++i)
        		ans.push_back(matrix[i][right]),num++;
            if(num == total)
                break;
        	--right;
        	for(i = right; i >= left; --i)
        		ans.push_back(matrix[bottom][i]),num++;
            if(num == total)
                break;
        	--bottom;
        	for(i = bottom; i >= top; --i)
        		ans.push_back(matrix[i][left]),num++;
            if(num == total)
                break;
        	++left;
        }
        return ans;
    }
};           

複制

4.《劍指Offer》面試題29

面試題29. 順時針列印矩陣

class Solution {	//2020.2.22
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if(matrix.size()==0 || matrix[0].size()==0)
        	return {};
        int i = 0, k = 0, count = matrix.size()*matrix[0].size();
        int left = 0, right = matrix[0].size()-1, up = 0, bottom = matrix.size()-1;
        vector<int> ans(count);
        while(count)
        {
        	i = left;
        	while(count && i <= right)
        	{
        		ans[k++] = matrix[up][i++];
        		count--;
        	}
        	up++,
        	i = up;
        	while(count && i <= bottom)
        	{
        		ans[k++] = matrix[i++][right];
        		count--;
        	}
        	right--;
        	i = right;
        	while(count && i >= left)
        	{
        		ans[k++] = matrix[bottom][i--];
        		count--;
        	}
        	bottom--;
        	i = bottom;
        	while(count && i >= up)
        	{
        		ans[k++] = matrix[i--][left];
        		count--;
        	}
            left++;
        }
        return ans;
    }
};           

複制

LeetCode 59. 螺旋矩陣 II &amp;&amp; LeetCode 54. 螺旋矩陣