๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Rotate Image

by dkswnkk 2021. 11. 15.

๋ฌธ์ œ

 

Rotate Image - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

์ฝ”๋“œ

์ƒˆ๋กœ์šด ๊ณต๊ฐ„ ์„ ์–ธ

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        vector<vector<int>>ans(n);

        for(int i=0; i<n; i++){
            for(int k=n-1; k>=0; k--){
                ans[i].push_back(matrix[k][i]);
            }
        }
        matrix = ans;
    }
};

์ œ์ž๋ฆฌ ํšŒ์ „


class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();

        for(int i=0; i<n; i++){

            for(int k=0; k<=i; k++){
                swap(matrix[i][k],matrix[k][i]);
            }
        }

        for(int i=0; i<n; i++){
            reverse(matrix[i].begin(),matrix[i].end());
        }
    }
};

 

ํ’€์ด

ํ–‰๋ ฌ์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ํšŒ์ „ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ์— "DO NOT allocate another 2D matrix and do the rotation." ์ฆ‰ ๋‹ค๋ฅธ ๊ณต๊ฐ„์„ ์„ ์–ธํ•˜์ง€ ๋ง๊ณ  ํ•ด๊ฒฐํ•˜๋ผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ๋ฒกํ„ฐ ๊ณต๊ฐ„์„ ์„ ์–ธํ•ด์„œ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ์‹์ด ์•„๋‹Œ ์ œ์ž๋ฆฌ์—์„œ ํšŒ์ „์„ ํ•ด์„œ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

'Algorithm ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป > Leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leetcode,c++] Single Number  (0) 2021.11.19
[Leetcode,c++] Combination Sum II  (0) 2021.11.16
[Leetcode,c++] Subsets II  (0) 2021.11.15
[Leetcode,c++] Permutations II  (0) 2021.11.15
[Leetcode,c++] Roman to Integer  (0) 2021.11.15
[Leetcode,c++] Find First and Last Position of Element in Sorted Array  (0) 2021.11.14

๋Œ“๊ธ€