• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

Valid Parentheses

我的解法

我的第一次提交

介绍

介绍,介绍个锤子,又不难!

思路

创建栈堆用来存储: (, [, {

遍历原始字符串

如果是(, [, {, 就入栈堆

如果是), 就检查栈顶元素是否为(, 如果是就出栈

如果是], 同2

如果是}, 同2

检查指针我=数组长度?和堆栈=0?如果都为没错,返回结果真实的

代码

/*

* @Author: fox

* @ date : 2022-05-03 08:44:53

* @ LastEditors: fox

* @ lastedittime : 2022-05-03 10:32:41

* @描述: https://leetcode.com/problems/valid-parentheses/

*/

/**

* @描述:运行时: 68.92%内存使用量: 56.34%

* @param {string} s

* @return {boolean}

*/

常量括号={

')' : '(',

']' : '[',

'}' : '{'

}

const isValid=(s,obj=括号)={

常量堆栈=[] //栈

让我;//指针

如果(!s || s .长度2)返回错误的

for(I=0;长度;i ) {

if(s==='(' | | s==='[' | | s===' { '){

stack.push(s)

} else if(obj。hasownproperty(s)stack[stack。length-1]===obj[s]){

stack.pop()

}否则{

打破;

}

}

返回I===s . length栈。长度===0

};

让我们='()'

控制台。log(有效))//true

s='()[]{} '

控制台。log(有效))//true

s='(]'

控制台。log(有效))//false

s='['

控制台。log(有效))//false

Link to comment
Share on other sites