1. 程式人生 > >[Leetcode59] 螺旋矩陣II

[Leetcode59] 螺旋矩陣II

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

程式碼寫得很繁瑣,不過好歹是實現了哈哈哈

python:

class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        if n == 1:
            return [[1]]
        res = []
        for i in range(n):
            temp = []
            for j in range(n):
                temp.append(0)
            res.append(temp)
        i, j, m_min, n_min = 0, 0, 0, 0
        m_max, n_max = n, n
        num = 0
        while num < n**2:
            num += 1
            if i == m_min and j < (n_max - 1):
                res[i][j] = num
                j += 1
                if j == (n_max - 1):
                    m_min += 1
                    if m_min == m_max:
                        res[i][j] = num + 1
                        break
            elif i < (m_max - 1) and j == (n_max - 1):
                res[i][j] = num
                i += 1
                if i == (m_max - 1):
                    n_max -= 1
                    if n_min == n_max:
                        res[i][j] = num + 1
                        break
            elif i == (m_max - 1) and j > n_min:
                res[i][j] = num
                j -= 1
                if j == n_min:
                    m_max -= 1
                    if m_min == m_max:
                        res[i][j] = num + 1
                        break
            elif i > m_min and j == n_min:
                res[i][j] = num
                i -= 1
                if i == m_min:
                    n_min += 1
                    if n_min == n_max:
                        res[i][j] = num + 1
                        break
        return res

C++: 

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res;
        for(int i = 0;i < n;i++){
            vector<int> temp;
            for(int j = 0;j < n;j++){
                temp.push_back(0);
            }
            res.push_back(temp);
        }
        if(n == 1){
            res[0][0] = 1;
            return res;
        }
        int i = 0;int j = 0;int m_min = 0;int n_min = 0;
        int m_max = n;int n_max = n;
        int num = 0;
        while(num < pow(n,2)){
            num += 1;
            if(i == m_min && j < (n_max - 1)){
                res[i][j] = num;j += 1;
                if(j == (n_max - 1)){
                    m_min += 1;
                    if(m_min == m_max){
                        res[i][j] = num + 1;break;
                    }
                }
            }
            else if(i < (m_max - 1) && j == (n_max - 1)){
                res[i][j] = num;i += 1;
                if(i == (m_max - 1)){
                    n_max -= 1;
                    if(n_min == n_max){
                        res[i][j] = num + 1;break;
                    }
                }
            }
            else if(i == (m_max - 1) && j > n_min){
                res[i][j] = num;j -= 1;
                if(j == n_min){
                    m_max -= 1;
                    if(m_min == m_max){
                        res[i][j] = num + 1;break;
                    }
                }
            }
            else if(i > m_min && j == n_min){
                res[i][j] = num;i -= 1;
                if(i == m_min){
                    n_min += 1;
                    if(n_min == n_max){
                        res[i][j] = num + 1;break;
                    }
                }
            }
        }
        return res;
    }
};