| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- C++
- 20201013뉴스
- 알고리즘
- 기사
- 헤드라인기사
- json
- 파이썬
- 20200816뉴스
- 헤드라인
- 오늘의뉴스
- 20201017뉴스
- 20201018뉴스
- LeetCode #Python #알고리즘 #코딩테스트 #interview
- MySQL
- 뉴스헤드라인
- 20201016뉴스
- 코테
- 20200615뉴스
- Python
- 백준2225
- 크롤링
- 20201015뉴스
- 백준
- 헤드라인모음
- encoding
- 경제뉴스
- 뉴스
- 헤드라인뉴스
- 기사헤드라인
- 20201011뉴스
Archives
- Today
- Total
HelloCho
[Python] #59. Spiral Matrix II(Medium) 본문
문제 :

이 문제는 #54에서 살펴본 문제와 동일한 문제로 동일한 알고리즘으로 해결할 수 있다. 다만 차이점이 있다면 #54 문제에서는 순회하면서 값을 추출했다면 이 문제에서는 나선형의 모양대로 값을 입력하면 된다.
[Python] #54. Spiral Matrix(Medium)
문제 이 문제는 2차원 배열의 데이터를 나선의 모양으로 순회하는 문제이다. 즉... 아래 빨간색의 방향대로 순차적으로 값을 가져오면 된다. class Solution: def spiralOrder(self, matrix): if not matrix: re..
hellocho.tistory.com
class Solution:
def generateMatrix(self, n):
res = [[0]* n for _ in range(n)]
n_col = n
n_row = n
num_elements = n*n
e = 0
zone = 0
data = 1
while data <= num_elements:
# move right
for i in range(zone, n_col - zone):
res[zone][i] = data
data +=1
if data == num_elements+1:
return res
# move down
for j in range(zone+1,n_row-zone):
res[j][n_col-1-zone] = data
data +=1
if data == num_elements+1:
return res
# move left
for i in range(n_col-1-zone,zone,-1):
res[n_col-1-zone][i-1] = data
data+=1
if data == num_elements+1:
return res
# move up
for j in range(n_row-2-zone,zone,-1):
res[j][zone] = data
data+=1
if data == num_elements+1:
return res
zone +=1
return res
res에 빈 리스트를 만든 뒤 나선형으로 값을 1개씩 증가하면서 부여한다.
결과

28ms, 13.9mb로 통과 !
'코테 > LeetCode' 카테고리의 다른 글
| [Python] #16. 3Sum Closest(Medium) (0) | 2020.09.15 |
|---|---|
| [Python] 15. 3Sum(Medium) (0) | 2020.09.15 |
| [Python] #54. Spiral Matrix(Medium) (0) | 2020.09.11 |
| [Python] #28. Implement strStr()(Easy) (0) | 2020.09.06 |
| [Python] #27. Remove Element(Easy) (0) | 2020.09.06 |
Comments