class (object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ res = [] self.dfs(0, 0, n, [], res) return res def dfs(self, count_left, count_right, n, cur, res): if count_right == n: res.append("".join(cur)) return if count_right < count_left and count_left < n: cur.append("(") self.dfs(count_left+1, count_right, n, cur, res) cur.pop() cur.append(")") self.dfs(count_left, count_right+1, n, cur, res) cur.pop() elif count_right == count_left and count_left < n: cur.append("(") self.dfs(count_left+1, count_right, n, cur, res) cur.pop() elif count_right < count_left and count_left == n: cur.append(")") self.dfs(count_left, count_right+1, n, cur, res) cur.pop()
|
近期评论