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

 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. CF#498 1006F Xor-Paths

    题意:一个n * m的矩阵,求从左上走到右下经过的数异或和为k的方案数. 题解: 因为数据范围较小,所以我们可以采用meet in the middle过掉此题... 然而define inf LL ...

  2. BZOJ2242:[SDOI2011]计算器——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2242 https://www.luogu.org/problemnew/show/P2485 你被 ...

  3. PHP 无限级分类树

    1. function generateTree($items){    $tree = array();    foreach($items as $item){        if(isset($ ...

  4. IDEA批量修改变量快捷键

    Window: Ctrl+Shift+Alt+J Mac:  Ctrl+Option+G

  5. 第六章 指针与const

    const一词在字面上来源于常量constant,const对象在C/C++中是有不同解析的,如第二章所述,在C中常量表达式必须是编译期,运行期的不是常量表达式,因此C中的const不是常量表达式:但 ...

  6. Django 2.0 URL

    Overview¶ A view is a “type” of Web page in your Django application that generally serves a specific ...

  7. Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)

    C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...

  8. Java的四种引用?用到的场景?

    在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2版本开始,把对象的引用分 ...

  9. c++11新特性之future

    std::future可以从异步任务中获取结果,一般与std::async配合使用,std::async用于创建异步任务,实际上就是创建一个线程执行相应任务. 先看段代码: #include < ...

  10. sql 语句 插入数据 返回值问题

    1. 主键ID 自增 ,插入数据后返回这条数据的ID值 insert into tableName() values() select @@identity 2.主键ID 使用GUID类型值 ,插入数 ...