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. pip_install的安装

    1.下载get-pip.py https://pip.pypa.io/en/latest/installing/#id9 2.运行 python get-pip.py 3.python -m pip ...

  2. SDUT OJ 迷之好奇 (字典树 )

    迷之好奇 Time Limit: 2000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description FF得到了一个有n个数字的集 ...

  3. jquery实现淘宝动态图展示商品

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. adminlte+layui框架搭建1

    之前写过一篇layui-master的UI搭建,但是感觉layui-master缺少一些东西(前台页面不过多研究),比如说页面的皮肤.菜单.插件,当然我也希望layui的插件可以越来越多,支持多个浏览 ...

  5. liunx php 安装 redis 扩展

    切换到安装目录:  cd /usr/local/ 下载php redis扩展:wget http://pecl.php.net/get/redis-2.2.8.tgz 更改名称压缩包名称: mv re ...

  6. C++_标准模板库STL概念介绍4-算法

    STL包含很多处理容器的非成员函数: sort() copy() find() random_shuffle() set_union() set_intersection() set_differen ...

  7. C++_异常8-异常、类和基础

    异常.类和继承以三种方式相互关联. 首先,可以像标准C++库所做的那样,从一个异常类派生出另一个. 其次,可以在类定义中嵌套异常类声明来组合异常. 第三,这种嵌套声明本身可以被继承,还可以作为基类. ...

  8. [USACO18FEB]Taming the Herd

    Luogu4267 题解 对于\(dp[i][j]\) , 预处理出一些转移一步的次数 , 然后可以很方便的转移 : \(dp[i][j]=min(dp[k][j-1]+cnt[j][i])\)

  9. QDU_AP协会18级ST1

    A - A + B Problem II I have a very simple problem for you. Given two integers A and B, your job is t ...

  10. c# 实现无符号右移

    /// <summary> /// 无符号右移, 相当于java里的 value>>>pos /// </summary> /// <param nam ...