C++ two methods


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.