22.go 507 B

123456789101112131415161718192021222324
  1. package main
  2. func generateParenthesisIter(last string, left, right, goal int, res *[]string) {
  3. if right == goal {
  4. *res = append(*res, last)
  5. return
  6. }
  7. if left < goal {
  8. generateParenthesisIter(last+"(", left+1, right, goal, res)
  9. }
  10. if right < left {
  11. generateParenthesisIter(last+")", left, right+1, goal, res)
  12. }
  13. }
  14. func generateParenthesis(n int) []string {
  15. res := &[]string{}
  16. generateParenthesisIter("", 0, 0, n, res)
  17. return *res
  18. }
  19. /* func main() {
  20. fmt.Println(generateParenthesis(3))
  21. } */