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

 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. BZOJ1036:[ZJOI2008]树的统计——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来 ...

  2. UOJ228:基础数据结构练习题——题解

    http://uoj.ac/problem/228 参考:https://www.cnblogs.com/ljh2000-jump/p/6357583.html 考虑当整个区间的最大值开方==最小值开 ...

  3. mybatis的Mapper代理原理

    前言:在mybatis的使用中,我们会习惯采用XXMapper.java+XXMapper.xml(两个文件的名字必须保持一致)的模式来开发dao层,那么问题来了,在XXMapper的文件里只有接口, ...

  4. 【状压DP】【P2831】【NOIP2016D2T3】愤怒的小鸟

    传送门 Description Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 $(0,0)$ 处,每次 Kiana 可以用它向第一象限发射一 ...

  5. Linux系统上的popen()库函数

    popen可以是系统命令,也可以是自己写的程序a.out. 假如a.out就是打印 “hello world“ 在代码中,想获取什么,都可以通过popen获取. 比如获取ls的信息, 比如获取自己写的 ...

  6. 关于javascript数组的定义与其一些常用方法总结

    由于JavaScript是一门宽松的语言,这种宽松可能会带来更加麻烦的事情.比如JavaScript的数组,定义与使用的方式太灵活有时候让人迷惑.下面将JavaScript中关于数组常用的方法.定义之 ...

  7. eclipse中支持python

    1. 启动eclipse,help-> Install New Software; 2. 点击add 3. 设置Repository name: pydev Location: http://p ...

  8. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. uboot启动原理

    1.裸机运行程序时一般情况下程序代码小于16KB将其下载地址设置到BL1的起始地址.BL0会自动加载并执行BL1. 当程序大于16kB时无法直接运行. 例如UBOOT就大于16KB,执行的原理为.将程 ...

  10. C#中excel读取和写入

    1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { stri ...