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.

这个题目就是用stack, 分别将nums2里面每个对应的next bigger number找到, 如果没有, 那么为-1, 并且存到d里面.  T :O(m)  m = len(nums)

Code

class Solution(object):
def nextGreaterElement(self, findNums, nums):
stack,d, ans = [],{}, []
for num in nums:
while stack and num > stack[-1]:
d[stack.pop()] = num
stack.append(num)
while stack:
d[stack.pop()] = -1
for num in findNums:
ans.append(d[num])
return ans

[LeetCode] 496. Next Greater Element I_Easy tag: Stack的更多相关文章

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

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

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

  3. LeetCode: 496 Next Greater Element I(easy)

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

  4. LeetCode 496 Next Greater Element I 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

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

  6. [LeetCode] 556. Next Greater Element III 下一个较大的元素 III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  7. 496. Next Greater Element I - LeetCode

    Question 496. Next Greater Element I Solution 题目大意:给你一个组数A里面每个元素都不相同.再给你一个数组B,元素是A的子集,问对于B中的每个元素,在A数 ...

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

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

  9. 【leetcode】496. Next Greater Element I

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

随机推荐

  1. SSL是什么?如何使用?

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...

  2. 渗透中Meterpreter基本操作和对应的windows上的排查或者现象

    Meterpreter的简单介绍 Meterpreter 是MSF自带一个强大的SHELL,可以执行很多功能. Meterpreter SHELL 基本操作 meterpreter>backgr ...

  3. 常用linux命令及shell脚本

    参考:Linux命令大全 分割大文件 Split命令 按行分割(只能是文本文件) $split -l 1000 big_file 前缀 按文件大小分割 $split -b 64m big_file 前 ...

  4. win10拖拽的问题

    以前很多可以支持托砖的到了win10都不行了 解决 按Windows键+R,打开“运行”对话框:输入regedit,回车或确定.   依次找到以下键值HKEY_LOCAL_MACHINE\SOFTWA ...

  5. CSS基础问题

    1.css引入问题 本来以为css引入是很简单的问题,但是在写demo中,使用连接方式引入就出现了问题,找了半天,还是说一下问题吧. 在引入时没有写rel="stylesheet" ...

  6. Timer应用之Interval优化

    开发中, 有时有这种场景,使用 Timer 的 Timer_Elapsed 间隔  执行(如:从数据库)获取数据 与 现有 应用服务器中的 静态变量数据(起到缓存的目的)做 对比 ,若有改变,则 更新 ...

  7. Keepalived+Nginx高可用集群

    Keepalived简介 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替 ...

  8. 10W年薪和30W+年薪的产品经理差距在哪?

    举办到今年第六届壹佰案例峰会,认识的“程序猿/媛”朋友越来越多,时间长了就发现,程序员的世界一点也不单调,外界传说的不善言辞.最大的乐趣就是买“机械键盘”都是不对的.你见过周末组团去山里骑哈雷的研发经 ...

  9. In MySQL, a zero number equals any string

    最近在做项目的过程中发现了一个问题 数据库表 test  有个字段是 target_id  int(11),这个字段可能为零 使用如下查询 select * from test where targe ...

  10. MongoDB的”存储过程“

    使用 db.system.js.insert({_id:"aaa", value:"zzq"}); 这条命令向全局系统的js中添加了一个_id为"aa ...