LetCode 48. 旋转图像

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    // void rotate(vector<vector<int>>& matrix) {
    //     int row = matrix.size();
    //     if (row <= 1)
    //         return;
    //     int round = row / 2;
    //     int edgeTop = 0, edgeBottom = row - 1, edgeLeft = 0, edgeRight = row - 1;
    //     while(round--){
    //         int tmp, last;
    //         for (int i = edgeLeft; i < edgeRight; i++){
    //            //第一个移动是[top,i]->[i,right]
    //             tmp = matrix[i][edgeRight];
    //             matrix[i][edgeRight] = matrix[edgeTop][i];
    //             last = tmp;
    //             //第二个是[i,right]->[bottom,right - (i - edgeLeft)]
    //             tmp = matrix[edgeBottom][edgeRight - (i - edgeLeft)];
    //             matrix[edgeBottom][edgeRight - (i - edgeLeft)] = last;
    //             last = tmp;
    //             //第三个是[bottom,right - (i - edgeLeft)]->[bottom - (i - edgeLeft),left]
    //             tmp = matrix[edgeBottom - (i - edgeLeft)][edgeLeft];
    //             matrix[edgeBottom-(i-edgeLeft)][edgeLeft]=last;
    //             //最后一个则是移动到开始的
    //             matrix[edgeTop][i] = tmp;
    //         }
    //         edgeTop++;
    //         edgeBottom--;
    //         edgeLeft++;
    //         edgeRight--;
    //     }
    // }
    
    void rotate(vector<vector<int>>& matrix) {
        size_t len = matrix.size();
        for (int i = 0; i < len; ++i) {
            for (int j = i + 1; j < len; ++j) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }   
        size_t len2 = len / 2;
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < len2; ++j) {
                swap(matrix[i][j], matrix[i][len - 1 - j]);
            }
        }
    }

};

猜你喜欢

转载自blog.csdn.net/wbb1997/article/details/80988456
今日推荐