zoukankan      html  css  js  c++  java
  • [leetcode]Spiral Matrix II

    class Solution {
    public:
        vector<vector<int> > generateMatrix(int n) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(n == 0) return vector<vector<int>>();
            vector<vector<int>> f(n, vector<int>(n,0));
            
            int cnt = 1;
            for(int start = 0; start < n/2; start++){
                for(int j = start; j < n-start; j++){
                    f[start][j] = cnt;
                    cnt++;
                }
                for(int i = start+1; i < n-1-start; i++){
                    f[i][n-1-start] = cnt;
                    cnt++;
                }
                for(int j = n-1-start; j >= start; j--){
                    f[n-1-start][j] = cnt;
                    cnt++;
                }
                for(int i = n-1-start-1; i>start; i--){
                    f[i][start] = cnt;
                    cnt++;
                }
                
            }
            
            if(n%2 != 0){
                f[n/2][n/2] = cnt;
            }
        
            return f;    
        }
    };


  • 相关阅读:
    2.7 矩阵的秩
    HDU
    HDU
    HDU
    HDU
    HDU
    hdu 5179 beautiful number(数位dp)
    ACdream
    CodeForces
    <a>标签中 href="/" 和 hideFocus="true"
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3225943.html
Copyright © 2011-2022 走看看