【leetcode】496. Next Greater Element I
原题
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.
解析
查找下一个较大的元素
给出num1,num2,1是2的子集但乱序,找出1中元素在2中右侧第一个较大的元素,若不存在则取-1
思路
我的思路很简单,就是遍历,但效率较低,找到一种比较清奇的思路:利用栈来装num2的元素,如果新元素比栈顶的小,就继续装,如果比它大,就将栈顶元素和即将装入的新元素成对装在一个map中待用,一直装map只到要入栈的元素比栈顶的小,再继续
待全部装完后,map中的元素对就都是该元素和他右侧第一个较大的元素对了,此时只需要遍历num1,从map中取值即可
我的解法
public int[] nextGreaterElement(int[] findNums, int[] nums) {
int[] result = new int[findNums.length];
int n = 0;
for (int i = 0; i < findNums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] == findNums[i]) {
boolean isFind = false;
for (int k = j + 1; k < nums.length; k++) {
if (nums[k] > findNums[i]) {
result[n++] = nums[k];
isFind = true;
break;
}
}
if (!isFind) {
result[n++] = -1;
}
break;
}
}
}
return result;
}
较优解
public int[] nextGreaterElementOptimize(int[] findNums, int[] nums) {
Map<Integer, Integer> nextGreater = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for (int num : nums) {
while (!stack.isEmpty() && stack.peek() < num) {
nextGreater.put(stack.pop(), num);
}
stack.push(num);
}
for (int i = 0; i < findNums.length; i++) {
findNums[i] = nextGreater.getOrDefault(findNums[i], -1);
}
return findNums;
}
【leetcode】496. Next Greater Element I的更多相关文章
- 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【leetcode】1019. Next Greater Node In Linked List
题目如下: We are given a linked list with head as the first node. Let's number the nodes in the list: n ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【LeetCode】162. Find Peak Element (3 solutions)
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
随机推荐
- k8s记录-下载k8s相关二进制包(一)
1)软件清单操作系统:CentOS7Kubernetes版本:v1.14.2flannel:v0.10.0ectd3:v3.3.11Docker版本:v18.09.0-ceDocker-compose ...
- v关于使用Glide加载图片失败时显示自己特定的图片
Glide是Android加载图片的一个框架. 常用加载图片到imageView:Glide.with(this).load(url).into(ImageView imageview). 当加载失败 ...
- mysql存储过程中declare 和set 定义变量的区别
declare为对变量进行声明,声明必须制定变量的数据类型,只能写在过程的前面set是对变量赋值,可以放在过程的任何地方对没有declare声明过的变量赋值,该变量必须以加上@号,否则会报错 DECL ...
- DNS解析问题
DNS解析问题,在/etc/resolv.conf文件中加如下一行: nameserver 8.8.8.8 即使用谷歌的域名服务器 如下问题都是DNS解析问题: apt-get update 异常 T ...
- html的输出&,空格,大小于号
最近定做安装程序,因为这次定做名字里有&符号,用微软的txt文本打开配置文件,在配置文件里修改了名称,名称在文本里显示正常,但是定做出来后,发现&符号变成了_下划线,在本来的& ...
- 【深度学习框架-caffe】caffe中使用到的layer
https://www.jianshu.com/p/f6f49f6bcea6 https://github.com/BVLC/caffe/tree/master/include/caffe/layer ...
- 我的Java之路
前言: 之前在学习python,刚开始的时候跟多数小白一样学习一些基础的知识,比如数据类型,用法,基本的语言结构,学了一段时间实在是学习不下去了,真是太TMD的无聊了,很多方法都记不住,也不知道学了这 ...
- (模板)hdoj2544(最短路--bellman-ford算法&&spfa算法)
题目链接:https://vjudge.net/problem/HDU-2544 题意:给n个点,m条边,求点1到点n的最短路. 思路: 今天学了下bellman_ford,抄抄模板.dijkstra ...
- Vue零碎总结
1.Vue指令里的bind钩子是生成了vnode,但是将它插入/更新到浏览器dom树之前的操作,因此对于一些需要插入dom树后执行的操作它是不支持的,如el.focus()方法,这些要放在insert ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...