做法很巧妙。分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶。注意最后要判断堆栈是否为空。

 bool isValid(const string& s)
{
string left = "([{";
string right = ")]}";
stack<char> stk; for (auto c : s)
{
if (left.find(c) != string::npos)
{
stk.push(c);
}
else
{
if (stk.empty() || stk.top() != left[right.find(c)])
return false;
else
stk.pop();
}
} return stk.empty();
}

Leetcode 之Length of Last Word(38)的更多相关文章

  1. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

  2. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. 【leetcode】Length of Last Word

    题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  4. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  5. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  7. LeetCode(48)-Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  8. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

  10. [leetcode] 18. Length of Last Word

    这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行.题目如下: Given a string s consists of upper/lower-case alphabets and emp ...

随机推荐

  1. BZOJ2730:[HNOI2012]矿场搭建——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2730 https://www.luogu.org/problemnew/show/P3225 听说 ...

  2. BZOJ1834:[ZJOI2010]网络扩容——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1834 https://www.luogu.org/problemnew/show/P2604#sub ...

  3. BZOJ1911:[Apio2010]特别行动队——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1911 又是一个显然的dp……好吧我懒得讲了. s[i]是战斗力前缀和. 我们仍然设k<j< ...

  4. 洛谷 P2272 [ZJOI2007]最大半连通子图 解题报告

    P2272 [ZJOI2007]最大半连通子图 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v \in V\),满 ...

  5. 命令:ln 使用方法

    http://linux.chinaunix.net/man/2004-10-06/45.shtml 指令名称 : ln 使用权限 : 所有使用者 使用方式 : ln [options] source ...

  6. Shell编程语法

    创建shell程序的步骤: 第一步:创建一个脚本文件.sh. 第二步:授予权限使它可以执行chmod u+x .sh 第三步:执行 ./example 或者 sh example 脚本调试:      ...

  7. c++ linux 判断string是中文的 or 英文的 字符串。

    #include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h&g ...

  8. find_in_set 函数使用方法

    find_in_set 函数使用方法 个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文...1,12,13 等等 . 现在有篇文章他既是 头条,又是 ...

  9. [解决]java.io.IOException: Cannot obtain block length for LocatedBlock

    在hadoop测试集群运行job的过程中发现部分运行失败,有Cannot obtain block length for LocatedBlock,使用hdfs dfs -cat ${文件}的时候也报 ...

  10. Azure Pipelines

    https://docs.microsoft.com/en-us/azure/devops/pipelines/?view=vsts