文章目录
引言
- 今天又要面试字节,不过这次没有上次那么紧张了,平常心,然后去面对就行了,行就行,不行也没有办法撒!反正得好好准备提前批还有秋招的正式批!好好准备吧!
- 今天多复习几题,整体来说,复习还是挺快的!
- 对了,今天还得整理一下MySQL中关于锁的相关内容,背八股的时候,总是有点疑惑,今天全过一遍!
复习
每日温度
复习实现
- 我记得这道题我是做出来了,然后当时方法和参考方法不同,但是思想大致是相同的,应该要维护两个栈。我是从前往后进行遍历,他是从后往前进行遍历。
- 整体来说还是很好做的,两个判定情况
- 当前元素比栈顶元素大,栈内元素弹出,直到一个比他本身大的元素,
- 当前元素比栈顶元素小,直接入栈。
- 从后往前进行遍历
class Solution {
public int[] dailyTemperatures(int[] tempe) {
// define the result array
int m = tempe.length;
int[] res = new int[m];
// define the compare stack
Deque<Integer> stk = new LinkedList<>();
// trasverse the tempe
stk.push(m - 1);
for(int i = m - 2;i >= 0;i --){
if(tempe[stk.peek()] > tempe[i]){
res[i] = stk.peek() - i ;
stk.push(i);
}else{
while( !stk.isEmpty() && tempe[stk.peek()] <= tempe[i]){
stk.pop();
}
if(!stk.isEmpty()) res[i] = stk.peek() - i;
stk.push(i);
}
}
return res;
}
}
总结
- 我这里还调整了蛮久的,不行呀,写出来的代码还那么繁琐!
- 关于多态有了更加深刻的理解,你要使用队列或者堆栈的功能,就要使用对应的父类接口来承接对应接口实现类的实例对象,才能调用对应的方法。
- 不要子类直接调用,这样方法太多了,代码可读性也不好!
- 不要子类直接调用,这样方法太多了,代码可读性也不好!
参考学习
参考代码
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temp) {
int m = temp.size();
vector<int> f(m);
stack<int> upst;
for(int i = temp.size() - 1;i >= 0;i --){
while(!upst.empty() && temp[i] >= temp[upst.top()]) upst.pop();
if(upst.size()) f[i] = upst.top() - i;
upst.push(i);
}
return f;
}
};
修改之后的代码
- 确实更加简洁了,继续再弄吧!
class Solution {
public int[] dailyTemperatures(int[] tempe) {
// define the result array
int m = tempe.length;
int[] res = new int[m];
// define the compare stack
Deque<Integer> stk = new LinkedList<>();
// trasverse the tempe
for(int i = m - 1;i >= 0;i --){
while( !stk.isEmpty() && tempe[stk.peek()] <= tempe[i]) stk.pop(