问题描述:给定一个字符串,其中只包含字符‘{’,    '}',    '[',    ']',   '(',    ')'确定如果输入字符串是有效的。
括号必须以正确的顺序排列,“()”和“()[]{ }”都是有效的,   "{",  " {]"等都是无效的。

解题思路:利用栈,如果不是右括号就压入栈中,如果是右括号,就看前一个字符是不是匹配的左括号,如果匹配出栈,接着看后面字符,不匹配则返回false。

 public boolean isValid(String s) {
Map<Character,Character> map = new HashMap<>();
map.put('(',')');
map.put('[',']');
map.put('{','}');
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i ++)
{
char curr = s.charAt(i);
if(map.keySet().contains(curr))
{
stack.push(curr);
}
//注意判断空的情况
else if(!stack.empty() && map.values().contains(curr))
{
char pre = stack.peek();
if(map.get(pre)==curr)
{
stack.pop();
}
else
{
return false;
}
}
else
{
return false;
}
}
return stack.empty();
}

Valid Parentheses有效括号匹配。利用栈。的更多相关文章

  1. 2.Valid Parentheses (括号匹配)

    Level: ​  Easy 题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  2. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  3. YTU 3003: 括号匹配(栈和队列)

    3003: 括号匹配(栈和队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版] 题目描述 假设一个表达式中只允许包含三种括号:圆括号&quo ...

  4. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...

  5. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. OJP1147括号匹配加强版(栈)与P1153乱头发节(单调栈)

    惨兮兮的被刷掉2%的通过率后在经过思考和dalao的指点后终于A掉了这道题 强烈建议修改这题的样例,实在太迷惑人,各种错误算法都能过 比如说这是一份错误代码,看懂了也不要学思路,和正解不知道差到哪里去 ...

随机推荐

  1. Oracle备份一张表

    数据库:myOnly 创建表:myTable 的备份表 myTable_tmpe create table myTable_tmpe as select * from myTable ; 补充: -- ...

  2. 用SQL语句生成唯一标识

    以前都是在代码中生成GUID值,然后保存到数据库中去,今天发现用sql也能生成GUID值,觉得很新奇,所以记下来. sellect newid();  //得到的即为GUID值 此sql内置函数返回的 ...

  3. Error: unable to connect to node rabbit@10: nodedown 修改hostname后异常

    https://blog.csdn.net/witsmakemen/article/details/22651365 [root@d bin]# rabbitmqctl start_appStarti ...

  4. linux linux 互传文件 win 不通过 ftp sftp 往linux 传文件(文件夹)

    linux 传入 传出文件 swp  port  22 怎样通过swp通过docker 容器向外传文件 通过scp Linux互传文件,需要知道文件源 file source 所在系统的ip wuse ...

  5. 初学习-python打印乘法表、正方形、三角形

    for x in range(1,4): for o in range(0,x-1): print('*',end='') pass pass print('*') print('\n')print( ...

  6. <2013 12 17> 专业技能

    Specialties: • Mechanical design modeling using Pro/ENGINEER and SolidWorks.• Robot control, path pl ...

  7. Django 之 权限系统(组件)

    参考: http://www.cnblogs.com/yuanchenqi/articles/7609586.html

  8. javascript实例:两种方式实现tab栏选项卡

    方法1: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  9. HDU1950-Bridging signals-最长上升子序列

    Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. ...

  10. sql语句备份/导入 mysql数据库或表命令

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq1355541448/article/details/30049851