티스토리 뷰

https://leetcode.com/problems/rotate-image/

 

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

NxN의 크기를 가진 2차원 배열을 시계방향으로 한번 회전 시키는 문제이다.

impl Solution {
    pub fn rotate(mut matrix: &mut Vec<Vec<i32>>) {
        let mut v : Vec<Vec<i32>> = matrix.clone();
        let n = matrix.len();
        for i in 0..n{
            for j in 0..n{
                matrix[i][n - 1 - j] = v[j][i];
            }
        }
    }
}

2차원 배열을 복사한 뒤 각 위치별로 재정립 하면 된다.

0,0 -> 0, v.length

0,1 -> 1, v.length

.

.

.

n,n -> v.length, 0

요런식으로 완성 시키면 된다.

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함