lettcode笔记--Valid Parentheses
20.Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
用栈来实现,参考:http://blog.csdn.net/feliciafay/article/details/17408469
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
解答:用二叉树递归方法来实现,参考: http://blog.csdn.net/fly_yr/article/details/48754087
只有当右边括号数目小于左边括号数目时,才可以添加右括号。
class Solution {
private:
void generateParenthesis1(vector<string>&v,string s,int l,int r){
if(l==0&&r==0)
{
v.push_back(s);
return;
}
if(l>0)
generateParenthesis1(v,s+"(",l-1,r);
if(r>0&&l<r)
generateParenthesis1(v,s+")",l,r-1);
}
public:
vector<string> generateParenthesis(int n) {
if(n==0)
return vector<string>();
vector<string> v;
generateParenthesis1(v,"",n,n);
return v;
}
};
89.Gray code
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
(看不太懂。。)
参考:http://www.cnblogs.com/jdneo/p/5228780.html 35. Search Insert Position
终于有一道会做的题目了好开心!!
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
在一个排好序的数组中查找某个值,存在则返回对应的value,不存在则返回能够插入的数组中的下标,其实就是找到第一个大于等于目标值的下标,用二分法查找。
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int low=0;
int high=nums.size()-1;
int mid=0;
while(low<=high){
mid=low+(high-low)/2;
if(target<=nums[mid])
high=mid-1;
else
low=mid+1;
}
return low;
}
};
14. Longest Common Prefix--找最长前缀。这里string[j]=0为什么就可以截断了呀?
Write a function to find the longest common prefix string amongst an array of strings.
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector> using namespace std; class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size()==) return "";
char* str=(char*)malloc(sizeof(char)*(strs[].size()+));
for(int i=;i<strs[].size();i++){
str[i]=strs[][i];
}
str[strs[].size()]=;
for(int i=;i<strs.size();i++){
int j=;
while(str[j]&&strs[i][j]&&str[j]==strs[i][j])
j++;
str[j]=;
}
return string(str);
}
}; int main()
{
Solution sl; vector<string> s;
s.push_back("abcd");
s.push_back("abdff");
string str=sl.longestCommonPrefix(s);
if(str=="")
str="无解";
cout<<"this is the end of "+str<<endl;
system("pause");
return ;
}
lettcode笔记--Valid Parentheses的更多相关文章
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
随机推荐
- Quartz.net 2.4.1 使用记录
项目需要开发一个调度任务工具,用于
- 移动端开发demo—移动端web相册(一)
本文主要是介绍开发移动端web相册这样一案例用到的前置知识. 一.移动端样式 移动端更接近手机原生的方式. 如下是一个angular mobile的demo的例子: 移动端demo做成这样的好处: 在 ...
- nginx 正则及rewrite常用规则实例
一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...
- Spring Security 架构与源码分析
Spring Security 主要实现了Authentication(认证,解决who are you? ) 和 Access Control(访问控制,也就是what are you allowe ...
- tomcat优化,java查看
java堆空间分为 新生代 ,老年代 , 持久代 各自有各自的垃圾回收算法 eden区:新生的对象存放在这经常被回收 from .to 存活区 在老年代,回收的频率不是很高 jdk8 就没有持久 ...
- AtCoder Regular Contest 100 (ARC100) E - Or Plus Max 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/9251448.html 题目传送门 - ARC100E 题意 给定一个正整数 $n(n\leq 18)$. 然后 ...
- Zookeeper安装使用
一:zookeeper介绍 Zookeeper 分布式协调组件.本质一个软件. Zookeeper常用功能 1 发布订阅功能.把zookeeper当作注册中心原因. 2 分布式/集群管理功能. 使用j ...
- JSP页面分页显示数据
效果如上图所示!最多显示10条:完整jsp和后台代码如下: <%@ page contentType="text/html;charset=UTF-8" %> < ...
- miniui表格load数据成功后,回调函数,其中setData要用如下方法
init: function () { mini.parse(); this.grid = mini.get("jsDatagrid"); var grid1 = mini.get ...
- hive参数配置及任务优化
一.hive常用参数 0.常用参数 --@Name: --@Description: --@Type:全量加载 --@Author:--- --@CreateDate: --@Target: --@S ...