栈实现队列 LeetCode232
- 保存一个输出栈和一个输入栈,push(x) 时将值 x 压入输入栈中;调用 pop()、peek() 时若输出栈为空,则将输入栈的值依次压入输出栈,返回值为输出栈顶元素值(peek() 注意重新压入输出栈);调用 empty() 时输入栈、输出栈均为空时为真
var MyQueue = function() {
this.in = new Array();
this.out = new Array();
};
/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.in.push(x);
};
/**
* @return {number}
*/
MyQueue.prototype.pop = function() {
if(this.out.length === 0) {
while(this.in.length > 0) {
this.out.push(this.in.pop())
}
}
return this.out.pop();
};
/**
* @return {number}
*/
MyQueue.prototype.peek = function() {
if(this.out.length === 0) {
while(this.in.length > 0) {
this.out.push(this.in.pop())
}
}
let top = this.out.pop();
this.out.push(top)
return top
};
/**
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.in.length === 0 && this.out.length === 0;
};
队列实现栈 LeetCode225
- 保存一个栈队列和一个辅助队列,push() 时插入辅助队列并将栈队列的值依次出队并插入辅助队列中,最后交换辅助队列和栈队列;栈队列队头、队尾分别对应栈顶、栈底
var MyStack = function() {
this.stack = new Array()
this.help = new Array()
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.help.push(x)
while(this.stack.length > 0) {
this.help.push(this.stack.shift())
}
let temp = this.stack
this.stack = this.help
this.help = temp
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.stack.shift()
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
return this.stack[0]
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.stack.length === 0
};
n 数之和
-
两数之和 LeetCode1
- 哈希
var twoSum = function(nums, target) { let map = new Map() let find for(let i = 0; i < nums.length; ++i) { find = target - nums[i] if(map.has(find)) { return new Array(map.get(find), i) } map.set(nums[i], i) } };
-
三数之和 LeetCode15
- 排序 + 固定首个数字 + 双指针(左右指针之后大于目标值时右指针左移,小于目标值时左指针右移,等于目标值时右指针左移 + 左指针右移),注意跳过重复值
var threeSum = function(nums) { nums.sort((a, b) => a - b) let pre let res = new Array() let length = nums.length for(let i = 0; i < length - 2; ++i) { if(nums[i] === pre) { continue } else { pre = nums[i] } let left = i + 1, right = length - 1 let target = 0 - nums[i] while(left < right) { let cur = nums[left] + nums[right] if(cur > target) { --right } else if(cur < target) { ++left } else { res.push(new Array(nums[i], nums[left], nums[right])) let prel = nums[left], prer = nums[right] while(left < right && prel === nums[left]) { ++left } while(left < right && prer === nums[right]) { --right } } } } return res };
-
四数之和 LeetCode
- 在三数之和基础上嵌套一层 for 循环
var fourSum = function(nums, target) { nums.sort((a, b) => a - b) let pre1 let res = new Array() let length = nums.length for(let i = 0; i < length - 3; ++i) { let newTarget = target - nums[i] if(nums[i] === pre1) { continue } else { pre1 = nums[i] } let pre2 for(let j = i + 1; j < length - 2; ++j) { if(nums[j] === pre2) { continue } else { pre2 = nums[j] } let left = j + 1, right = length - 1 let newNewTarget = newTarget - nums[j] while(left < right) { let sum = nums[left] + nums[right] if(sum > newNewTarget) { --right } else if(sum < newNewTarget) { ++left } else { res.push(new Array(nums[j], nums[i], nums[left], nums[right])) let prel = nums[left], prer = nums[right] while(left < right && prel === nums[left]) { ++left } while(left < right && prer === nums[right]) { --right } } } } } return res };