Spiral Matrix II Solutions in C++
Number 59
Difficulty Medium
Acceptance 54.0%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/spiral-matrix-ii/// Author : Hao Chen// Date : 2014-06-30#include <stdio.h>#include <stdlib.h>#include <iostream>#include <vector>using namespace std;vector<vector<int> > generateMatrix(int n, int m);vector<vector<int> > generateMatrix(int n) {return generateMatrix(n, n);}vector<vector<int> > generateMatrix(int n, int m) {vector< vector <int> > matrix(n);if (n<=0) return matrix;for(int i=0; i<n; i++){vector<int> v(m);matrix[i] = v;}int row=n, col=m;int r, c;int cnt=1;for (r=0, c=0; r<(row+1)/2 && c<(col+1)/2; r++, c++){//topfor(int i=c; i<col-c; i++){matrix[r][i] = cnt++;}//rightfor(int i=r+1; i<row-r; i++){matrix[i][col-c-1] = cnt++;}//bottomfor(int i=col-c-2; row-r-1>r && i>=c; i--){matrix[row-r-1][i] = cnt++;}//leftfor(int i=row-r-2; col-c-1>c && i>r; i--){matrix[i][c] = cnt++;}}return matrix;}void printArray(vector<int> v){cout << "[";for(int j=0; j<v.size(); j++) {printf(" %02d", v[j]);}cout << "]" << endl;;}void printMatrix(vector< vector<int> > &vv){for(int i=0; i<vv.size(); i++) {printArray(vv[i]);}cout << endl;}int main(int argc, char** argv){int n=3, m=3;if (argc>1){m = n = atoi(argv[1]);}if (argc>2){m = atoi(argv[2]);}vector< vector<int> > matrix = generateMatrix(n, m);printMatrix(matrix);return 0;}
C++ solution by pezy/LeetCode
#include <vector>using std::vector;class Solution {public:vector<vector<int> > generateMatrix(int n) {vector<vector<int>> vec(n, vector<int>(n,-1));for (int i=0, j=n; value != n*n; ++i, --j)spiralFilled(i, j, vec);return vec;}private:void spiralFilled(int beg, int end, vector<vector<int> >& vec){for (int i=beg, j=beg; true; ){if (vec[i][j] == -1) vec[i][j] = ++value;else break;if (i == beg && j != end-1) { ++j; continue; }if (j == end-1 && i != end-1) { ++i; continue; }if (i == end-1 && j != beg) { --j; continue; }if (j == beg && i != beg) { --i; continue; }}}int value{0};};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/spiral-matrix-ii/description//// Author : liuyubobobo/// Time : 2018-04-24#include <iostream>#include <vector>#include <cmath>using namespace std;/// Simulation/// Time Complexity: O(n^2)/// Space Complexity: O(1)class Solution {private:int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int N;public:vector<vector<int>> generateMatrix(int n) {if(n == 0)return {};N = n;int maxnum = n * n;vector<vector<int>> res(N, vector<int>(N, 0));int curx = 0, cury = 0, curd = 0, num = 1;while(num <= maxnum){if(res[curx][cury] == 0){res[curx][cury] = num;num ++;}int nextx = curx + d[curd][0];int nexty = cury + d[curd][1];if(inArea(nextx, nexty) && res[nextx][nexty] == 0){curx = nextx;cury = nexty;}elsecurd = (curd + 1) % 4;}return res;}private:bool inArea(int x, int y){return x >= 0 && x < N && y >= 0 && y < N;}};void print_matrix(const vector<vector<int>>& matrix){for(const vector<int>& vec: matrix){for(int e: vec)cout << e << " ";cout << endl;}}int main() {vector<vector<int>> res = Solution().generateMatrix(3);print_matrix(res);return 0;}