Spiral Matrix Solutions in C++
Number 54
Difficulty Medium
Acceptance 34.2%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/spiral-matrix/// Author : Hao Chen// Date : 2014-06-30#include <stdio.h>#include <stdlib.h>#include <iostream>#include <vector>using namespace std;vector<int> spiralOrder(vector<vector<int> > &matrix) {vector <int> v;int row = matrix.size();if (row<=0) return v;int col = matrix[0].size();if (col<=0) return v;int r, c;for (r=0, c=0; r<(row+1)/2 && c<(col+1)/2; r++, c++){//topfor(int i=c; i<col-c; i++){v.push_back(matrix[r][i]);}//rightfor(int i=r+1; i<row-r; i++){v.push_back(matrix[i][col-c-1]);}//bottomfor(int i=col-c-2; row-r-1>r && i>=c; i--){v.push_back(matrix[row-r-1][i]);}//leftfor(int i=row-r-2; col-c-1>c && i>r; i--){v.push_back(matrix[i][c]);}}return v;}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;}vector< vector<int> > createMatrix(int n, int m){vector< vector<int> > vv;int cnt = 1;for(int i=0; i<n; i++){vector<int> v;for(int j=0; j<m; j++){v.push_back(cnt++);}vv.push_back(v);}return vv;}int main(int argc, char** argv){int n=3, m=3;if (argc>2){n = atoi(argv[1]);m = atoi(argv[2]);}vector< vector<int> > matrix = createMatrix(n, m);printMatrix(matrix);vector<int> v = spiralOrder(matrix);printArray(v);return 0;}
C++ solution by pezy/LeetCode
#include <vector>using std::vector;class Solution {public:vector<int> spiralOrder(vector<vector<int> > &matrix) {vector<int> ret;if (matrix.empty()) return ret;int mMax = matrix.size();int nMax = matrix[0].size();int mMin = 0, nMin = 0;ret.reserve(mMax*nMax);for (; nMin<mMax && nMin<nMax; --mMax, --nMax, ++mMin, ++nMin) {for (int i=nMin; i<nMax; ++i)ret.push_back(matrix[mMin][i]);for (int i=mMin+1; i<mMax; ++i)ret.push_back(matrix[i][nMax-1]);if (mMax-1 == mMin) break;for (int i=nMax-2; i>=nMin; --i)ret.push_back(matrix[mMax-1][i]);if (nMax-1 == nMin) break;for (int i=mMax-2; i>mMin; --i)ret.push_back(matrix[i][nMin]);}return ret;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/spiral-matrix/description//// Author : liuyubobobo/// Time : 2018-04-24#include <iostream>#include <vector>using namespace std;/// Simulation/// Time Complexity: O(n^2)/// Space Complexity: O(n^2)class Solution {private:int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int N, M;public:vector<int> spiralOrder(vector<vector<int>>& matrix) {N = matrix.size();if(N == 0)return {};M = matrix[0].size();if(M == 0)return {};vector<vector<bool>> visited(N, vector<bool>(M, false));int curd = 0, curx = 0, cury = 0;vector<int> res;while(res.size() < N * M){if(!visited[curx][cury]) {res.push_back(matrix[curx][cury]);visited[curx][cury] = true;}int nextx = curx + d[curd][0];int nexty = cury + d[curd][1];if(inArea(nextx, nexty) && !visited[nextx][nexty]){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 < M;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<vector<int>> matrix1 = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};print_vec(Solution().spiralOrder(matrix1));return 0;}