LeetCode: Rotate Image
好麻烦的一道题,完全是算术。。少数次过
1 class Solution { 2 public: 3 void rotate(vector<vector<int> > &matrix) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int n = matrix.size(); 7 for (int i = 0; i < n/2; i++) { 8 for (int j = i; j < n-1-i; j++) { 9 int tmp = matrix[i][j]; 10 matrix[i][j] = matrix[n-1-j][i]; 11 matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; 12 matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; 13 matrix[j][n-1-i] = tmp; 14 } 15 } 16 } 17 };
C#
1 public class Solution { 2 public void Rotate(int[,] matrix) { 3 int n = matrix.GetLength(0); 4 for (int i = 0; i < n/2; i++) { 5 for (int j = i; j < n-1-i; j++) { 6 int tmp = matrix[i, j]; 7 matrix[i, j] = matrix[n-1-j, i]; 8 matrix[n-1-j, i] = matrix[n-1-i, n-1-j]; 9 matrix[n-1-i, n-1-j] = matrix[j, n-1-i]; 10 matrix[j, n-1-i] = tmp; 11 } 12 } 13 } 14 }