作弊式刷题 7 30

来源:互联网 发布:精选淘宝怎么卸载 编辑:程序博客网 时间:2024/05/21 11:18

22. Generate Parentheses 生成所有的括号组

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:

[  "((()))",  "(()())",  "(())()",  "()(())",  "()()()"]
抄袭思路1:

使用递归的形式。使用递归的形式生成括号序列。代码如下:

class Solution {public:    vector<string> generateParenthesis(int n) {        vector<string> res;        gen(res,"",n,0);        return res;    }
//str采用的是复制的形式,这样各个结果没有影响,n表示现在剩余的未安排的左括号,m
//表示可以安排的右括号数,只有左边有空余的左括号时才能安排有括号。    void gen(vector<string>&res,string str,int n,int m){        if(n==0&&m==0) res.push_back(str);//这里不需要退出,因为三个判断条件不同时满足        if(m>0) gen(res,str+")",n,m-1);        if(n>0) gen(res,str+"(",n-1,m+1);    }};

抄袭思路2:与思路1基本一致 这个思路更清晰一些

class Solution {public:    vector<string> generateParenthesis(int n) {        vector<string> res;        gen(res,"",0,0,n);        return res;    }    void gen(vector<string>& res,string str,int left,int right,int m){        if(str.length()==m*2)         {            res.push_back(str);            return;//这里需要退出        }        if(left<m) gen(res,str+"(",left+1,right,m);        if(right<left) gen(res,str+")",left,right+1,m);    }};
抄袭思路3:只是粘贴在这,并没有尝试

My method is DP. First consider how to get the result f(n) from previous result f(0)...f(n-1).
Actually, the result f(n) will be put an extra () pair to f(n-1). Let the "(" always at the first position, to produce a valid result, we can only put ")" in a way that there will be i pairs () inside the extra () and n - 1 - i pairs () outside the extra pair.

Let us consider an example to get clear view:

f(0): ""

f(1): "("f(0)")"

f(2): "("f(0)")"f(1), "("f(1)")"

f(3): "("f(0)")"f(2), "("f(1)")"f(1), "("f(2)")"

So f(n) = "("f(0)")"f(n-1) , "("f(1)")"f(n-2) "("f(2)")"f(n-3) ... "("f(i)")"f(n-1-i) ... "(f(n-1)")"

Below is my code:

public class Solution{    public List<String> generateParenthesis(int n)    {        List<List<String>> lists = new ArrayList<>();        lists.add(Collections.singletonList(""));                for (int i = 1; i <= n; ++i)        {            final List<String> list = new ArrayList<>();                        for (int j = 0; j < i; ++j)            {                for (final String first : lists.get(j))                {                    for (final String second : lists.get(i - 1 - j))                    {                        list.add("(" + first + ")" + second);                    }                }            }                        lists.add(list);        }                return lists.get(lists.size() - 1);    }}


34. Search for a Range

Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

寻找给定数字的首尾位置






原创粉丝点击