LEETCODE 20. Valid Parentheses《越努力越幸运》

it2024-12-29  13

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

An input string is valid if:

Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order.

Example 1:

Input: s = “()” Output: true Example 2:

Input: s = “()[]{}” Output: true Example 3:

Input: s = “(]” Output: false Example 4:

Input: s = “([)]” Output: false Example 5:

Input: s = “{[]}” Output: true

bool isValid(string s) { int size=s.size(); if (s.size() % 2 != 0) return false; //就近寻找原则 while (s.size()) { for (int i = 0; i < s.size() - 1; i++) { if(s[i]+1==s[i+1]||s[i]+2==s[i+1]) { s.erase(i,2); break; } } int s1=s.size(); if(size>s1){ size=s1; } else{ return false; } } return true; }

最新回复(0)