LeetCode: Valid Parentheses 解题报告

Valid Parentheses

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.

SOLUTION1:

使用stack来解决的简单题目。所有的字符依次入栈

1. 遇到成对的括号弹栈,弹栈不成对返回false.

2. 栈为空只能压入左括号

3. 扫描完成时,栈应该为空,否则返回FALSE.

SOLUTION 2:

使用堆栈解决,比较简单。push右括号时,检查左括号在不在,如果不在,返回false,否则弹出一个左括号。

最后看栈是否为空,不为空代表括号数不对称,也要返回false;

 1 public class Solution {
 2     public boolean isValid(String s) {
 3         if (s == null) {
 4             return false;
 5         }
 6
 7         int len = s.length();
 8
 9         // bug 1: don‘t use s as the name of the stack.
10         Stack<Character> stk = new Stack<Character>();
11
12         for (int i = 0; i < len; i++) {
13             char c = s.charAt(i);
14             switch(c) {
15                 case ‘(‘:
16                 case ‘[‘:
17                 case ‘{‘:
18                     stk.push(c);
19                     break;
20                 case ‘)‘:
21                     if (!stk.isEmpty() && stk.peek() == ‘(‘) {
22                         stk.pop();
23                     } else {
24                         return false;
25                     }
26                     break;
27                 case ‘}‘:
28                     if (!stk.isEmpty() && stk.peek() == ‘{‘) {
29                         stk.pop();
30                     } else {
31                         return false;
32                     }
33                     break;
34                 case ‘]‘:
35                     if (!stk.isEmpty() && stk.peek() == ‘[‘) {
36                         stk.pop();
37                     } else {
38                         return false;
39                     }
40                     break;
41             }
42         }
43
44         return stk.isEmpty();
45     }
46 }

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/stack/IsValid.java

时间: 2024-10-15 15:30:37

LeetCode: Valid Parentheses 解题报告的相关文章

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Longest Valid Parentheses 解题报告

[题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

LeetCode: Valid Sudoku 解题报告

Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board

LeetCode: Valid Palindrome 解题报告

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you

LeetCode: Generate Parentheses 解题报告

Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

Valid Parentheses——解题报告

[题目] 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 "([)]

[leetcode]Valid Sudoku 解题报告 C 语言

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.

[leetcode]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont