1、重新学习了栈的操作,isEmpty()、peek()以及pop()、push()操作
但是值得注意的点是push()必须要有输入
2、单调栈用在这里非常巧妙,通过暴力搜索的方法无法通过最后一个用例
并且通过使用单调栈可以使得时间复杂度从O(
)降到了O(
)
3、Deque
LinkedList 实现了 Deque 接口,可作为队列使用。
LinkedList
单调栈算法:
- class Solution {
- public int[] dailyTemperatures(int[] temperatures) {
- int[] answer = new int[temperatures.length];
- Deque
stack = new LinkedList<>(); - for(int i=0;i
- while( !stack.isEmpty() && temperatures[stack.peek()] < temperatures[i] ) {
- answer[stack.peek()] = i - stack.peek();
- stack.pop();
- }
- stack.push(i);
- }
- return answer;
- }
- }
-
暴力算法:
- class Solution {
- public int[] dailyTemperatures(int[] temperatures) {
- int[] answer = new int[temperatures.length];
- for(int i=0;i
- for(int j=i+1;j
- if( temperatures[j] > temperatures[i] ) {
- answer[i] = j-i;
- break;
- }
- // answer[i] = 0;
- }
- }
- return answer;
-
- }
- }
-
相关阅读:
短链平台设计
重写muduo网络库开发环境和前期准备
【Arduino+ESP32专题】串口的简单使用
算力免费,还奖钱,OpenI日常激励活动“我为开源打榜狂”来袭
分支结构相关
springboot自定义加密数据库密码
[PG]将一行数据打散成多行数据
使用ref如何获取到input标签中的值
数据结构中常见的哈希表,到底是什么?
CUDA学习笔记(十二) CUDA库简介
-
原文地址:https://blog.csdn.net/weixin_43582743/article/details/133814688
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU