A very simple C++ solution.


  • 0
    Y
    class Solution {
    public:
        void rotate(vector<vector<int>>& matrix) {
            //swap based on diagonal
            for(std::size_t i=0;i<matrix.size();++i){
                for(std::size_t j=0;j<i;++j){
                    std::swap(matrix[i][j],matrix[j][i]);
                }
            }
            //reverse earch row
            for(std::size_t i=0;i<matrix.size();++i){
                std::reverse(matrix[i].begin(),matrix[i].end());
            }
        }
    };
    

Log in to reply
 

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