LeetCode - 48. Rotate Image

简介: 48. Rotate Image  Problem's Link  ---------------------------------------------------------------------------- Mean:  顺时针旋转矩阵.

48. Rotate Image 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

顺时针旋转矩阵.

analyse:

略.

Time complexity: O(N)

 

view code

#include <bits/stdc++.h>
using namespace std;

class Solution
{
public :
    vector < vector < int >> rotate( vector < vector < int >>& matrix)
    {
        int n = matrix . size();
        vector < vector < int >> res;
        for( int i = 0; i <n; ++ i)
        {
            vector < int > nums(n);
            int cnt = 0;
            for( int j =n - 1; j >= 0; -- j)
                nums [ cnt ++ ] = matrix [ j ][ i ];
            res . push_back( nums);
        }
        matrix = res;
        return matrix;
    }
};

int main()
{
    int n;
    while( cin >>n)
    {
        vector < vector < int >> matrix(n);
        for( int i = 0; i <n; ++ i)
        {
            vector < int > nums(n);
            for( int j = 0; j <n; ++ j)
            {
                cin >> nums [ j ];
            }
            matrix [ i ] = nums;
        }
        Solution solution;
        auto ans = solution . rotate( matrix);

        for( auto p1: ans)
            for( auto p2: p1)
                cout << p2 << " ";
            cout << endl;
    }
    return 0;
}

 

目录
相关文章
LeetCode 189. 旋转数组 Rotate Array
LeetCode 189. 旋转数组 Rotate Array
LeetCode 189. Rotate Array
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
54 0
LeetCode 189. Rotate Array
|
索引
LeetCode 61. Rotate List
给定链表,将列表向右旋转k个位置,其中k为非负数。
62 0
LeetCode 61. Rotate List
LeetCode 48. Rotate Image
给定一个n x n的二维矩阵,以顺时针方向旋转90度
58 0
LeetCode 48. Rotate Image
|
机器学习/深度学习 存储 人工智能
LeetCode 832. Flipping an Image
LeetCode 832. Flipping an Image
81 0
|
Python
Leetcode-Easy 796. Rotate String
Leetcode-Easy 796. Rotate String
53 0
Leetcode-Easy 796. Rotate String
LeetCode之Rotate Array
LeetCode之Rotate Array
104 0
LeetCode 61:旋转链表 Rotate List
​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。 Given a linked list, rotate the list to the right by k places, where k is non-negative.
3072 0
|
算法 Java 索引
LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 Given an array, rotate the array to the right by k steps, where k is non-negative.
743 0
|
C++
LeetCode 189 Rotate Array(旋转数组)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50600861 翻译 通过K步将一个有着n个元素的数组旋转到右侧。
758 0

热门文章

最新文章