Skip to content

Latest commit

 

History

History
42 lines (26 loc) · 927 Bytes

2018-04-09-2.md

File metadata and controls

42 lines (26 loc) · 927 Bytes

이 문제의 저작권은 매일프로그래밍(https://mailprogramming.com/)에 있습니다.

문제

  • 매일프로그래밍 - Question 3
  • 정수 n이 주어지면, n개의 여는 괄호 "("와 n개의 닫는 괄호 ")"로 만들 수 있는 괄호 조합을 모두 구하시오. (시간 복잡도 제한 없습니다).

풀이

  • 재구함수를 이용해서 여는괄호와 닫는괄호의 수를 비교해가면서 추가를 해주면서 재귀함수를 호출하는 방식으로 작동한다.

코드

def get_parenthesis(n):
	answers = []
	recursion(answers,'',0,0,n)
	return answers


def recursion(answers, s, open, close, n):
	if len(s) == n*2:
		answers.append(s)
		return

	if open < n:
		recursion(answers, s+'(',open+1,close,n)
	if close < open:
		recursion(answers, s+')',open,close+1,n)


print(get_parenthesis(3))

반성

참고자료