【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 ...
随机推荐
- matlab优化函数fminunc
一起来学演化计算-matlab优化函数fminunc 觉得有用的话,欢迎一起讨论相互学习~Follow Me fminunc 求无约束多变量函数的最小值 非线性编程求解器 找到指定问题的最小值,\(m ...
- centos上为新创建的用户(git)指定根目录并生成公钥和私钥
1.修改用户的根目录 vim /etc/passed 2.su git 3.ssh-keygen -t rsa ssh-keygen -t rsa 4.如图所示,如果要实现无密码访问git仓库,把公钥 ...
- Eureka 与 zookeeper 的区别、
前言在微服务的开发过程中,如果使用的是 Dubbo 那就必须使用到 Zookeeper ,在使用 Spring Cloud Eureka 时,自然其功能更强大得多.博主也不得不感叹,Spring Cl ...
- matlab绘制直方图的方法
直接上代码,利用hist绘制频次直方图和频率直方图... %rand Fs=1000;N=10000; t=0:1/Fs:(N-1)/Fs; X1=rand(1,length(t)); subplot ...
- Qt qml的软件架构设计
google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...
- [.Net] - 使用 iTextSharp 生成基于模板的 PDF,生成新文件并保留表单域
背景 基于 PDF Template 预填充表单项,生成一份新的 PDF 文件,并保留表单域允许继续修改. 代码段 using iTextSharp.text.pdf; /* Code Snippet ...
- [DevExpress] - 在 DataGrid 中添加多选复选框的方法
设置方法 在 GridView 中设置 OptionSelection 属性如下: 效果 参考资料 https://stackoverflow.com/a/9078848http://blog.csd ...
- java多线程中篇(一) —— Thread详情
简介 简言之,现在的JDK线程模型基于操作系统原生线程,所以模型依赖于操作系统对线程的支持,另外Windows和Linux系统提供的线程模型就是一对一的 所以可以简单认为: 现在Java线程与操作系统 ...
- 【转】MySQL中EXISTS的用法
原文链接:https://www.cnblogs.com/qlqwjy/p/8598091.html 比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,Compan ...
- python 之 并发编程(开启子进程的两种方式,进程对象的属性)
第九章并发编程 同一个程序执行多次是多个进程 import time import os print('爹是:',os.getppid()) #父进程PID,(pycharm) print('me ...