[LeetCode]59.Spiral Matrix II

【题目】

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:

[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

【分析】

模拟

【代码】

    /**------------------------------------
    *   日期:2015-02-04
    *   作者:SJF0115
    *   题目: 59.Spiral Matrix II
    *   网址:https://oj.leetcode.com/problems/spiral-matrix-ii/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        vector<vector<int> > generateMatrix(int n) {
            vector<vector<int> > matrix(n,vector<int>(n,0));
            if(n <= 0){
                return matrix;
            }//if
            int count = n * n;
            int index = 1;
            int x = 0,y = -1;
            while(index <= count){
                // right
                ++y;
                while(y < n && matrix[x][y] == 0){
                    matrix[x][y++] = index;
                    ++index;
                }//while
                --y;
                // down
                ++x;
                while(x < n && matrix[x][y] == 0){
                    matrix[x++][y] = index;
                    ++index;
                }//while
                --x;
                // left
                --y;
                while(y >= 0 && matrix[x][y] == 0){
                    matrix[x][y--] = index;
                    ++index;
                }//while
                ++y;
                // up
                --x;
                while(x >= 0 && matrix[x][y] == 0){
                    matrix[x--][y] = index;
                    ++index;
                }//while
                ++x;
            }//while
            return matrix;
        }
    };

    int main(){
        Solution s;
        int n = 5;
        vector<vector<int> > matrix = s.generateMatrix(n);
        // 输出
        for(int i = 0;i < n;++i){
            for(int j = 0;j < n;++j){
                cout<<matrix[i][j]<<"  ";
            }//for
            cout<<endl;
        }//for
        return 0;
    }

【代码二】

    /**------------------------------------
    *   日期:2015-02-05
    *   作者:SJF0115
    *   题目: 59.Spiral Matrix II
    *   网址:https://oj.leetcode.com/problems/spiral-matrix-ii/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
   class Solution {
    public:
        vector<vector<int> > generateMatrix(int n) {
            vector<vector<int> > matrix(n,vector<int>(n,0));
            if(n <= 0){
                return matrix;
            }//if
            int count = n * n;
            int index = 1;
            int beginX = 0,endX = n - 1;
            int beginY = 0,endY = n - 1;
            while(index <= count){
                // right
                for(int i = beginY;i <= endY;++i){
                    matrix[beginX][i] = index;
                    ++index;
                }//for
                ++beginX;
                // down
                for(int i = beginX;i <= endX;++i){
                    matrix[i][endY] = index;
                    ++index;
                }//for
                --endY;
                // left
                for(int i = endY;i >= beginY;--i){
                    matrix[endX][i] = index;
                    ++index;
                }//for
                --endX;
                // up
                for(int i = endX;i >= beginX;--i){
                    matrix[i][beginY] = index;
                    ++index;
                }
                ++beginY;
            }//while
            return matrix;
        }
    };

时间: 2024-09-21 09:23:38

[LeetCode]59.Spiral Matrix II的相关文章

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] C++实现代码:(注意奇数和偶数的不同) #include<iostream> #inclu

[LeetCode]--54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 我想着把这个二维数组分成一层一层的环,然后一层

[LeetCode]240.Search a 2D Matrix II

题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to botto

[LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output:

[LeetCode] Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

[LeetCode]40.Combination Sum II

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[LeetCode]113.Path Sum II

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [分析] 题目要求求出所有路径

[LeetCode]73.Set Matrix Zeroes

[题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu