1ms beats 92% Java DFS solution


  • 0
    S
    public List<String> generateParenthesis(int n) {
            List<String> results = new ArrayList<>();
            if (n < 1) {
                return results;
            }
            dfs(0, 0, n, new StringBuilder(), results);
            return results;
        }
        
        private void dfs(int left, int right, int n, StringBuilder sb, List<String> results) {
            int len = left + right;
            if (left + right == 2 * n) {
                results.add(sb.toString());
                return;
            }
            if (left < n) {
                sb.append('(');
                dfs(left + 1, right, n, sb, results);
                sb.setLength(len);
            }
            if (left > right) {
                sb.append(')');
                dfs(left, right + 1, n, sb, results);
                sb.setLength(len);
            }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.