- 本次 系列内容 将围绕算法 相关的内容, 并且持续下去,当前目标,先刷完 剑指 offer相关内容
* 栈和队列
1、剑指 Offer 09. 用两个栈实现队列
题目描述
代码
var CQueue = function() {
this.stackA = []
this.stackB = []
};
/**
* @param {number} value
* @return {void}
*/
CQueue.prototype.appendTail = function(value) {
this.stackA.push(value)
};
/**
* @return {number}
*/
CQueue.prototype.deleteHead = function() {
if(this.stackB.length){
return this.stackB.pop()
}else{
while(this.stackA.length){
this.stackB.push(this.stackA.pop())
}
if(!this.stackB.length){
return -1
}else{
return this.stackB.pop()
}
}
};
2、剑指 Offer 30. 包含min函数的栈
题目描述
* 代码 展示
var MinStack = function() {
this.x_stack = []
this.min_stack = [Infinity]
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.x_stack.push(x)
// 将比较小的 放入min_stack 中
this.min_stack.push(Math.min(this.min_stack[this.min_stack.length - 1], x))
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
this.x_stack.pop()
this.min_stack.pop()
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.x_stack[this.x_stack.length - 1]
};
/**
* @return {number}
*/
MinStack.prototype.min = function() {
return this.min_stack[this.min_stack.length - 1]
};
* 链表
3、剑指 Offer 06. 从尾到头打印链表
-
题目描述 -
代码展示
4、剑指 Offer 24. 反转链表
题目 描述
代码展示
5、剑指 Offer 35. 复杂链表的复制
题目 描述
代码展示
时间复杂度 O(n) 空间复杂度 O(1)
* 字符串
6、剑指 Offer 05. 替换空格
- 题目
- 代码
复杂性分析:
- 时间复杂度:O(n)。遍历字符串 s 一遍。
- 空间复杂度:O(n)。额外创建字符数组
7、剑指 Offer 58 - II. 左旋转字符串
- 题目
- 代码
- 查找算法
8、剑指 Offer 03. 数组中重复的数字
*题目
- 代码
9、剑指 Offer 53 - I. 在排序数组中查找数字
- 题目
- 代码
10、剑指 Offer 53 - II. 0~n-1中缺失的数字
- 题目
- 代码