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的更多相关文章

  1. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  2. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  4. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  8. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

随机推荐

  1. bzoj4428

    题解: f[i]=f[n/(j+1)向上取整]+p*j+k 然后可以通过枚举每个数的因子来做 时间复杂度nlogn(打表看了一下sigma (i因子个数) 是比较接近nlogn的) 可以有方法优化到n ...

  2. Sql与C#中日期格式转换总结

    SQL中的转换方法: 一.将string转换为datetime,主要是使用Convert方法, 方法,Convert(datetime [ ( length ) ] , expression, [st ...

  3. 在启用了“编辑并继续”时,修改包含 lambda 表达式的“method”将会阻止调试会话继续进行

    将所有的引用的“复制到本地”属性都设置成false就可以了

  4. 伪分布式hadoop1.1.2和hbase0.94.11配置

    Hadoop 1.1.2 和Hbase 0.94.11版本配置 测试时ip  172.19.32.128 这个版本需要把/etc/hosts的aa-vm改成127.0.0.1,也就是和localhos ...

  5. Codeforces Gym100783H 最短路 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100783H.html 题目传送门 - CF-Gym100783H 题意 给定一个 $n$ 个节点 ...

  6. P1063 能量项链 区间dp

    题目描述 在MarsMars星球上,每个MarsMars人都随身佩带着一串能量项链.在项链上有NN颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一 ...

  7. 039 在weblogic下部署jndi的多数据源

    这个问题,在公司遇到了,一直没有学,今天学了一下. 后续,还要实验一下,暂时粘贴一下一个不错的url:https://www.cnblogs.com/xdp-gacl/p/4201094.html

  8. >maven-compiler-plugin的理解

    在maven中项目中这个插件一直都会看见,但是一直没有认真学习一下为啥使用,现在有空就查询学习一下. 1.使用场景 下载了一些工程需要编译的时候. maven是个项目管理工具,如果我们不告诉它我们的代 ...

  9. 环境变量误删path找回方法与mysql基础命令

    环境变量误删path找回方法用户path:%USERPROFILE%\AppData\Local\Microsoft\WindowsAppsWin+R 输入regedit 打开注册表(开始-运行里输入 ...

  10. Codeforces 998D. Roman Digits 【打表找规律】

    <题目链接> 题目大意: 现在有无限个 1,5,10,50这四个数字,从中恰好挑选n个数字,问你这些数字的和总共有多少种不同的情况. 解题分析: 由于此题 n 的范围特别大,达到了1e9, ...