数组和矩阵(3)——Next Greater Element I
https://leetcode.com/problems/next-greater-element-i/#/description
You are given two arrays (without duplicates)
nums1
andnums2
wherenums1
’s elements are subset ofnums2
. Find all the next greater numbers fornums1
's elements in the corresponding places ofnums2
.The Next Greater Number of a number x in
nums1
is the first greater number to its right innums2
. 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.
1.暴力
public class Solution {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
int len = findNums.length;
int[] res = new int[len];
if(len > nums.length) {
return res;
}
for(int i=0; i<len; i++) {
boolean flag = false;
boolean k = false;
for(int j=0; j<nums.length; j++) {
if(findNums[i] == nums[j]) {
flag = true;
continue;
}
if(flag == true && nums[j] > findNums[i]) {
res[i] = nums[j];
k = true;
break;
}
}
if(k == false) {
res[i] = -1;
}
}
return res;
}
}
2.集合
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x
Stack<Integer> stack = new Stack<>();
for (int num : nums) {
while (!stack.isEmpty() && stack.peek() < num)
map.put(stack.pop(), num);
stack.push(num);
}
for (int i = 0; i < findNums.length; i++)
findNums[i] = map.getOrDefault(findNums[i], -1);
return findNums;
}
数组和矩阵(3)——Next Greater Element I的更多相关文章
- 496. Next Greater Element I 另一个数组中对应的更大元素
[抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 下一个更大的数 Next Greater Element
2018-09-24 21:52:38 一.Next Greater Element I 问题描述: 问题求解: 本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- LeetCode——Next Greater Element I
LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...
- [LeetCode] 496. Next Greater Element I 下一个较大的元素 I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- [LeetCode] 503. Next Greater Element II 下一个较大的元素 II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- LeetCode 503. 下一个更大元素 II(Next Greater Element II)
503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...
随机推荐
- C# 动态创建实例化泛型对象,实例化新对象 new()
普通类实现字符串创建实例: var type =Assembly.Load("SqlSugar").GetType("SqlSugar.SqlServerDb" ...
- SDUT OJ 数据结构实验之链表八:Farey序列
数据结构实验之链表八:Farey序列 Time Limit: 10 ms Memory Limit: 600 KiB Submit Statistic Discuss Problem Descript ...
- 导出table为Excel
1.HTML <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...
- yalinqo 的使用...
from($this->getInfo())->where('$v["is_enable"]==1')->where(function (&$v) use ...
- windows使用putty工具 进行【复制】,【粘贴】操作
#复制# 按住鼠标左键,执行选择,放开左键时完成复制 #粘贴# 点击鼠标右键,执行粘贴
- AtCoder Beginner Contest 113 C
C - ID Time limit : 2sec / Memory limit : 1024MB Score: 300 points Problem Statement In Republic of ...
- [转] 如何在ie11里使用a连接创建动态下载文件流
[From] https://segmentfault.com/q/1010000009470664 查了资料,可以使用微软独家的msSaveBlob, 这个方法支持ie10及以上. var down ...
- windos下redis服务的后台启动
1. 进入 DOS窗口 2. 在进入Redis的安装目录 3. 输入:redis-server --service-install redis.windows.conf --loglevel verb ...
- 【记录】sqli-labs-master搭建
附上:链接:http://pan.baidu.com/s/1bpCRzl1 密码:ep48 下载完成后直接解压到phpstudy(该工具之前分享过,直接搜索下)的WWW目录下,启动phpstudy, ...
- Scrapy安装指南(windows)
windows开发,难免遇到很多坑,比一般开发是艰苦得多.先不吐槽windows,我们直接看这个scrapy怎么安装. 首先,要有一份文档,比如我用这个: http://scrapy-chs.read ...