码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode Rotate Image

时间:2015-06-25 22:56:48      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

就是转圈圈!

代码:

#include<iostream>
#include<vector>

using namespace std;

void rotate(vector<vector<int>>& matrix) 
{
    int L = matrix.size();
    int i = 0;
    while (i < L / 2)
    {
        int j = 0;
        while (j < L - i*2-1)
        {
            int c = matrix[i][i+j];
            matrix[i][i+j] = matrix[L-i-j-1][i];
            matrix[L - j- i - 1][i] = matrix[L-i-1][L-j-i-1];
            matrix[L - i - 1][L - j-i - 1] = matrix[i+j][L - i - 1];
            matrix[i+j][L - i - 1] = c;
            j++;
        }
        i++;
    }
}

int main()
{
    vector<vector<int>> matrix = 
    {
        {1, 2, 3, 4 },
        {5, 6, 7, 8 },
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    rotate(matrix);
    for (int i = 0; i < matrix.size(); i++)
    {
        for (int j = 0; j < matrix.size(); j++)
        {
            cout << matrix[i][j] << "    ";
        }
        cout << endl;
    }
    return 0;
}

 

leetcode Rotate Image

标签:

原文地址:http://www.cnblogs.com/chaiwentao/p/4601025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!