22. Generate Parentheses

来源:互联网 发布:深夜解莫软件 编辑:程序博客网 时间:2024/06/14 12:13

算是一道启蒙dfs的题,不是取所有解,而是在一定限定条件下取的部分解,而且就两个分支,所以就两个并列的 if 来代表。

很熟悉了,再来过一遍手。。

public class Solution {    public List<String> generateParenthesis(int n) {/*        List<String> result = new ArrayList<String>();            iterateValidPath(n, 0, 0, "", result);        return result;*/                List<String> list = new ArrayList<String>();        dfs(0, 0, n, "", list);        return list;    }        private void dfs(int left, int right, int n, String s, List<String> list){        if(left==n && right==n){            list.add(s);            return;        }        if(left<n){            dfs(left+1, right, n, s+"(", list);        }                if(right<n&&right<left){            dfs(left, right+1, n, s+")", list);        }            }// the following was done around 1.10. I want to re-do the problem again for better understanding of dfs. so writh the above code    /*    public void iterateValidPath(int n, int left, int right, String str, List<String> result){        if(left==n&& right==n){            result.add(str);            return;  // in the void type, just use return!!!        }                // in which condition, we can add "(" to the string; Analysis: the central idea is focusing on constructing a single element at one time. So it is good to compare the implementation of this one to the stringToNumber question, where we collectively generate all possilble result at once, and then procede to the next round. Here we procede with just one result.         if(left<n){            String nextStr=str+"(";            iterateValidPath(n, left+1, right, nextStr, result);        }                if(right<n && right<left){            String nextStr=str+")";            iterateValidPath(n, left, right+1, nextStr, result);        }    }*/}    // by looking at all cases, and think about this, I find the rule is the left part must be large or equal than the right part.    // so I could set the limitations as 1, num_left>=num_right; 2, while(total_num<=6)    // this is like the telephone number question, generation possibity, my knowledge about using tree and DSF to construct the result     // is lacking , so let me wait. == written around 1.10.17    // today is 1.25.17, only two weeks from then, I looked back at what I wrote, and felt that I have pretty good sense of what kind of problem they weren even two weeks back from now, and I have a much better understanding of the dfs now, that I can wrote the code one-time bug-free in 5 mins.... I can combine this question and the phone number question together. By comparing different quesitons, I can remember this dfs techinique much much better.     //  basically, dfs is quite useful for generating a collection/combination of string, sequence, by appending letter one after the other. If the problem can be modelled as a tree, and using the dfs, then we can use the dfs iteration. /**     精华总结 *      Structure/elements of dfs funtion(){        1. dfs fucntion is a helper function.        2. return type: void. So the one of inputs needs to be the List<String>        3. input: a). return result for the main function, such as the list; b). limitation, just like the while loop condition, and the varibles which will be getting closer to the limitation. c). the gradually forming elements, such as str+"(" or str+")" in this case         4. the internal struction of dfs() fucntion;            a) the ending condition for this fucntion. eg: list.add(final str); return;  // remember to return            b) in which conditions that the recursive call of dfs() will happen. in this case, there are two cases: adding "(" and adding ")"... REMEMBER!!! these two cases happen in parallel manner, and they are not conflicting each other. One does not prevent the other, so do not write if + else if.... in the phone number question, all there is no limitation such like here, so we for each loop in the for() loop, we will call the recursive function...         }*/

今天写的,非常简练,漂亮!一道dfs的题,不是取所有解,而是在一定限定条件下取的部分解,而且就两个分支,所以就两个并列的 if 来代表。

public class Solution {    public List<String> generateParenthesis(int n) {        List<String> result= new ArrayList<String>();        dfs(result, n, 0, 0, "");        return result;    }        private void dfs(List<String> result, int n, int left, int right, String str){        if(left==n & right==n){            result.add(str);            return;        }                if(left<n) dfs(result, n, left+1, right, str+"(");        if(right<n && right<left) dfs(result, n, left, right+1, str+")");            }}



0 0
原创粉丝点击