美文网首页Leetcode
Leetcode 48. Rotate Image

Leetcode 48. Rotate Image

作者: SnailTyan | 来源:发表于2018-10-11 19:00 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Rotate Image

2. Solution

  • Version 1
class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
        for(int k = 0; k < n; k++) {
            int i = 0;
            int j = n - 1;
            while(i < j) {
                swap(matrix[k][i], matrix[k][j]);
                i++;
                j--;
            }
        }
    }
    
private:
    void swap(int& a, int& b) {
        int temp = a;
        a = b;
        b = temp;
    }
};

Reference

  1. https://leetcode.com/problems/rotate-image/description/

相关文章

网友评论

    本文标题:Leetcode 48. Rotate Image

    本文链接:https://www.haomeiwen.com/subject/huciaftx.html