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:
All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000. 这题描述的需求是:
给我们两个数组 比如 nums1 = [1,2,3] nums2 = [1,2,3,4,5,6]
需要我们求出的结果也是一个数组,这个数组,里面的数值一次是:
  对num1里面每一个数字x ,找到在num2里x出现的右侧最近的一个比x大的数字。
  如过nums里x的右侧没有比x大的 就用-1代表这个数字的结果 比如:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
4在num2中的右侧没有比4大的 所以结果是-1
1在num2中 右侧最近的比他大的是3
2在num2中没有右侧数字了 结果是-1 我的python代码:
 class Solution(object):
def nextGreaterElement(self, findNums, nums):
"""
:type findNums: List[int]
:type nums: List[int]
:rtype: List[int]
"""
res = []
for i in findNums:
index = nums.index(i)
index2 = -1
for j in range(index + 1, len(nums)):
if nums[j] > i:
index2 = j
break
if index2 == -1:
res.append(-1)
else :
res.append( nums[index2])
return res if __name__ == '__main__':
s = Solution()
res = s.nextGreaterElement([4,1,2], [1,3,4,2] )
print(res)


leetcode算法:Next Greater Element I的更多相关文章

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

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

  4. LeetCode算法题-Majority Element(Java实现)

    这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/2的元素.假 ...

  5. 【算法】LeetCode算法题-Remove Element

    这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...

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

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

  7. LeetCode - 503. Next Greater Element II

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

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

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

  9. [LeetCode] 496. Next Greater Element I_Easy tag: Stack

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

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

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

随机推荐

  1. jQuery中的ajax的相关方法

    JQuery对Ajax操作进行了封装,$.ajax()方法属于最底层的方法,第2层是load().$.get().$.post()方法,第3层是$.getScript()和$.getJSON()方法. ...

  2. postgresql和oracle数据库对比

    SQL执行计划干预 从使用postgresql来看,想要改变执行计划只能通过対表进行分析,不能通过添加hint的方式来改变执行计划: oracle不仅可以通过对表进行收集统计来改变执行计划,而且很重要 ...

  3. [Oracle] UNIX与Windows 2000上Oracle的差异(III)

    作者:Ian Adam & David Stien, SAIC Ltd 日期:19-Dec-2003 出处:http://www.dbanotes.net翻译:Fenng ORACLE 的安装 ...

  4. Java 常用单词

    1.Object-Oriented ['əbdʒekt'ɔ:rɪəntɪd] 面向对象 adj 2.inheritance  [ɪnˈherɪtəns]  继承;遗传;遗产 n inherit  [ɪ ...

  5. 兄弟连学Python-Mysql的基础知识

    ##MySQL数据库基础知识 1.数据库系统(database system) 数据库系统是计算机系统中一种专门管理数组资源的系统,数据库存储的是一组或多组经过处理后的数据,管理这个数据库的软件成为数 ...

  6. Kafka OffsetMonitor:监控消费者和延迟的队列

    一个小应用程序来监视kafka消费者的进度和它们的延迟的队列. KafkaOffsetMonitor是用来实时监控Kafka集群中的consumer以及在队列中的位置(偏移量). 你可以查看当前的消费 ...

  7. 手把手 git建立仓库,远程推拉及常用git命令和部分Linux命令集锦

    方法一:直接在GitHub上建立一个项目,然后git clone (git address name): 此时已经建立好了一个git仓库: cd 文件夹 > 添加文件进去 >git add ...

  8. linux --> 系统信息命令

    系统信息命令 # uname -a // # 查看内核/操作系统/CPU信息 # head -n /etc/issue // # 查看操作系统版本 # cat /proc/cpuinfo // # 查 ...

  9. Android 优化APP 构建速度的17条建议

    转载:http://www.jianshu.com/p/a1cc8f2e0877 较长的构建时间将会减缓项目的开发进度,特别是对于大型的项目,app的构建时间长则十几分钟,短则几分钟,长的构建时间已经 ...

  10. 2018上C语言程序设计(高级)- 第0次作业成绩

    作业链接: https://edu.cnblogs.com/campus/hljkj/CS201702/homework/1617 评分规则 本次作业作为本学期的第一次作业,大家态度较诚恳,篇幅都比较 ...