ํฐ์คํ ๋ฆฌ ๋ทฐ
๐จ๐ป ์ฝ๋ฉํ
์คํธ/Codility
Lesson 7: Stacks and Queues → Brackets
dirmathfl 2020. 6. 9. 19:10728x90
๋ฐ์ํ
๋ฌธ์
https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
๋ฌธ์ ํ์ด
๊ดํธ๊ฐ ์ฌ๋ฐ๋ฅธ ์์ ์ด๋ฃจ๋์ง ํ์ธํ๋ ๋ฌธ์ ๋ค.
์๋ฅผ ๋ค์ด ({)} ์ด๋ฉด ์ฌ๋ฐ๋ฅธ ๊ดํธ ์ ์ด์ง๋ง (}){ ์ผ ๊ฒฝ์ฐ ์ฌ๋ฐ๋ฅธ ๊ดํธ ์์ด ์๋๋ค.
์ฝ๋
def solution(S):
stack = []
opens = ('{', '[', '(')
close = ('}', ']', ')')
for char in S:
if char in opens:
stack.append(char)
else:
if not stack:
return 0
elif opens.index(stack.pop()) == close.index(char):
continue
else:
return 0
return 1 if not stack else 0
-
ํ ์คํธ ์ผ์ด์ค ์ค { { { {๊ฐ ์ ๋ ฅ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ด๋ฅผ ์ํด stack์ ๊ฐ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ์ฌ์ผ ํ๋ค.
728x90
๋ฐ์ํ
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > Codility' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Lesson 7: Stacks and Queues โ Nesting (0) | 2020.06.09 |
---|---|
Lesson 7: Stacks and Queues โ Fish (0) | 2020.06.09 |
Lesson 6: Sorting โ Triangle (0) | 2020.06.08 |
Lesson 6: Sorting โ Max Product Of Three (0) | 2020.06.08 |
Lesson 6: Sorting โ Distinct (0) | 2020.06.08 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ