【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 ...
随机推荐
- DB2使用MERGE INTO语句实现西虹市首富的新增及更新操作
首先我们新建一张名为XIHONGSHISHOUFU的表,这张表是评委会初步评选出的西虹市首富的候选人员,下面的SQL语句包含建表和插入数据的部分: CREATE TABLE XIHONGSHISHOU ...
- 解释张量及TF的一些API
张量的定义 张量(Tensor)理论是数学的一个分支学科,在力学中有重要应用.张量这一术语起源于力学,它最初是用来表示弹性介质中各点应力状态的,后来张量理论发展成为力学和物理学的一个有力的数学工具.张 ...
- code and dataset resources of computer vision
From:http://rogerioferis.com/VisualRecognitionAndSearch2014/Resources.html Source Code Non-exhaustiv ...
- [EF] - "已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭" 之解决
错误 解决 在 ConnectionString 中添加 MultipleActiveResultSets=true(适用于SQL 2005以后的版本).MultipleActiveResultSet ...
- C#中HttpWebRequest、WebClient、HttpClient的使用
HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 ...
- java多线程中篇(三) —— 线程的控制(创建,运行,阻塞,中断,结束)
简介 线程的控制就是程序对线程的主要管理,最重要的就是状态的切换维护. 每种转态都有不同的引发事件(对应线程的方法),每种状态又有各自不同的处理步骤和过程,整个线程控制主要就是涉及这些内容. 正文 线 ...
- struts笔记1
框架:所谓框架,就是把一些繁琐的重复性代码封装起来,使程序员在编码中把更多的精力放到业务需求的分析和理解上面 SHH:strust spring hibernate; SSM:springmvc sp ...
- PAT(B) 1062 最简分数(Java)
题目链接:1062 最简分数 (20 point(s)) 题目描述 一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正 ...
- Filter讲解4
想要 浏览更多Fiddler内容:请点击进入Fiddler官方文档 阅读目录: 一.使用.NET代码扩展Fiddler 二.实现Fiddler接口 三.创建Fiddler扩展项目 四.在扩展程序选项卡 ...
- 去除echarts饼状图的引导线
series: { name: "流量占比分布", type: "pie", radius: ["40%", "60%" ...