232. 用栈实现队列https://leetcode.cn/problems/implement-queue-using-stacks/
var MyQueue = function() {
// 用两个栈来实现队列
// 一个栈为入栈
// 一个栈为出栈
// 需要注意的是如果出栈里面没有元素了,将入栈里面的元素压进去;
this.inStack = [];
this.outStack = [];
};
/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.inStack.push(x);
};
/**
* @return {number}
*/
MyQueue.prototype.pop = function() {
if(!this.outStack.length){
this.intoout();
}
return this.outStack.pop();
};
/**
* @return {number}
*/
MyQueue.prototype.peek = function() {
if(!this.outStack.length){
this.intoout();
}
return this.outStack[this.outStack.length-1];
};
/**
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.inStack.length===0 && this.outStack.length===0;
};
MyQueue.prototype.intoout = function(){
while(this.inStack.length){
this.outStack.push(this.inStack.pop());
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/
225. 用队列实现栈https://leetcode.cn/problems/implement-stack-using-queues/
var MyStack = function() {
// 用两个队列实现栈,只要入队列有数,就得赋值到出队列里面;
this.inlist = [];
this.outlist = [];
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.inlist.push(x);
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
if(this.inlist.length){
while(this.inlist.length){
this.outlist.push(this.inlist.shift());
}
}
return this.outlist.pop();
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
if(this.inlist.length){
while(this.inlist.length){
this.outlist.push(this.inlist.shift());
}
}
return this.outlist[this.outlist.length-1];
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.inlist.length===0 && this.outlist.length===0
};
/**
* 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()
*/
var MyStack = function() {
// 用一个队列实现
this.list = [];
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.list.push(x);
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.list.pop();
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
return this.list[this.list.length-1];
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.list.length===0;
};
20. 有效的括号https://leetcode.cn/problems/valid-parentheses/
// 奇数的话直接返回
if(s.length%2 !== 0){
return false;
}
const map = new Map([
[']', '['],
[')', '('],
['}','{']
]);
console.log(map);
let stack = [];
for(let i = 0;i<s.length;i++){
if(map.has(s[i])){
if(!stack.length || map.get(s[i]) !== stack[stack.length-1]){
return false;
}
stack.pop();
}else{
stack.push(s[i]);
}
}
return !stack.length;
1047. 删除字符串中的所有相邻重复项https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
//利用栈,如果现在不是在做栈的专题,我还是想不到呜呜
let stack = [];
for(let i = 0;i<s.length;i++){
if(stack[stack.length-1] !== s[i]){
stack.push(s[i]);
}else{
stack.pop();
}
}
return stack.join("");
150. 逆波兰表达式求值https://leetcode.cn/problems/evaluate-reverse-polish-notation/
//要注意是哪个数-哪个数,哪个数/哪个数,除出来是负数的情况
let stack = [];
for(let i =0;i<tokens.length;i++){
if(tokens[i] === '+'){
let num1 = stack.pop();
let num2 = stack.pop();
stack.push(num1 + num2);
}else if(tokens[i] === '-'){
let num1 = stack.pop();
let num2 = stack.pop();
stack.push(num2 - num1);
}else if(tokens[i] === '*'){
let num1 = stack.pop();
let num2 = stack.pop();
stack.push(num1 * num2);
}else if(tokens[i] === '/'){
let num1 = stack.pop();
let num2 = stack.pop();
stack.push(num2 / num1 > 0 ? Math.floor(num2 / num1) : Math.ceil(num2/ num1));
}else{
stack.push(tokens[i] - '0');
}
}
return stack.pop();
347. 前 K 个高频元素https://leetcode.cn/problems/top-k-frequent-elements/
//(1)哈希表+sort,好优秀的方法!!!
// let map = new Map();
// for(let i =0;i<nums.length;i++){
// if(map.has(nums[i])){
// map.set(nums[i], map.get(nums[i]) + 1);
// }else{
// map.set(nums[i], 1);
// }
// }
// // 转化成数组,并按第二参数排序;
// let arr = Array.from(map).sort((a,b)=>{
// return b[1] - a[1];
// })
// // 截取前k个数组的第一项; .map方法返回数组对象中的某一项的属性值,并组成一个新数组
// return arr.slice(0, k).map(n=>n[0]);
// (2)哈希表+桶排序
let map = new Map();
for(let i =0;i<nums.length;i++){
if(map.has(nums[i])){
map.set(nums[i], map.get(nums[i]) + 1);
}else{
map.set(nums[i], 1);
}
}
// 桶排序;
// arr[i] 对应存放频率为i的数组
let arr = [];
map.forEach((value, key)=>{
if(!arr[value]){
arr[value] = [key];
}else{
arr[value].push(key);
}
})
let res = [];
for(let i = arr.length - 1; i >= 0 && res.length < k; i--) {
if(arr[i]) {
res.push(...arr[i]);//将数组转换为用逗号分割的参数序列
}
}
return res;
entries()方法有点忘记了;
map转化成数组 Array.from(map);
.map(n=>n.name):返回数组对象中某一项的属性值,并合并成新数组;
桶排序;
两个栈实现队列,一个入栈,一个出栈,当入栈为空时,将入栈的值全部赋给出栈的;
一个/两个队列实现栈;