题目:
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:
1. All elements in nums1 and nums2 are unique.
2. The length of both nums1 and nums2 would not exceed 1000.

思路:
nums1的数为nums2的子集,如果对nums1中的数,在nums2中找到它的位置,如果之后还有比它大的数,输出数组对应位置数就是比它大的第一个数,如果没有就是-1
代码:

class Solution {
public:vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {vector<int> result;stack<int> s;map<int, int> m;for (int i = nums.size() - 1; i >= 0; i--) {//先根据nums2将所有的数出现对应的结果保存在map中while (!s.empty() && s.top() <= nums[i]){s.pop();}m[nums[i]] = s.empty() ? -1 : s.top();s.push(nums[i]);}for (int i = 0; i < findNums.size(); i++){//循环判断nums1中的数result.push_back(m[findNums[i]]);}return result;}
};

LeetCode 496. Next Greater Element I相关推荐

  1. 496. Next Greater Element I - LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 496. Next Greater Element I Solution 题目大意:给你一个组数A里面每个元素都不相 ...

  2. LeetCode 503 Next Greater Element II(stack)

    问题:给出一个数组,数组头与尾相连,求所有元素后第一个比当前元素大的元素 思路:因为要求元素后第一个比当前元素大的元素.当采用从头到尾遍历方法时,如果当前元素比栈顶元素大,则入栈记录栈顶元素的大元素. ...

  3. LeetCode之Next Greater Element I

    1.题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset ...

  4. leetcode 496, 503, 556. Next Greater Element I, II, III | 496, 503, 556. 下一个更大元素 I,II,III(单调栈)

    496. Next Greater Element I https://leetcode.com/problems/next-greater-element-i/ 单调栈问题,参考:https://l ...

  5. LeetCode:Find Peak Element - 寻找一个数组内的顶点

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Find Peak Element(寻找一个数组内的顶点) 2.题目地址 https://leetcode.co ...

  6. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  7. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

  8. LeetCode 496.下个更大的数

    LeetCode 496. 下一个更大元素 I 给你两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.请你找出 nums1 中每个元素在 nums2 中 ...

  9. C#LeetCode刷题之#496-下一个更大元素 I(Next Greater Element I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4026 访问. 给定两个没有重复元素的数组 nums1 和 num ...

最新文章

  1. 数据中心基础设施管理的演进
  2. 利用Redis实现消息队列原理
  3. 一图尽览华为云数据库全套安全解决方案
  4. arm 基于qcamera实现_基于Arm平台的研华EPC—R4680工控机实现快速储物柜智能解决方案...
  5. 永洪BI-实现按钮输出文件
  6. 书剑中医电子处方软件 V16.0
  7. (droid分享)新浪微博开发系列【十一】之查看微博正文
  8. flash 加载外部flash怎么卸载干净
  9. sam格式的结构和意义_SAM文件是什么
  10. 「鹿班智能设计平台」是如何工作的
  11. 关于RTMP推流直播摄像头最新问题解答
  12. Correcting Chinese Spelling Errors with Phonetic Pre-training
  13. 【PyTorch】随机种子 与 网络初始化
  14. ​2022企业级BI平台白皮书(附下载)
  15. 中考禁用计算机,南方网:广州中考使用计算器有规定 禁用型号要留意
  16. 邓应海:美通胀见顶迹象令美联储缩表时点蒙阴?日内最新黄金走势分析
  17. Python自然语言处理 第一章 课后习题答案
  18. 将0.1101101*2^(-10)表示成阶码用4位移码、尾数用8位原码(含符号位)的浮点数
  19. R 数据分析方法(梅长林)exercise1-4
  20. 案件缺席?跨境电商平台遭遇TRO账户冻结,什么时候会被判缺席?

热门文章

  1. 302状态码_HTTP状态码status code详解
  2. OpenCL 数据类型
  3. jQuery UI基础 学习笔记
  4. 沈航计算机考研真题,2018年沈阳航空航天大学考研真题硕士研究生入学考试试题...
  5. java中字符串(2)String
  6. C语言字符5,c语言总览5:字符输入和输出
  7. phpShort v3.2 – PHP短网址平台源码
  8. HTML5清爽博客自媒体网站模板
  9. linux如何更改ssh配置文件,Linux系统下ssh的相关配置详细解析
  10. 服务器不删档的设置_不删档预捏脸开启,快来体验次世代黑科技!