javascript实现队列和栈
数组实现队列
unshift()在数组第一个前面添加一个元素
pop()在数组最后面删除一个元素
数组实现栈
push()在数组最后面增加一个元素
pop()在数组最后面删除一个元素
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
题目如下:
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
奇奇怪怪,这个题整个就无语到了,就不该用js写,用数组仿队列,我用两个队列,然后第二个队列做备份,用for循环超出时间限制了。。。
那不如直接用数组的slice,splice。。。
直接用一个队列就可以完成了,这个时间到底是怎么弄的,奇奇怪怪,哭泣
var MyStack = function() {
this.queue1 = []
this.queue2 = []
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
// console.log(queue1.push(x))
this.queue1.unshift(x)
// return this.queue1;
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
// var temp;
// while(this.queue1.length>1){
// temp = this.queue1.pop()
// this.queue2.unshift(temp)
// }
// temp = this.queue1.pop()
// this.queue1 = this.queue2
// return temp;
return this.queue1.splice(0,1)
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
// var temp
// while(this.queue1.length>1){
// temp = this.queue1.pop()
// this.queue2.unshift(temp)
// // return 0;
// }
// temp = this.queue1.pop()
// // this.queue2.unshift(temp)
// this.queue1 = this.queue2
// // this.queue1 = this.queue2
// return temp;
return this.queue1.slice(0,1)
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
if(this.queue1.length == 0 && this.queue2.length == 0){
return true
}else{
return false
}
};
/**
* Your MyStack object will be instantiated and called as such:
* var obj = new MyStack()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/
题目
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
示例 1:
输入:s = “()”
输出:true
示例 2:
输入:s = “()[]{}”
输出:true
示例 3:
输入:s = “(]”
输出:false
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-parentheses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思考:
首先,如果输入的是左括号,那么就入栈
如果输入的是有括号,先判断栈是否为空,在判断输入的这个括号与栈顶的那个括号是否是匹配的,如果不匹配直接返回false
最后判断栈为空则返回true
这个过程说起来还是很简单的,但是如果没有用对方法,就很容易犯错
1.首先,判断括号是否匹配可以用哈希表,Map([ [ 键,值 ] , [ , ], [ , ] ])
刚开始忘了哈希表的结构,其实很简单,就是键和值组合起来,has()方法判断是否有键,get(键)得到对应的值
2.第二个难点是缺乏逆向思维,总想着if(s[i] == hash.get(arr[arr.length-1]))之后该怎么做,栈不为空改如何,但其实只要栈是空的,或者括号不匹配,就都是返回false,这样就简单多了,也不容易漏掉许多情况。
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
// (([)])
var arr = [];
// for(var i = 0; i < s.length; i++){
// if(arr.length == 0){
// if(s[i] == '}'|| s[i] ==')'|| s[i] ==']'){
// return false
// }
// }else{
// if(s[i] == '('|| s[i] =='{'|| s[i] =='['){
// arr.push(s[i])
// }
// if(arr.length > 0){
// if(s[i] == ')'){
// if(arr.slice(arr.length-1) == '('){
// arr.pop()
// break;
// }
// }
// else if(s[i] == ']'){
// console.log(arr.slice(arr.length-1))
// if(arr.slice(arr.length-1) == '['){
// arr.pop()
// console.log(arr)
// console.log(arr.length)
// // break
// }
// }
// else if(s[i] == '}'){
// console.log(arr)
// if(arr.slice(arr.length-1) == '{'){
// arr.pop()
// console.log(arr)
// break
// }
// }
// }
// }
// if(arr.length == 0){
// return true
// }else{
// return false
// }
// }
if(s.length % 2 != 0){
return false
}
var hash = new Map([
['(',')'],
['[',']'],
['{','}']
])
for(var i = 0; i < s.length; i++){
if(hash.has(s[i])){
arr.push(s[i])
}else{
if(s[i] != hash.get(arr[arr.length-1]) || !arr.length){
return false
}
arr.pop()
}
}
return !arr.length
};