LeetCode_Valid Parentheses
来源:互联网 发布:淘宝上下架查询 编辑:程序博客网 时间:2024/05/19 17:56
题目如下:
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
分析:这是关于栈的操作,可以遍历该String字符串,如果碰到左括号,则将该字符入栈,并将指针上移一位,如果碰到右括号,则比较此时的指针所指向的括号类型是否和该右括号类型匹配,如匹配,则出栈(指针下移一位),如不匹配,则直接返回false,循环完毕后,如果指针此时没有指向栈底,则返回false,否则true。
Java解题:
public static isValid(String s) {char[] stack = new char[s.length()+1];char[] ch = s.toCharArray();int point=0;for(int i=0;i<ch.length;i++){if(ch[i]=='('||ch[i]=='['||ch[i]=='{'){stack[++point]=ch[i];}else if(ch[i]==')'){if(stack[point]=='(')point--;elsereturn false;}else if(ch[i]==']'){if(stack[point]=='[')point--;elsereturn false;}else if(ch[i]=='}'){if(stack[point]=='{')point--;elsereturn false;}}if(point==0)return true;return false;}
0 0
- Leetcode_valid-parentheses
- leetcode_Valid Parentheses
- LeetCode_Valid Parentheses
- Leetcode_valid-palindrome
- Leetcode_valid-sudoku
- Leetcode_Valid Palindrome
- LeetCode_Valid Sudoku
- leetcode_Valid Palindrome
- leetcode_Valid Number
- leetcode_Valid Sudoku
- leetcode_Valid Anagram
- leetcode_Valid Anagram_easy
- LeetCode_valid-anagram
- leetCode_Valid Number
- leetcode_Valid Perfect Square
- Leetcode_valid-number(updated c++ and python version)
- leetcode_Valid Sudoku and Sudoku Solver (数独游戏) _easy
- Generate Parentheses & Valid Parentheses
- js算法:动态规划-金矿模型与买书问题(附js源码)
- MapReduce实现大矩阵乘法
- UISearchBar修改输入框,取消按钮样式
- Android学习之ActionBar
- PHP 最佳实践
- LeetCode_Valid Parentheses
- oracle层次查询
- HDU1233 - 还是畅通工程 最小生成树,用了三种姿势AC
- 如何在ubuntu下设置永久的alias别名
- [转载文章] 在线支付逻辑漏洞总结
- 【Howie玩docker】-Docker常用命令操作
- [转]SkGradientShader::CreateLinear
- js的indexOf方法IE不兼容问题
- css3中单位px,em,rem,vh,vw,vmin,vmax的区别及浏览器支持情况