Share my C++ 6-ms solution


  • 0
    E
    class Solution{
    public:
    	void rotate(vector<vector<int> > &matrix) {
    		int N = matrix.size();
    		int L = N >> 1;
    		vector<int> buff;
    		int loop = 0;
    		int top, bottom, left, right;
    		int i = 0;
    		for (loop = 0; loop < L; loop++){
    			left = top = loop;
    			right = bottom = N - loop - 1;
    
    			for (i = left; i < right; i++){		// top line
    				buff.push_back(matrix[top][i]);
    			}
    			for (i = bottom; i > top; i--) {	// left column
    				matrix[top][N-1 - i] = matrix[i][left];
    			}
    
    			for (i = right; i > left; i--){		// bottom line
    				matrix[i][left] = matrix[bottom][i];
    			}
    			for (i = top; i < bottom; i++) {	// right column
    				matrix[bottom][N-1 - i] = matrix[i][right];
    			}
    			for (i = bottom - 1; i >= top; i--){
    				matrix[i][right] = buff.back();
    				buff.pop_back();
    			}
    		}
    	}
    };

Log in to reply
 

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