Leetcode 之Length of Last Word(38)

做法很巧妙。分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶。注意最后要判断堆栈是否为空。
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)的更多相关文章
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【leetcode】Length of Last Word
题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 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 ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- LeetCode(48)-Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
- [leetcode] 18. Length of Last Word
这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行.题目如下: Given a string s consists of upper/lower-case alphabets and emp ...
随机推荐
- BZOJ5300:[CQOI2018]九连环——题解
一种打表的方法,适用于知道如何解九连环的人. 我们知道,解九(n)连环必须先解第九(n)环,然后解八(n-1).七(n-2)-- 根据这个我们飞快的写出了一个递推式,设\(f[i]\)为\(i\)连环 ...
- 中国MOOC_面向对象程序设计——Java语言_第1周 类与对象
第1周编程题 查看帮助 返回 我们在题目说明中给出了一部分代码,你需要在这部分代码的基础上,按照题目说明编写代码,然后将两部分代码一起提交. 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨 ...
- bzoj 4488 [Jsoi2015]最大公约数 结论+暴力
[Jsoi2015]最大公约数 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 302 Solved: 169[Submit][Status][Dis ...
- JupyterHub的安装与配置——让Jupyter支持多用户
1.下载anaconda 打开https://www.continuum.io/downloads,找到自己要的版本 如:https://repo.anaconda.com/archive/Anaco ...
- java后台svg转成png
1.替换img地址 /** * 替换img地址 * @param str -- 前台svg字符串 * @return */ private String transferImgPath(HttpSer ...
- 旋转 3d
建议chorme浏览器浏览,有样式兼容性问题. 图片可以根据自己本地路径设置路径,js库引用jquery. 写的不好,多多建议,谢谢大家. <html onselectstart="r ...
- [Luogu 2261] CQOI2007 余数求和
[Luogu 2261] CQOI2007 余数求和 这一定是我迄今为止见过最短小精悍的省选题了,核心代码 \(4\) 行,总代码 \(12\) 行,堪比小凯的疑惑啊. 这题一看暴力很好打,然而 \( ...
- UOJ#21 【UR #1】缩进优化
传送门 http://uoj.ac/problem/21 枚举 (调和级数?) $\sum_{i=1}^{n} (a_i / x + a_i \bmod x) =\sum a_i - (\sum_{i ...
- 2016-2017 2 20155335《java程序设计》第四周总结
# 20155335 <Java程序设计>第四周学习总结 ## 教材学习内容总结 继承,在本职上是特殊到一般的关系,即is—a关系,子类继承父类,表明子类是一种特殊的父类,并且具 ...
- JS语句循环(100以内奇偶数、100以内与7先关的数、100以内整数的和、10以内阶乘、乘法口诀、篮球弹起高度、64格子放东西)
3.循环 循环是操作某一个功能(执行某段代码). ①循环四要素: a 循环初始值 b 循环的条件 c 循环状态 d 循环体 ②for循环 a 穷举:把所有的可能性的都一一列出来. b 迭代:每次循环都 ...