Wednesday, July 2, 2014

[Leetcode] Generate Parentheses


Problem

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"

Algorithm

This problem can be solved by using the Combination Problem Template.

void helper(List<List<T>> res, List<T> cur, List<E> source, int index, ...){
    if(exceed boundary condition) return;
    if(valid solution) {res.add(cur); return;}
    For each possible choice without potential duplicates
            recursively solve remaining subproblem
    EndFor
}

Code


No comments:

Post a Comment