- 浏览: 183489 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
Subscribe to see which companies asked this question
这道题目比较简单,用堆栈来处理,当遇到‘(’, ‘{’, ‘[’ 时就push到堆栈中,当遇到‘)’, ‘}’, ‘]’时,首先判断堆栈是否为空,如果为空说明之前字符中没有与right braces匹配的,返回false;如果堆栈不为空,判断是否匹配,如果匹配就继续往下匹配,如果不匹配直接返回false;最后要检查堆栈是否为空。代码如下:
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Subscribe to see which companies asked this question
这道题目比较简单,用堆栈来处理,当遇到‘(’, ‘{’, ‘[’ 时就push到堆栈中,当遇到‘)’, ‘}’, ‘]’时,首先判断堆栈是否为空,如果为空说明之前字符中没有与right braces匹配的,返回false;如果堆栈不为空,判断是否匹配,如果匹配就继续往下匹配,如果不匹配直接返回false;最后要检查堆栈是否为空。代码如下:
public class Solution { public boolean isValid(String s) { if(s == null || s.length() == 0) return true; Stack<Character> stack = new Stack<Character>(); for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(c == '(' || c == '[' || c == '{') { stack.push(c); } else { if(stack.isEmpty()) return false; char tem = stack.pop(); if(c == ')' && tem == '(') continue; if(c == '}' && tem == '{') continue; if(c == ']' && tem == '[') continue; return false; } } return stack.isEmpty() == true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Longest Valid Parentheses.java
java java_leetcode题解之Maximum Nesting Depth of Two Valid Parentheses
第四章 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 有效括号 给定一个只包含字符'(' , ')' , '{' , '}' , '['和']'的字符串,确定输入字符串是否有效。 输入字符串在以下情况下有效: * 左括号必须由相同类型的括号封闭。...类别:堆栈、序列处理
c c语言_leetcode 0020_valid_parentheses.zip
其中,“Valid Parentheses”是一道经典的字符串处理问题。这个题目要求我们编写一个函数,检查给定的字符串中括号的使用是否合法。具体来说,我们需要确保所有打开的括号都有对应的闭合括号,并且它们的顺序是正确...
js js_leetcode题解之20-valid-parentheses.js
c语言入门 C语言_leetcode题解之20-valid-parentheses.c
js js_leetcode题解之32-longest-valid-parentheses.js
c语言入门 C语言_leetcode题解之32-longest-valid-parentheses.c
1. 有效的括号(Valid Parentheses):给定一个字符串,判断括号是否匹配,使用栈来解决。难度:简单 2. 删除最外层的括号(Remove Outermost Parentheses):给定一个字符串,删除最外层的括号,使用栈来解决。难度...
2. **Longest Valid Parentheses**:找到有效括号字符串中的最长连续子串长度,同样利用栈记录括号状态来计算最长长度。 3. **Evaluate Reverse Polish Notation**:逆波兰表达式求值,使用栈处理运算符和操作数,...
如果所有括号都正确配对,且队列为空,那么程序将输出"Valid parentheses."。 在实际项目中,你可能还需要考虑更复杂的情况,例如处理嵌套的括号结构或者支持更多类型的运算符。同时,为了提高代码的可读性和可维护...
* 有效的括号(Valid Parentheses):判断括号是否有效。 * 生成括号(Generate Parentheses):生成所有可能的括号组合。 6. 字符串匹配: * 正则表达式匹配(Regular Expression Matching):实现正则表达式...
这道面试题解是针对LeetCode中的第20题——"有效的括号"(Valid Parentheses),它常在求职面试中出现,特别是对于Python开发者而言。该题目的目标是检查一个由括号组成的字符串是否有效,即是否存在一种闭合的顺序...
合并排序数组, Valid Parentheses, 实现 strStr(), Set Matrix Zeroes, 搜索插入位置, Longest Consecutive Sequence, Valid Palindrome, 螺旋矩阵, 搜索一个二维矩阵, 旋转图像, 三角形, Distinct Subsequences ...
在本资源包中,主题聚焦于C语言的基础学习,特别是针对LeetCode编程挑战中的第32题——"最长有效括号"(Longest ValidParentheses)的解法。LeetCode是一个在线平台,提供了各种算法问题,旨在提升程序员的编程技能...
Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last...