Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions valid-parentheses/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
 #해석
#매개변수 string s의 각 character인 c 가 open bracket이면 temp 리스트에 추가한다.
#c가 close bracket이면 temp의 마지막 element와 짝이 맞는지 검사한다. 짝이 아니거나 temp에 아무 요소도 없으면 return false
#검사 이후 temp에 잔여 요소가 남아있으면 짝이 맞지 않았다는 뜻이니 return false, 아닐 경우 return true
#

#Big O
#- N: 문자열 s의 길이

#Time Complexity: O(N) = O(N) + O(1)
#- for c in s : string s의 character의 수 만큼 진행된다. -> O(N)
#-temp.append(c), temp.pop() : 리스트 연산은 상수 취급 -> O(1)

#Space Complexity: O(N)
#- temp : list temp은 최대 string s의 character수 만큼 요소를 저장할 가능성이 있다.


class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
temp = []
for c in s:
#If c is Open bracket, append to the list
if (c == "(") or (c=="{") or (c=="["):
temp.append(c)
#If C is Close bracket, Check the close bracket pairs with last elememt of temp list
else:
#There's no element in the tmep, Return false
if(len(temp)==0):
return False

if(c==")") and (temp.pop()=="("):
continue
if(c=="}") and (temp.pop()=="{"):
continue
if(c=="]") and (temp.pop()=="["):
continue
else:
return False

#After loop, Check temp is empty or not.
#If all c of s is pairs each other, the temp list is empty.
if (len(temp) == 0) :
return True
else:
return False
Comment on lines +46 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요, @Jay-Mo-99 님,
개인 선호 차이인것 같습니다만, 아래와 같이 마무리해도 좋을것 같습니다. 😀

Suggested change
if (len(temp) == 0) :
return True
else:
return False
return len(temp) == 0

또는

Suggested change
if (len(temp) == 0) :
return True
else:
return False
return not temp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return에 바로 조건문을 입력하면 코드 수를 줄일수 있군요. if~else 구조 대신에 자주 사용해보도록 해봐야 겠어요. 피드백 감사합니다.









Loading