์ƒˆ์†Œ์‹

IN DEPTH CAKE/Supercoder

[c++/LeetCode-2DArray] 48. Rotate Image

  • -
๋ฐ˜์‘ํ˜•

 

๋‚œ์ด๋„Medium

ํ‚ค์›Œ๋“œ: Array

 

๋ฌธ์ œ

 

์ฃผ์–ด์ง„ n x n 2์ฐจ์› ํ–‰๋ ฌ์€ ์ด๋ฏธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด ์ด๋ฏธ์ง€๋ฅผ 90๋„(์‹œ๊ณ„ ๋ฐฉํ–ฅ)๋กœ ํšŒ์ „ํ•˜์‹ญ์‹œ์˜ค.
์ด๋ฏธ์ง€๋ฅผ ์ œ์ž๋ฆฌ์—์„œ (in-place) ํšŒ์ „ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ž…๋ ฅ 2์ฐจ์› ํ–‰๋ ฌ์„ ์ง์ ‘ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค๋ฅธ 2์ฐจ์› ํ–‰๋ ฌ์„ ํ• ๋‹นํ•˜์—ฌ ํšŒ์ „ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค.

 

๋ฌธ์ œ ์›๋ฌธ : https://leetcode.com/problems/rotate-image/description/

 

 

๋ฌธ์ œ ํ’€์ด:

 

์ฐธ๊ณ ๋กœ, ์ด๋ฒˆ ํฌ์ŠคํŒ…์œผ๋กœ ์ •๋ฆฌํ•œ ์ฝ”๋“œ๋Š” ์†๋„๊ฐ€ ๋น ๋ฅธ solution์€ ์•„๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •๊ณผ ๊ฐ™์ด ์ •๋ฆฌํ•ด๋ณด๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค.

์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ณผ์ •๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด๋ณด๋ฉด, ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์กฐ๊ฑด์„ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.

 

๋‚˜๋Š” 3 x 3 ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ๋ถ„์„ํ–ˆ๋‹ค. 90๋„ clock-wise๋กœ ํšŒ์ „์‹œํ‚ฌ ๋•Œ index์— ์ผ์–ด๋‚˜๋Š” ์ผ์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด๋ณด๋ฉด ์ข‹๋‹ค. ๊ทธ๋Ÿฌ๊ณ ๋‚˜์„œ, ๋ณธ ๋ฌธ์ œ๊ฐ€ in-place๋กœ ์ง„ํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐฉ์•ˆํ•ด์„œ rotateํ•˜๋Š” ์• ๋“ค์„ ํ•œ๋ฒˆ์— ์—ฐ์‡„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๊ฒŒ ์ข‹๋‹ค๋Š” ์•„์ด๋””์–ด๊นŒ์ง€ ๋„์ถœํ–ˆ๋‹ค.

 

์ด๋กœ๋ถ€ํ„ฐ ๊ทœ์น™์„ ์ฐพ์•„๋‚ด๊ณ  ์ฝ”๋“œ๋กœ ์˜ฎ๊ฒจ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ฐธ๊ณ ๋กœ, degrees vector๋Š” ๊ทธ๋ƒฅ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ readability๋ฅผ ์œ„ํ•ด ์“ด๊ฑฐ์ง€๋งŒ ์‚ฌ์‹ค์ƒ ๋”๋ฏธ ๋ฒกํ„ฐ์ด๋‹ค.

 

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {

        const auto N = matrix.size();
        vector<int> degrees = {0, 90, 180, 270};

        for(int row = 0; row < round(N / 2.0); ++ row){
            for(int col = row; col < N-row-1; ++col){
                int target = matrix[row][col];
                for(auto degree: degrees){
                    int tmp = row;
                    row = col;
                    col = (N -1) - tmp;
                    tmp = matrix[row][col];
                    matrix[row][col] = target;
                    target = tmp;
                }
            }
        }
    }
};

 

 

์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•˜๋‹ˆ๊นŒ  swap ํ•จ์ˆ˜๋ฅผ ์จ์„œ ์กฐ๊ธˆ ๋” ์ฝ๊ธฐ ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด๋ฉด

 

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {

        const auto N = matrix.size();
        vector<int> degrees = {0, 90, 180, 270};

        for(int row = 0; row < round(N / 2.0); ++ row){
            for(int col = row; col < N-row-1; ++col){
                int target = matrix[row][col];
                for(auto degree: degrees){
                    swap(row, col);
                    col = (N-1) - col;
                    swap(matrix[row][col], target);
                }
            }
        }
    }
};

 

์ข€ ๋” ๋น ๋ฅด๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ตœ์ ํ™”ํ•˜๋ ค๋ฉด transpose๋ฅผ ๋จผ์ € ์‹œํ‚ค๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด ๋น ๋ฅผ ๊ฒƒ ๊ฐ™๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ transpose๋ฅผ ์‹œํ‚ค๋Š” ๊ฒƒ์€ row col๋งŒ ๋ฐ”๊พธ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ๋ณต์žก๋„๊ฐ€ ๋†’์ง€ ์•Š์œผ๋‹ˆ๊นŒ transpose๋กœ ์ ‘๊ทผํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹๊ฒ ๋‹ค.

 

๋ฐ˜์‘ํ˜•

'IN DEPTH CAKE > Supercoder' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[c++/LeetCode-Stack] 1047. Remove All Adjacent Duplicates In String  (1) 2024.05.05
[c++/LeetCode-Stack] 155. MinStack  (1) 2024.05.05
[c++/LeetCode-Array] 75. Sort Colors  (3) 2024.05.05
[c++/LeetCode-Array] 56. Merge Intervals  (2) 2024.05.04
[c++/LeetCode-Array] 162. Find Peak Element  (1) 2024.05.02
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

# ๋กœ๋”ฉ ํ™”๋ฉด ๋™์ž‘ ์ฝ”๋“œ(Code) ์„ค์ •ํ•˜๊ธฐ