KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (487)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • C++
  • 수학
  • tree
  • Level 2
  • C
  • LeetCode
  • js
  • recursion
  • 제코베 JS 100제
  • 그래프
  • 정렬
  • programmers
  • Level1
  • Level 0
  • string
  • Level 1
  • 백준
  • codeup
  • 문자열
  • 다이나믹 프로그래밍

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

LeetCode / Simulation / 54번 / Spiral Matrix / JS
PS/LeetCode

LeetCode / Simulation / 54번 / Spiral Matrix / JS

2023. 4. 18. 01:52

< 문제 바로가기 >

 

Spiral Matrix - LeetCode

Can you solve this real interview question? Spiral Matrix - Given an m x n matrix, return all elements of the matrix in spiral order.   Example 1: [https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg] Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Outpu

leetcode.com

 

< 문제 간단설명 >

위와 같은 규칙을 가지면서 이동할 때, 이동한 순서를 담긴 배열을 반환한다.

 

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function(matrix) {
    let result = [];
    let [matrixRow, matrixCol] = [matrix.length, matrix[0].length];
    let [startRow, startCol] = [0, 0];
    let [endRow, endCol] = [matrix.length - 1, matrix[0].length - 1];

    let totalElementCount = matrixRow * matrixCol;
    while(true) {
        // result에 모두 담겼을 경우 반복문 탈출
        if(result.length === totalElementCount) {
            break;
        }

        // 왼쪽에서 오른쪽으로
        for(let col=startCol; col<=endCol; col+=1) {
            result.push(matrix[startRow][col]);
        }
        startRow += 1;

        // 위쪽에서 아래쪽으로
        for(let row=startRow; row<=endRow; row+=1) {
            result.push(matrix[row][endCol]);
        }
        endCol -= 1;

        // 마지막에 왼쪽에서 오른쪽으로 가서 중앙에서 끝날 경우,
        // 또는 열이 1개라 아래로 쭉 가서 끝날경우
        if(startRow > endRow || startCol > endCol) {
            break;
        }

        // 오른쪽에서 왼쪽으로
        for(let col=endCol; col>=startCol; col-=1) {
            result.push(matrix[endRow][col]);
        }
        endRow -= 1;

        // 아래쪽에서 위쪽으로
        for(let row=endRow; row>=startRow; row-=1) {
            result.push(matrix[row][startCol]);
        }
        startCol += 1;
    }

    return result;
};
저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / String / 43번 / Multiply Strings / JS  (1) 2023.04.19
LeetCode / String / 14번 / Longest Common Prefix / JS  (0) 2023.04.19
LeetCode / Implementation / 202번 / Happy Number / JS  (0) 2023.04.18
LeetCode / Heap / 692번 / Top K Frequent Words / JS  (0) 2023.04.18
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / String / 43번 / Multiply Strings / JS
    • LeetCode / String / 14번 / Longest Common Prefix / JS
    • LeetCode / Implementation / 202번 / Happy Number / JS
    • LeetCode / Heap / 692번 / Top K Frequent Words / JS
    KimMinJun
    KimMinJun

    티스토리툴바