作弊式刷题 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]
.
- 作弊式刷题 7 30
- LeetCode 作弊式刷题 7月26日
- 作弊
- 计算广告系列篇(7)------反作弊
- leetcode 7月26日 非作弊刷题
- 作弊心得
- 作弊[古龙版]
- 网游作弊
- 网页作弊
- 关于作弊
- 反作弊
- 反作弊
- 搜索引擎反作弊之:链接作弊与隐藏作弊
- 搜索引擎反作弊之:链接作弊与隐藏作弊
- 搜索引擎反作弊之内容作弊
- 搜索引擎反作弊之内容作弊
- 网络投票中的作弊与反作弊
- 魔高道涨-互联网作弊与反作弊
- 特征工程笔记(1)
- 视频网站上线测试了
- 概率DP [Lydsy2017年4月月赛]抵制克苏恩
- POJ
- remove duplicates from sorted array
- 作弊式刷题 7 30
- 接口和抽象类
- DirectX(九)
- 进程管理--之二
- C/S和B/S网络架构的区别
- 解决 Visual Studio 卸载不完全的问题
- Java容器简介-欢迎大佬指正
- 框架学习之struts2-04数据类型转化和数据校验
- Linux学习小结(二)