【leetcode】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.

【My Solution:】

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    bool checkMatch(char first,char second){
        bool isMatch = false;
        if((first == ‘(‘ && second == ‘)‘)
            || (first == ‘[‘ && second == ‘]‘)
            || (first == ‘{‘ && second == ‘}‘))
        {
            isMatch = true;
        }
        return isMatch;
    }

    bool isValid(string s) {
        bool isValid = false;
        vector<char> vTemp;
        if(s.length() <= 0){
            return isValid;
        }
        //vTemp.push_back(s[0]);
        for(int i = 0; i < s.length(); i++){
            if(vTemp.size() == 0){
                vTemp.push_back(s[i]);
            }else{
                vector<char>::iterator id = vTemp.end()-1;
                if(checkMatch((*id),s[i])){
                    vTemp.erase(id);
                }else{
                    vTemp.push_back(s[i]);
                }
            }
        }
        if(vTemp.size() == 0){
            isValid = true;
        }
        return isValid;
    }
};

int main(){
    Solution solution;
    solution.isValid("()");
    solution.isValid("()[]{}");
    solution.isValid("([)]");
    return 0;
}

【other‘s】算法使用相同,都使用栈来实现,但his更简洁,1、直接用到了已有的数据结构stack;2、匹配用到了ASCII码,但这样不合适,万一输入为“sdsf”等字符,则结果不正确。

public class Solution {
public boolean isValid(String s) {
    char [] arr = s.toCharArray();
    Stack stack = new Stack();
    for(char ch : arr){
        if(stack.isEmpty()){
            stack.push(ch);
        }else{
            char top = (char)stack.lastElement();
            if(ch - top == 1 || ch - top == 2){
                stack.pop();
            }else{
                stack.push(ch);
            }
        }
    }
    if(stack.isEmpty()){
        return true;
    }
    return false;
}
时间: 2024-12-23 20:00:08

【leetcode】Valid Parentheses的相关文章

【LeetCode】- 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 "

【LeetCode】Valid Parentheses合法括号

给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]([]({}))" 是合法的,而"(]"."([)]" 是不合法的. 使用栈stack C++实现: 1 bool isValid(string s) { 2 stack<char> stack; 3 for (char &c : s) {

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Generate Parentheses 解题报告

[题目] Given 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 解题报告

[题目] Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambig

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

【Leetcode】Valid Palindrome

题目链接:https://leetcode.com/problems/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 ca

【LeetCode】Valid Sudoku

Valid Sudoku 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 '.'. A partially filled sudoku which is valid. Note:A valid Sudoku boar

【leetcode】Valid Number

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