Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
生成匹配的括号对,简单DFS
 1 class Solution(object):
 2     ans = []
 3     def DFS(self, str, n, pp):
 4         if n == 0:
 5             if pp == 0:
 6                 self.ans.append(str)
 7                 str = ''
 8                 return
 9         if n > 0:
10             self.DFS(str+'(', n-1, pp+1)
11         if pp > 0:
12             self.DFS(str+')', n, pp-1)
13         
14         
15     def generateParenthesis(self, n):
16         """
17         :type n: int
18         :rtype: List[str]
19         """
20         self.ans = []
21         self.DFS('', n, 0)
22         return self.ans

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理