https://leetcode.com/problems/next-greater-element-i/#/description

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.

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的更多相关文章

  1. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  2. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  3. [LeetCode] Next Greater Element I 下一个较大的元素之一

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  4. 下一个更大的数 Next Greater Element

    2018-09-24 21:52:38 一.Next Greater Element I 问题描述: 问题求解: 本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums ...

  5. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  6. LeetCode——Next Greater Element I

    LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...

  7. [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 ...

  8. [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 ...

  9. LeetCode 503. 下一个更大元素 II(Next Greater Element II)

    503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...

随机推荐

  1. 模板【洛谷P3368】 【模板】树状数组 2

    P3368 [模板]树状数组 2 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的值 树状数组区间加,单点查询. code: #include <i ...

  2. Spring boot redis自增编号控制 踩坑

    近段期间,公司 接手一个订单号生成服务,规则的话已经由项目经理他们规定好了,主要是后面的四位数代表的关于当前订单号已经执行第几个了.而这里面有一个要求就是支持分布式.为了实现这个东西,刚开始我使用了r ...

  3. kotlin spring mvc request json 请求

    // json 代码{ /*用户信息*/ user: { username: '{$user.username}', headImg: '{$user.headImg}', targetId: '{$ ...

  4. 洛谷 P1800 software_NOI导刊2010提高(06)

    题目链接 题解 二分答案+dp 如果我们知道答案,贪心地想,让每个人做尽量多的模块一定不会比最优解差 \(f[i][j]\)表示前\(i\)个人第一个模块做了\(j\)块,第二个模块最多能做多少 然后 ...

  5. nvm安装

    1.下载安装包,地址:https://github.com/coreybutler/nvm-windows 2.解压后,点击 nvm-setup 安装,一路默认安装,next下去 3.打开安装位置,然 ...

  6. D3.js v4版本 按住shift键框选节点demo

    http://download.csdn.net/download/qq_25042329/10139649

  7. AMD、CMD/AMD与CMD的区别

    http://blog.csdn.net/jackwen110200/article/details/52105493

  8. springboot(四)-项目部署

    Springboot和我们之前学习的web应用程序不一样,其本质上是一个java应用程序.部署的方式有两种:打成jar包,或者打成war包. 打成jar包 切换到项目文件中 然后mvn install ...

  9. hdu-1022-栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. 【Python】struts2-045批量检测

    0x00   环境 存在struts2-045漏洞的war包 apache-tomcat 0x01   脚本 #coding:utf-8 import re import urllib import ...