Rotate Image Solutions in C++
Number 48
Difficulty Medium
Acceptance 56.9%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/rotate-image/// Author : Hao Chen// Date : 2014-06-27#include <stdio.h>#include <stdlib.h>#include <iostream>#include <vector>using namespace std;void rotate(vector<vector<int> > &matrix) {int n = matrix.size();for( int i=0; i<n/2; i++ ){int low=i, high=n-i-1;for (int j=low; j<high; j++){int tmp;tmp = matrix[i][j];// left to topmatrix[i][j] = matrix[n-j-1][i];// bottom to leftmatrix[n-j-1][i] = matrix[n-i-1][n-j-1];// right to bottommatrix[n-i-1][n-j-1] = matrix[j][n-i-1];// top to rightmatrix[j][n-i-1] = tmp;}}}void printMatrix(vector<vector<int> > &matrix){for(int i=0; i<matrix.size(); i++){for(int j=0; j< matrix[i].size(); j++) {printf("%3d ", matrix[i][j]) ;}cout << endl;}cout << endl;}int main(int argc, char** argv){int n = 2;if (argc>1){n = atoi(argv[1]);}vector< vector<int> > matrix;for (int i=1; i<=n; i++) {vector<int> v;for(int j=1; j<=n; j++){v.push_back( (i-1)*n + j );}matrix.push_back(v);}printMatrix(matrix);rotate(matrix);printMatrix(matrix);return 0;}
C++ solution by pezy/LeetCode
#include <algorithm> // std::reverse#include <vector>using std::vector; using std::reverse; using std::swap;class Solution {public:void rotate(vector<vector<int> > &matrix) {reverse(matrix.begin(), matrix.end());for (decltype(matrix.size()) i=0; i<matrix.size(); ++i)for (decltype(matrix.size()) j=i; j<matrix[i].size(); ++j)swap(matrix[i][j], matrix[j][i]);}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/rotate-image/description//// Author : liuyubobobo/// Time : 2018-09-13#include <iostream>#include <vector>using namespace std;/// Ad-Hoc/// Time Complexity: O(n^2)/// Space Complexity: O(1)class Solution {public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();for(int i = 0; i <= n / 2; i ++)for(int j = i; j < n - 1 - i; j ++){int t = matrix[i][j];matrix[i][j] = matrix[n - 1 - j][i];matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];matrix[j][n - 1 - i] = t;Solution::print_matrix(matrix);}}static void print_matrix(const vector<vector<int>>& m){for(int i = 0; i < m.size(); i ++){for(int j = 0; j < m[i].size(); j ++)cout << m[i][j] << " ";cout << endl;}cout << endl;}};int main() {vector<vector<int>> matrix1 = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};Solution().rotate(matrix1);Solution::print_matrix(matrix1);vector<vector<int>> matrix2 = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};Solution().rotate(matrix2);Solution::print_matrix(matrix2);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/rotate-image/description//// Author : liuyubobobo/// Time : 2018-12-16#include <iostream>#include <vector>using namespace std;/// Reverse diagonally and then reverse row/// Time Complexity: O(n^2)/// Space Complexity: O(1)class Solution {public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();for(int i = 0; i < n; i ++)for(int j = 0; j < n - i; j ++)swap(matrix[i][j], matrix[n - j - 1][n - i - 1]);for(int i = 0; i < n / 2; i ++)for(int j = 0; j < n; j ++)swap(matrix[i][j], matrix[n - i - 1][j]);}};void print_matrix(const vector<vector<int>>& m){for(int i = 0; i < m.size(); i ++){for(int j = 0; j < m[i].size(); j ++)cout << m[i][j] << " ";cout << endl;}cout << endl;}int main() {vector<vector<int>> matrix1 = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};Solution().rotate(matrix1);print_matrix(matrix1);vector<vector<int>> matrix2 = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};Solution().rotate(matrix2);print_matrix(matrix2);return 0;}