엔지니어 게시판
LeetCode 솔루션 분류

[5/9] 54. Spiral Matrix

컨텐츠 정보

본문

관련자료

댓글 3

JJJJJJJJJJ님의 댓글

  • 익명
  • 작성일
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        spiral = []
        m, n = len(matrix), len(matrix[0])
        total = m * n
        directions = [(0,1), (1,0), (0, -1), (-1, 0)]
        cur_directions = 0
        cur = (0,0)
        curlen = 0
        while curlen < total:
            spiral.append(matrix[cur[0]][cur[1]])
            curlen += 1
            matrix[cur[0]][cur[1]] = -101
            next_pos = (cur[0] + directions[cur_directions][0], cur[1] + directions[cur_directions][1])
            if next_pos[0] < 0 or next_pos[1] < 0 or next_pos[0] == m or next_pos[1] == n or -101 ==  matrix[next_pos[0]][next_pos[1]]:
                cur_directions = (cur_directions + 1) % 4
            cur = (cur[0] + directions[cur_directions][0], cur[1] + directions[cur_directions][1])
        return spiral

mingki님의 댓글의 댓글

  • 익명
  • 작성일
댓글 다실때 오른쪽 아래에 보시면 </> 아이콘이 있는데 이걸로 코드 스니펫 입력 가능해요

mingki님의 댓글

  • 익명
  • 작성일
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 132 명
  • 오늘 방문자 2,218 명
  • 어제 방문자 6,462 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,049 명
알림 0