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

 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. 模板:数论 & 数论函数 & 莫比乌斯反演

    作为神秘奖励--?也是为了方便背. 所有的除法都是向下取整. 数论函数: \((f*g)(n)=\sum_{d|n}f(d)g(\frac{n}{d})\) \((Id*\mu)(n)=\sum_{d ...

  2. POJ 2774 求两个串的最长公共前缀 | 后缀数组

    #include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using name ...

  3. LUOGU 1440

    #include<cstdio> #include<algorithm> #include<cstring> #define N 1000005 using nam ...

  4. js空对象判断 isPlainObject

    //有缺陷,JSON.stringify(obj)中,如果obj本来是空的,又继承了一个非空的对象那么结果也会是“{}” 1. JSON.stringify(obj) == '{}' 2. Objec ...

  5. NOIP2016Day2T3愤怒的小鸟(状压dp) O(2^n*n^2)再优化

    看这范围都知道是状压吧... 题目大意就不说了嘿嘿嘿 网上流传的写法复杂度大都是O(2^n*n^2),这个复杂度虽然官方数据可以过,但是在洛谷上会TLE[百度搜出来前几个博客的代码交上去都TLE了], ...

  6. hdu 5621

    KK's Point Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. bzoj 4069 [Apio2015]巴厘岛的雕塑 dp

    [Apio2015]巴厘岛的雕塑 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 494  Solved: 238[Submit][Status][Dis ...

  8. FreeRTOS 问题归纳

    分配给FreeRTOS的heap过少,可能出现任务创建不成功:也可能调用vTaskStartScheduler()后有返回(也就是执行下一条程序),实质上也是vTaskStartScheduler() ...

  9. C语言实现单链表的遍历,逆序,插入,删除

    单链表的遍历,逆序,插入,删除 #include<stdio.h> #include<stdlib.h> #include <string.h> #define b ...

  10. WPF DataGrid、ListView 简单绑定

    DataGrid运行效果: xaml 代码: DataGridName= dtgData ItemsSource= {Binding} AutoGenerateColumns= False DataG ...