739. Daily Temperatures - LeetCode
Question
Solution
题目大意:比今天温度还要高还需要几天
思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减
Java实现:
public int[] dailyTemperatures(int[] temperatures) {
int[] ans = new int[temperatures.length];
for (int i = 0; i<temperatures.length; i++) {
int highIdx = i;
for (int j=i+1; j<temperatures.length; j++) {
if (temperatures[j] > temperatures[i]) {
highIdx = j;
break;
}
}
ans[i] = highIdx - i;
}
return ans;
}
Ref
别人实现高效的方法
https://leetcode.com/problems/daily-temperatures/discuss/109832/Java-Easy-AC-Solution-with-Stack
Stack
public int[] dailyTemperatures(int[] temperatures) {
Stack<Integer> stack = new Stack<>();
int[] ret = new int[temperatures.length];
for(int i = 0; i < temperatures.length; i++) {
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int idx = stack.pop();
ret[idx] = i - idx;
}
stack.push(i);
}
return ret;
}
Array
public int[] dailyTemperatures(int[] temperatures) {
int[] stack = new int[temperatures.length];
int top = -1;
int[] ret = new int[temperatures.length];
for(int i = 0; i < temperatures.length; i++) {
while(top > -1 && temperatures[i] > temperatures[stack[top]]) {
int idx = stack[top--];
ret[idx] = i - idx;
}
stack[++top] = i;
}
return ret;
}
739. Daily Temperatures - LeetCode的更多相关文章
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
- LeetCode 739. Daily Temperatures (每日温度)
题目标签:HashMap 题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖.返回一组等待的天数. 可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的in ...
- 739. Daily Temperatures
https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- [Leetcode 739]*还有几天会升温 Daily Temperatures
[题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- LeetCode - Daily Temperatures
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
随机推荐
- vue全家桶+axios+jsonp+es6 仿肤君试用小程序
vue全家桶+axios+jsonp+es6 仿肤君试用小程序 把自己写的一个小程序项目用vue来实现的,代码里面有一些注释,主要使用了vue-cli,vue,vuex,vue-router,axoi ...
- php弹窗后跳入另一个页面
之前写项目时,在跳转页面前加入一个弹窗,发现弹窗没有弹出来就直接跳转了,之前使用的header跳转发现不行,换成location.href也不行,后来再location.href前加入一个parent ...
- Java在方法中定义可变参数类型
学习目标: 掌握可变参数的应用 学习内容: 1.定义 在方法中传递数组有一种更简单的方式--方法的可变参数,其本质是一个语法糖,目的是让开发者写代码更简单. 2.语法 [修饰符] 返回值类型 方法名称 ...
- docker搭建cordova 11环境
cordova@11 依赖环境: Java_jdk@1.8.0 Nodejs@12.22.9 android-sdk Build-tools 28 API 28 apache-ant@1.10.12 ...
- PostgreSQL执行计划:Bitmap scan VS index only scan
之前了解过postgresql的Bitmap scan,只是粗略地了解到是通过标记数据页面来实现数据检索的,执行计划中的的Bitmap scan一些细节并不十分清楚.这里借助一个执行计划来分析bitm ...
- JetBrains Rider C# 学习①
Rider 发现 Alt+F7 键无效: 把GeForce Experience里的游戏覆盖关闭 前言 C#从入门到精通 链接:https://pan.baidu.com/s/1UveJI_f-c5D ...
- 计算机编码规则之:Base64编码
目录 简介 Base64和它的编码原理 Base64的变体 Base64的编码细节 总结 简介 我们知道计算机中的文件可以分为两种,一种是人肉眼可读的文本类文件,一种是肉眼不可读的二进制文件.一般来说 ...
- Mysql入门学习day2随笔2
事务 什么是事务 要么都成功,要么都失败 事务原则 原子性:针对一个事务,两个步骤一起成功或一起失败 一致性:最终一致性,例如A.B之间的转账,无论两个账户如何操作,两账户的总价值不会变 隔离性:针对 ...
- Java学习day30
线程分为用户线程和守护线程,虚拟机必须确保用户线程执行完毕,虚拟机不用等待守护线程执完毕 并发:同一个对象被多个线程同时操作,例如上万了同时抢100张票,手机银行和柜台同时取同一张卡里的钱 处理多线程 ...
- AcWing 【算法提高课】笔记02——搜索
搜索进阶 22.4.14 (PS:还有 字串变换 A*两题 生日蛋糕 回转游戏 没做) 感觉暂时用不上 BFS 1. Flood Fill 在线性时间复杂度内,找到某个点所在的连通块 思路 统计连通块 ...