为什么学算法?
面试的时候多多少少都会问到算法,但是考算法,并不是为了考,而是为了在探讨算法的过程中,考察和验证一个人是否聪明,是否基本功扎实,是否能够顺畅沟通,是否能够快速反应和学习。在讨论和交流的过程中,发现闪光点。比如,面试官可能会问是否知道一些排序算法,那不管是在学校,还是在工作中,还是在平时的阅读中、同事沟通中,都应该可能触及到。如果一无所知,有很大的几率就会挂。然后,可能会问到某个具体的排序算法,比如,快排,这个可能有些人真的不清楚,或是不记得了。其实,没有关系,好的面试官会给你简单的解释,这时候考察的就是你是否学得快理解力强。接下来,面试官可能会问问时间复杂度的问题,不要说不记得,可以根据你的理解进行快速的推理。学习某信;;itspcool
Slogan:“轻松高效学习“,学习某信;;itspcool
所以,考算法作为面试中重要的考察手段,为了考察一个人是否聪明,能否出活,是否主动。聪明是高质量出活的关键因素之一,主动是出活的速度和质量的有力保障。
大厂算法面试题
「极客时间」 用队列实现栈 (easy) 方法1.使用两个 Queue 实现 思路:还是考察栈和队列的熟悉程度,没有什么具体的工程实际意义,可以用两个队列来实现栈的功能,但是一个队列的数据导入另一个队列顺序还是没有改变,所以其中一个队列只是用来做备份的,在代码里queue2就是备份队列,入栈的时候,队列1入队,出栈的时候,如果队列1为空,则交换队列1和队列2,为的是将备份队列的元素全部加入队列1,然后将队列1中除了最后一个元素外全部出队,并且加入备份队列, 复杂度分析:push的时间复杂度为O(1),pop的时间复杂度为O(n)。空间复杂度O(n),其中n是栈内元素的个数,用两个队列来存储 动画过大,学习某信;;itspcool
Js:
var MyStack = function() {
this.queue1 = [];
this.queue2 = [];//备份的队列
};
MyStack.prototype.push = function(x) {
this.queue1.push(x);
};
MyStack.prototype.pop = function() {
// 减少两个队列交换的次数, 只有当queue1为空时,交换两个队列
if(!this.queue1.length) {
[this.queue1, this.queue2] = [this.queue2, this.queue1];
}
while(this.queue1.length > 1) {//当队列1的元素数量大于1的时候不断将元素push进备份队列
this.queue2.push(this.queue1.shift());
}
return this.queue1.shift();//最后将队列1最后一个元素出队
};
MyStack.prototype.top = function() {
const x = this.pop();//查看栈顶,队列出队,然后在push进队列1
this.queue1.push(x);
return x;
};
MyStack.prototype.empty = function() {
return !this.queue1.length && !this.queue2.length;
};
收录于 2021-12-04 18:49:31
查看 6777 次
leetcode 算法面试
大厂算法面试之leetcode精讲18.队列
视频讲解(高效学习):点击学习
目录:
1.开篇介绍
2.时间空间复杂度
3.动态规划
4.贪心
5.二分查找
6.深度优先&广度优先
7.双指针
8.滑动窗口
9.位运算
10.递归&分治
11剪枝&回溯
12.堆
13.单调栈
14.排序算法
15.链表
16.set&map
17.栈
18.队列
19.数组
20.字符串
21.树
22.字典树
23.并查集
24.其他类型题
队列的特点:先进先出(FIFO) 队列的时间复杂度:入队和出队O(1),查找O(n) 优先队列:priorityQueue,按优先级出队,实现 Heap(Binary,Fibonacci...) js里没有队列,但是可以用数组模拟 ds_29
- 算法训练营2022 用队列实现栈 (easy) 方法1.使用两个 Queue 实现 思路:还是考察栈和队列的熟悉程度,没有什么具体的工程实际意义,可以用两个队列来实现栈的功能,但是一个队列的数据导入另一个队列顺序还是没有改变,所以其中一个队列只是用来做备份的,在代码里queue2就是备份队列,入栈的时候,队列1入队,出栈的时候,如果队列1为空,则交换队列1和队列2,为的是将备份队列的元素全部加入队列1,然后将队列1中除了最后一个元素外全部出队,并且加入备份队列, 复杂度分析:push的时间复杂度为O(1),pop的时间复杂度为O(n)。空间复杂度O(n),其中n是栈内元素的个数,用两个队列来存储 动画过大,点击查看
Js:
var MyStack = function() {
this.queue1 = [];
this.queue2 = [];//备份的队列
};
MyStack.prototype.push = function(x) {
this.queue1.push(x);
};
MyStack.prototype.pop = function() {
// 减少两个队列交换的次数, 只有当queue1为空时,交换两个队列
if(!this.queue1.length) {
[this.queue1, this.queue2] = [this.queue2, this.queue1];
}
while(this.queue1.length > 1) {//当队列1的元素数量大于1的时候不断将元素push进备份队列
this.queue2.push(this.queue1.shift());
}
return this.queue1.shift();//最后将队列1最后一个元素出队
};
MyStack.prototype.top = function() {
const x = this.pop();//查看栈顶,队列出队,然后在push进队列1
this.queue1.push(x);
return x;
};
MyStack.prototype.empty = function() {
return !this.queue1.length && !this.queue2.length;
};
Java:
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public void push(int x) {
queue2.offer(x);
while (!queue1.isEmpty()){
queue2.offer(queue1.poll());
}
Queue<Integer> queueTemp;
queueTemp = queue1;
queue1 = queue2;
queue2 = queueTemp;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}
方法2.使用一个 队列 实现 动画过大,点击查看
思路:使用一个 队列 实现,入栈的时候直接push进队列就行,出栈的时候将除了最后一个元素外的元素全部加入到队尾。 复杂度分析:push的时间复杂度为O(1),pop的时间复杂度为O(n),空间复杂度O(n) js:
var MyStack = function() {
this.queue = [];
};
MyStack.prototype.push = function(x) {
this.queue.push(x);
};
MyStack.prototype.pop = function() {
let size = this.queue.length;
while(size-- > 1) {//将除了最后一个元素外的元素全部加入到队尾。
this.queue.push(this.queue.shift());
}
return this.queue.shift();
};
MyStack.prototype.top = function() {
const x = this.pop();//先出栈,然后在加入队列
this.queue.push(x);
return x;
};
MyStack.prototype.empty = function() {
return !this.queue.length;
};
java:
class MyStack {
Deque<Integer> queue1;
public MyStack() {
queue1 = new ArrayDeque<>();
}
public void push(int x) {
queue1.addLast(x);
}
public int pop() {
int size = queue1.size();
size--;
while (size-- > 0) {
queue1.addLast(queue1.peekFirst());
queue1.pollFirst();
}
int res = queue1.pollFirst();
return res;
}
public int top() {
return queue1.peekLast();
}
public boolean empty() {
return queue1.isEmpty();
}
}