반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 코딩테스트
- 코드잇
- 연필
- 다비드상
- IT자격증
- PrivateRouter
- 색연필
- Python
- 토익 환급
- 소묘
- 파이썬
- Kriss 재택
- 미켈란젤로
- 웹개발
- csts
- IT 자격증
- KSTQB
- 재택근무
- 프로그래머스
- 프로그래밍
- 클린 코드
- leetcode
- react
- 연필소묘
- 알고리즘
- 취미
- 코딩
- 테스팅 자격증
- 그림
- clean code
Archives
- Today
- Total
글모음
1441. Build an Array With Stack Operations [Easy, Python] 본문
728x90
반응형
1부터 n까지의 숫자들을 list에 넣고, 넣을 때 "Push"
만약 target에 해당하는 숫자가 없다면 제거하면서 "Pop"
기초적인 stack 문제다.
[ 코드 1 ]
class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
ans, temp = [], []
for i in range(1,n+1):
temp.append(i)
ans.append("Push")
if i not in target:
temp.pop()
ans.append("Pop")
if temp == target: break
return ans
Push, Pop을 넣어줄 정답 리스트 ans
굳이 추가로 list를 만들어주지 않아도 괜찮지만 숫자들이 Push, Pop 되는 과정을 보고 싶어서 숫자를 넣을 stack list인 temp를 만들었다.
생각해보니 temp와 target이 같으면 지워주는 라인은 필요가 없는 듯..
[ 코드 2 ]
def buildArray_2(self, target, n):
ans = []
for i in range(1,n+1):
ans.append("Push")
if i not in target:
ans.append("Pop")
return ans
temp 리스트도 지우고 좀 더 깔끔하게 다듬어봤다.
728x90
반응형
'알고리즘 > Leetcode' 카테고리의 다른 글
[Leetcode] 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree [Python, Medium] (0) | 2021.06.18 |
---|---|
[Leetcode] 733. Flood Fill [Easy, Python] (0) | 2021.06.15 |
[Leetcode] 144. Binary Tree Preorder Traversal [Python, Easy] (0) | 2021.06.12 |
682. Baseball Game [Easy, Python] (0) | 2021.03.06 |
Leetcode 문제풀이 목록 (0) | 2021.03.06 |
Comments