Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 431 Bytes

File metadata and controls

24 lines (21 loc) · 431 Bytes
  • 交换对角线元素,再翻转每一行即可
123    147    741
456 => 258 => 852
789    369    963
  • 实现如下
class Solution {
 public:
  void rotate(vector<vector<int>>& matrix) {
    int sz = size(matrix);
    for (int i = 0; i < sz; ++i) {
      for (int j = i + 1; j < sz; ++j) {
        swap(matrix[i][j], matrix[j][i]);
      }
      reverse(begin(matrix[i]), end(matrix[i]));
    }
  }
};