-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
图解腾讯&哔哩哔哩&leetcode20:有效的括号 #25
Comments
|
方法: |
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
const brackets = []
if (s % 2) return false
for (const i of s) {
switch(i) {
case '{':
brackets.push(i)
break
case '[':
brackets.push(i)
break
case '(':
brackets.push(i)
break
case '}':
if (brackets.pop() !== '{') return false
break
case ']':
if (brackets.pop() !== '[') return false
break
case ')':
if (brackets.pop() !== '(') return false
break
}
}
return brackets.length === 0
}; |
解答:利用栈结构解题思路: 将字符串中的字符依次入栈,遍历字符依次判断:
当遍历完成时,所有已匹配的字符都已匹配出栈,如果此时栈为空,则字符串有效,如果栈不为空,说明字符串中还有未匹配的字符,字符串无效 画图帮助理解一下: 代码实现: const isValid = function(s) {
let map = {
'{': '}',
'(': ')',
'[': ']'
}
let stack = []
for(let i = 0; i < s.length ; i++) {
if(map[s[i]]) {
stack.push(s[i])
} else if(s[i] !== map[stack.pop()]){
return false
}
}
return stack.length === 0
}; 时间复杂度:O(n) 空间复杂度:O(n) |
/**
* 有效的括号
*
* 要求:
给定一个只包括 '(' ,')' ,'{' ,'}' ,'[' ,']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
*/
function validBrackets(str) {
if (typeof str !== 'string') return false;
if (str === '') return true;
const matches = ['()', '[]', '{}'];
const leftStr = replaceMatch(str);
// 匹配并替换,直到不能匹配
function replaceMatch(str) {
const matchItem = matches.find((i) => str.includes(i));
if (matchItem) {
str = str.replace(matchItem, '');
if (str.length) str = replaceMatch(str);
}
return str;
}
if (leftStr.length !== 0) console.log('leftStr: ', leftStr);
// 匹配完 leftStr === '' 则是括号正确
return leftStr.length === 0;
}
const v = validBrackets;
console.log('(): ', v('()')); // true
console.log('()[]{}: ', v('()[]{}')); // true
console.log('(]: ', v('(]')); // false
console.log('([)]: ', v('([)]')); // false
console.log('{[]}: ', v('{[]}')); // true
console.log('{[}](): ', v('{[}]()')); // false
console.log('(){[}][]: ', v('(){[}][]')); // false
console.log(')(][: ', v(')(][')); // false
console.log('{[({[]})]}: ', v('{[({[]})]}')); // false
console.log('{[({[()]})]: ', v('{[({[()]})]')); // false
console.log('haha: ', v('haha')); // false |
var isValid = function(s) {
const charMap = {
'(': ')',
'[': ']',
'{': '}',
}
const charStack = []
for (let i = 0, len = s.length; i < len; i++) {
const char = s.charAt(i)
const hitChar = charMap[ char ]
if (hitChar) {
charStack.push(hitChar)
} else if (charStack.pop() !== char) {
return false
}
}
return charStack.length === 0
};
|
作了套面试题,我的解法如下 题目:
} |
var isValid = function (s) {
let map = new Map();
map.set('}', '{');
map.set(')', '(');
map.set(']', '[');
let right = ['}', ')', ']'];
let tmpStack = new Array();
for (let i = 0; i < s.length; i++) {
if(right.indexOf(s[i]) === -1){
tmpStack.push(s[i]);
} else {
let tmp = tmpStack.pop();
if(tmp !== map.get(s[i])) return false;
}
}
return tmpStack.length === 0;
}; |
|
利用栈保存读取到的字符,判断是否为有效,只需判断当出现反向符号的时候,栈顶符号一定是对应的正向符号,如果是,就推出栈顶符号,继续遍历,如果不是直接返回false, const isValid = (source: string) => {
const len = source.length;
if (len <= 0) return false;
const CharMap: Record<string, string> = {
')': '(',
'}': '{',
']': '[',
};
const stack: string[] = [ source[0] ];
for (let i = 1; i < len; i++) {
const char = source[i];
if (!CharMap[char]) {
stack.push(char);
continue;
}
// 出现反向符号,且反向符号对应的正向符号 !== 栈顶的符号,肯定不符合规则直接返回false
if (CharMap[char] !== stack[stack.length - 1]) return false;
stack.pop();
}
return stack.length <= 0;
}; |
var isValid = function(s) {
s= s.split('')
let map = new Map([['(', ')'], ['{', '}'], ['[', ']']])
let stack = []
for (let i=0;i<s.length;i++) {
if (map.get(s[i])) {
stack.push(s[i])
} else {
if (s[i] !== map.get(stack.pop())) return false
}
}
return !stack.length
}; |
let mapBrackets = {
"(":")",
"[":"]",
"{":"}",
}
function validBrackets(str){
let stack = []
for(let i = 0;i<str.length;i++){
let c = str.charAt(i)
if(['(','{','['].includes(c)){
stack.push(mapBrackets[c])
}else if(!stack.length||stack.pop()!=c){
return false
}
}
return !stack.length
}
console.log(validBrackets("()"))
console.log(validBrackets("()[]{}"))
console.log(validBrackets("(]"))
console.log(validBrackets("([)]"))
console.log(validBrackets("{[]}")) |
给定一个只包括
'('
,')'
,'{'
,'}'
,'['
,']'
的字符串,判断字符串是否有效。有效字符串需满足:
注意空字符串可被认为是有效字符串。
示例 1:
示例 2:
示例 3:
示例 4:
示例 5:
附赠leetcode:leetcode
The text was updated successfully, but these errors were encountered: