LeetCode笔记:20. Valid Parentheses
来源:互联网 发布:国内可视化软件 编辑:程序博客网 时间:2024/06/05 02:49
问题:
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.
大意:
给出一个只包含 ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ 和 ‘]’ 的字符串,判断它的输入是否是有效的。
括号必须是以正确的顺序关闭的, “()” 和 “()[]{}” 都是有效的,但是 “(]” 和 “([)]” 是无效的。
思路:
题目的要求说来也简单,就是判断括号是不是有效的,自己先用测试用例试了一下,括号中包含括号也是有效的。
其实无效的情况也就几种,左括号匹配到了不一样的右括号、左括号多了、右括号多了,我用一个数组记录不同位置出现的括号的种类,出现新的右括号的时候判断是否匹配到了正确的括号,还要看是不是是多了的右括号,最后看有没有多出来的左括号。方法很笨,代码也写的很冗余,不过速度还可以。
代码(Java):
public class Solution { public boolean isValid(String s) { int[] markArr = new int[s.length()];// 1表示(),2表示[],3表示{} char[] arr = s.toCharArray(); for (int i = 0; i < arr.length; i++) { if (arr[i] == '(') { markArr[i] = 1; } else if (arr[i] == ')') { boolean hasMatched = false; for (int j = i-1; j >= 0; j--) { if (markArr[j] > 0 && markArr[j] != 1) return false; else if (markArr[j] == 1) { markArr[j] = 0; hasMatched = true; break; } } if (!hasMatched) return false; } else if (arr[i] == '[') { markArr[i] = 2; } else if (arr[i] == ']') { boolean hasMatched = false; for (int j = i-1; j >= 0; j--) { if (markArr[j] > 0 && markArr[j] != 2) return false; else if (markArr[j] == 2) { markArr[j] = 0; hasMatched = true; break; } } if (!hasMatched) return false; } else if (arr[i] == '{') { markArr[i] = 3; } else if (arr[i] == '}') { boolean hasMatched = false; for (int j = i-1; j >= 0; j--) { if (markArr[j] > 0 && markArr[j] != 3) return false; else if (markArr[j] == 3) { markArr[j] = 0; hasMatched = true; break; } } if (!hasMatched) return false; } } for (int i = 0; i < markArr.length; i++) { if (markArr[i] > 0) return false; } return true; }}
他山之石:
public class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<Character>(); // Iterate through string until empty for(int i = 0; i<s.length(); i++) { // Push any open parentheses onto stack if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') stack.push(s.charAt(i)); // Check stack for corresponding closing parentheses, false if not valid else if(s.charAt(i) == ')' && !stack.empty() && stack.peek() == '(') stack.pop(); else if(s.charAt(i) == ']' && !stack.empty() && stack.peek() == '[') stack.pop(); else if(s.charAt(i) == '}' && !stack.empty() && stack.peek() == '{') stack.pop(); else return false; } // return true if no open parentheses left in stack return stack.empty(); }}
这个做法用到了Stack栈这个类型,确实这道题很适合用栈来做,先进后出,遇到左括号的时候放进去,遇到右括号的时候从栈顶拿括号进行匹配,匹配失败就错了,全部匹配正确而且最后栈里没东西了就对了。代码简洁多了。
合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_
- LeetCode笔记:20. Valid Parentheses
- leetcode笔记:Valid Parentheses
- leetcode笔记--Valid Parentheses
- 小白笔记--------------------------leetcode(20. Valid Parentheses )
- LeetCode 20. Valid Parentheses
- [LeetCode] 20.Valid Parentheses
- LeetCode --- 20. Valid Parentheses
- LeetCode 20.Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- [leetcode] 20.Valid Parentheses
- [leetcode] 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- leetcode 20. Valid Parentheses
- 【LeetCode】20. Valid Parentheses
- leetcode 20. Valid Parentheses
- leetcode 20. Valid Parentheses
- Deep Reinforcement Learning for Dialogue Generation
- 备份远程数据库Code
- LeetCode —— 412. Fizz Buzz
- 软碟通制作u盘启动
- adb 一些常用的命令
- LeetCode笔记:20. Valid Parentheses
- emmet安装
- Windows系统盘盘符修改
- vmware vsphere vcenter6.0安装后注意事项
- H264码流打包成RTP包
- C,C++,VC++,MFC之间有什么区别和联系
- python自动化之路-day5.1
- CGI与JSP联系与区别
- clean clean