【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记录-ca证书制作(二)
1)下载cfssl #!/bin/bash wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget https://pkg.cfssl.org/R ...
- Linux系统调优——Memory内存(二)
(1).查看Memory(内存)运行状态相关工具 1)free命令查看内存使用情况 [root@youxi1 ~]# free -m //-m选项,以MB为单位显示 total used free s ...
- 利用SynchronizationContext.Current在线程间同步上下文(转)
https://blog.csdn.net/iloli/article/details/16859605 简而言之就是允许一个线程和另外一个线程进行通讯,SynchronizationContext在 ...
- Silence Removal and End Point Detection JAVA Code(音频删除静音与结束判断)
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/08/silence-removal-and-end-point-detection_29.h ...
- [ kvm ] 学习笔记 8:Ovirt 基础及使用
目录- 1. oVirt 功能介绍- 2. oVirt 安装部署 - 2.1 基础准备 - 2.2 安装 ovirt-engine - 2.3 配置 kvm 主机 - 2.4 ...
- 《MySQL必知必会》学习笔记——第1章 了解SQL
第1章 了解SQL 本章将介绍数据库和SQL,它们是学习MySQL的先决条件. 1.1 数据库基础 你正在阅读本书,这表明你需要以某种方式与数据库打交道.在深入学习MySQL及其SQL语言的实现之前, ...
- js实现深度优先遍历和广度优先遍历
深度优先遍历和广度优先遍历 什么是深度优先和广度优先 其实简单来说 深度优先就是自上而下的遍历搜索 广度优先则是逐层遍历, 如下图所示 1.深度优先 2.广度优先 两者的区别 对于算法来说 无非就是时 ...
- Github克隆代码慢问题解决办法
参考:https://blog.csdn.net/stone8761/article/details/79072148 https://blog.csdn.net/github_37847975/ar ...
- Linux杀毒软件(ClamAV)
Clam AntiVirus是一个类UNIX系统上使用的反病毒软件包.主要应用于邮件服务器,采用多线程后台操作,可以自动升级病毒库. 一.下载安装 1.下载 clamav官网:http://www.c ...
- Selenium(二十):expected_conditions判断页面元素
1. 判断元素(expected_conditons) 作为一个刚刚转到python开发的小朋友,在开发前只将前辈们封装的方法看了一遍,学了一边selenium基础.看到封装的方法有什么判断元素是否存 ...