- 浏览: 19074 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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.
解题思路
本题考查的是判断一个字符串是否为正确括号的使用,即括号要成对使用,并且使用的前后顺序要正确。
相关知识点
(1)Java中的栈
自己的代码
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.
解题思路
本题考查的是判断一个字符串是否为正确括号的使用,即括号要成对使用,并且使用的前后顺序要正确。
相关知识点
(1)Java中的栈
boolean empty();//测试堆栈是否为空。 E peek();//查看堆栈顶部的对象,但不从堆栈中移除它。 E pop();//移除堆栈顶部的对象,并作为此函数的值返回该对象。 E push(E item);//把项压入堆栈顶部。
自己的代码
package leetcode; import java.util.Stack; public class ValidParentheses { public boolean isValid(String s) { if(s == null) return false; if(s.length() == 0) return false; if(s.length()%2 != 0) return false; boolean isValided = true; Stack<Character> stack = new Stack<Character>(); for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); if(c == '(') stack.add(')'); else if(c == '[') stack.add(']'); else if(c== '{') stack.add('}'); else if(c == ')' || c == ']' || c == '}'){ if(stack.empty() || stack.pop() != c){ isValided = false; break; } } else{ isValided = false; break; } } if(!stack.empty()) isValided = false; return isValided; } public static void main(String[] args) { //String s = null; //String s = ""; //String s = " "; //String s = " "; //String s = "()"; //String s = "[]"; //String s = "{}"; //String s = "(]"; //String s = "([)]"; //String s = "([])"; String s = "(("; ValidParentheses vp = new ValidParentheses(); System.out.println(vp.isValid(s)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 398题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 284题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 420题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 301题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 327题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 346题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 355题目描述 Determine if a Sudoku is v ... -
Palindrome Number
2014-10-21 09:41 344题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 357题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 307题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 255题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 292题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 232题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 292题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 255题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 317题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 336题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 397题目描述 Given two sorted integer a ...
相关推荐
第四章 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 ...