423. Valid Parentheses【LintCode java】

Description

Given a string containing just the characters ‘(‘, ‘)‘‘{‘‘}‘‘[‘ and ‘]‘, determine if the input string is valid.

Example

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Challenge

O(n)的时间,n为括号的个数

解题:括号匹配问题,用栈来做较为简单,代码如下:

public class Solution {
    /**
     * @param s: A string
     * @return: whether the string is a valid parentheses
     */
    public boolean isValidParentheses(String s) {
        // write your code here
        Stack<Character>stack = new Stack<Character>();
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == ‘(‘ || s.charAt(i) == ‘{‘ || s.charAt(i) == ‘[‘){
                stack.push(s.charAt(i));
            }else{ //pop前要先检查栈是不是空的
                if(!stack.isEmpty()){
                    switch(s.charAt(i)){
                    case ‘)‘:
                        if(!stack.pop().equals(‘(‘)){
                            return false;
                        }
                        break;
                    case ‘]‘:
                        if(!stack.pop().equals(‘[‘)){
                            return false;
                        }
                        break;
                    case ‘}‘:
                        if(!stack.pop().equals(‘{‘)){
                            return false;
                        }
                        break;
                    }
                }else{
                    return false;
                }
            }
        }
        if(stack.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}

原文地址:https://www.cnblogs.com/phdeblog/p/9330723.html

时间: 2024-10-15 07:41:53

423. Valid Parentheses【LintCode java】的相关文章

389. Valid Sudoku【LintCode java】

Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character .. A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be vali

245. Subtree【LintCode java】

Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of

365. Count 1 in Binary【LintCode java】

Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, return 2 Given 1023, return 9 Challenge If the integer is n bits with m 1 bits. Can you do it in O(m) time? 解题:很简单,但是要考虑范围的问题.代码如下: public

20. Valid Parentheses【leetcode】

20. 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 "(]&quo

LeetCode 32 Longest Valid Parentheses (C,C++,Java,Python)

Problem: 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 exa

158. Valid Anagram【LintCode by java】

Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q

LeetCode 20 Valid Parentheses (C,C++,Java,Python)

Problem: 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 "

【Effective Java】8、优先考虑类型安全的异构容器

有的时候我们一个容器只有一个类型或几个类型并不能满足我们的要求,比如set中存放的元素类型都是同一种,map也就指定的两种 这里我们可以将键进行参数化,而不是将容器参数化,也就是我们可以给容器传一个键的类型,然后value用来放对应的实例,这样就可以存放多个不同的类型了 如: package cn.xf.cp.ch02.item29; import java.util.HashMap; import java.util.Map; public class ManyTypeClass { //一个

【Effective Java】7、优先考虑泛型方法

package cn.xf.cp.ch02.item27; import java.util.HashSet; import java.util.Set; public class Union { /** * 这个方法就会有警告 * @param s1 * @param s2 * @return */ public static Set union1(Set s1, Set s2) { Set result = new HashSet(s1); result.addAll(s2); return