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中有Strack类,所以直接用就可以了。
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
这是一道匹配题,最简单的方式就是用栈来做了,JAVA中有Strack类,所以直接用就可以了。
public boolean isValid(String s) { boolean result = false; List<Character> ls = new ArrayList<Character>(); if(s == null || s.length()<2){ return false; } char[] chs = {'(',')','[',']','{','}'}; for(char c : chs){ ls.add(c); } Stack<Character> st = new Stack<Character>(); for(int i = 0 ;i<s.length();i++){ if(ls.contains(s.charAt(i))){ if(st.isEmpty()){ st.push(s.charAt(i)); }else{ /*System.out.println((char)st.peek()); System.out.println(s.charAt(i)); System.out.println((char)st.peek()+s.charAt(i));*/ if((char)st.peek()+s.charAt(i) == 81 || (char)st.peek()+s.charAt(i) == 248 ||(char)st.peek()+s.charAt(i) == 184){ st.pop(); }else{ st.push(s.charAt(i)); } } } } if(st.isEmpty()){ result = true; }else{ result = false; } return result; }
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 327Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 365Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Remove Nth Node From End of List
2015-03-05 22:31 340Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 332Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 312Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 292Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 319Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 328Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 328Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 289Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 323Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 367Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 332Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 343Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 227Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 255The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 334Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 300You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 425[size=24px;]Longest Substring W ...
相关推荐
第四章 Leetcode 题解 ...20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli
leetcode 2 有效括号 给定一个只包含字符'(' , ')' , '{' , '}' , '['和']'的字符串,确定输入字符串是否有效。 输入字符串在以下情况下有效: * 左括号必须由相同类型的括号封闭。...类别:堆栈、序列处理
Longest Valid Parentheses Largest Rectangle in Histogram Evaluate Reverse Polish Notation Implement Stack using Queues 队列 Implement Queue using Stacks 二叉树 二叉树的遍历 Binary Tree Preorder ...
Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last...
java lru leetcode ...Parentheses](./leetcode/动态规划-Longest Valid Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./leetcode/动态规划-Maximum Length of Repeated Subar
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** ...Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to ...
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 ...Valid Parentheses 简单 堆叠 重要 Merge Two Sorted Lists 简单 链结串列 重要 Remove Duplicates from
加油站 leetcode leetcode_java prepare for jobhunting. java version of Leetcode. easy 55 about takes 5 days medium 112 about takes 20 days hard 48 about ...1.Valid Parentheses 2.Maximum
Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock...
Parentheses 有效的括号 Stack / 栈 用栈实现配对 Daily Challenge 2020/08/14 28 Implement strStr() 实现 strStr() String / 字符串 循环遍历即可 algorithm-pattern: quick start 43 Multiply S
java lru leetcode :ice_cream: LeetCode ...Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
Parentheses Easy 94 二叉树的中序遍历 Binary Tree Inorder Medium 155 最小栈 Min Stack Easy 394 字符串编码 Decode String Medium 链表 No Problem Solution Difficulty Tag 2 两数相加 Add Two Numbers Medium ...
leetcode 跳跃 LeetCode 力扣刷题70道! 数组 Array 力扣 485 最大连续1的个数 | Max Consecutive ...Parentheses 力扣 496 下一个更大的元素 | Next Greater Element I 力扣 232 用栈实现队列 | Implement
lru cache leetcode #算法解题报告 主要记录我每天做的题目,包括leetcode, 剑指offer等在线编程平台,以前做过的等时间够再一起分享。 使用swift解题 30-Day ...Parentheses 21.Merge Two Sorted L
Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 毫秒内存使用:22.5 MB 26. Remove Duplicates from Sorted Array 运行时间:100 毫秒内存使用:15.2 MB 27. Remove...
LeetCode字符串换行 Solve_Leetcode 复习的过程,来刷刷题,坚持坚持坚持! 逆波兰表达式 题目如下: 使用了 class Stack A more complete and consistent ...Valid Parentheses 题目如下: Stack泛型
Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 18 3 sum 扩展版, 外层多套一个循环即可。注意判断重复及细节优化 19 细节:nodelist前插入一个dummy node. 删除倒数第n 等于正数查到len ...