일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Regular Expression
- Python
- 315. Count of Smaller Numbers After Self
- Substring with Concatenation of All Words
- Generator
- Convert Sorted List to Binary Search Tree
- 운영체제
- shiba
- DWG
- Class
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- iterator
- 715. Range Module
- kaggle
- Decorator
- 시바견
- 파이썬
- 컴퓨터의 구조
- Python Code
- t1
- 밴픽
- LeetCode
- 43. Multiply Strings
- attribute
- concurrency
- Protocol
- 프로그래머스
- data science
- 30. Substring with Concatenation of All Words
Archives
- Today
- Total
Scribbling
LeetCode: 51. N-Queens 본문
오늘도 무지성 스텍 쌓아버린 나의 코드
from copy import deepcopy
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
ret = []
board = [['.'] * n for _ in range(n)]
self.solver(n, board, 0, ret)
return ret
def solver(self, n, board, num_Q, ret):
if num_Q == n:
ret_board = []
for i in range(n):
temp = list(board[i])
for j in range(len(temp)):
if temp[j] != 'Q':
temp[j] = '.'
ret_board.append(''.join(temp))
ret.append(ret_board)
return
for i in range(n):
if board[num_Q][i] == '.':
next_board = deepcopy(board)
self.place_Q(n, next_board, num_Q, i)
self.solver(n, next_board, num_Q+1, ret)
def place_Q(self, n, board, y, x):
i, j = y, x
while i <= n - 1:
board[i][j] = '-'
i += 1
i, j = y, x
while i <= n - 1 and j >= 0:
board[i][j] = '-'
i += 1
j -= 1
i, j = y, x
while i <= n - 1 and j <= n - 1:
board[i][j] = '-'
i += 1
j += 1
board[y][x] = 'Q'
훨씬 좋은 해법
코드에 대한 누군가의 설명
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 55. Jump Game (0) | 2021.09.15 |
---|---|
LeetCode: 52. N-Queens II (0) | 2021.09.15 |
LeetCode: 10. Regular Expression Matching (0) | 2021.09.14 |
LeetCode: 43. Multiply Strings (0) | 2021.09.13 |
LeetCode: 45. Jump Game II (0) | 2021.09.13 |