【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 ...
随机推荐
- Spring cloud微服务安全实战-3-2 第一个API及注入攻击防护
先来写一些用户的基础数据的管理的api.就是用户的基本的增删改查.用spring boot可以很容易的写出这种api 首先新建maven的项目 依赖关系 引入依赖.用最新的spring boot ht ...
- 聚类K-Means和大数据集的Mini Batch K-Means算法
import numpy as np from sklearn.datasets import make_blobs from sklearn.cluster import KMeans from s ...
- centos上为新创建的用户(git)指定根目录并生成公钥和私钥
1.修改用户的根目录 vim /etc/passed 2.su git 3.ssh-keygen -t rsa ssh-keygen -t rsa 4.如图所示,如果要实现无密码访问git仓库,把公钥 ...
- layer实现鼠标悬浮效果
; $(document).on('mouseenter', '.layer_hover', function(){ var words = $(this).data('words'); tip_in ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- jquery weui 图片浏览器Photo Browser
jquery weui 图片浏览器Photo Browser 如何使用? 对应组件地址:http://jqweui.com/extends#swiper 先说说业务场景:类似朋友圈这样的布局效果,点击 ...
- Java基础教程:内部类
Java基础教程:内部类 内部类 内部类,是指在一个类的内部定义的类.就像下面这样: public class EnclosingClass { . . . public class Nest ...
- C语言实现从左向右字幕滚动的效果
#include <stdio.h> #include <string.h> #include <windows.h> int main() { char str[ ...
- Redis从认识安装到实现增删改查
Redis从一无所知,到知道一点点 Redis是一个使用ANSI C编写的开源.支持网络.基于内存.可选持久性的键值对存储数据库 --维基百科 可以简单的说,Redis就是一款高性能的NoSQL数据库 ...
- 031 Android 自定义控件
1.自定义控件的优点 Android自身带的控件不能满足需求, 需要根据自己的需求定义控件. 2.自定义控件的分类: (1)组合已有的控件实现 (2)继承已有的控件实现(扩展已有的功能) (3)完全自 ...