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. Linux上部署SVN

    Linux上部署SVN author:headsen chen  2017-10-16  16:45:04 前提:通过yum来安装,必须是centos6.5的桌面版的.否则会出现某些的安装包不全而导致 ...

  2. delphi XE Datasnap SERVER 在windows 7 下为服务添加描述信息

    网上对服务添加描述信息的,多数是用注册表的方式,而注册表的方式,我在win7 下测试,不知为什么,总是不行,把执行的CMD以管理员模式开启,还是没加进去. 于是在网上查到下面的代码,保存供叁考,原文博 ...

  3. webpack使用教程

    webpack使用教程 接触webpack也有挺长一段时间了,公司的项目也是一直用着webpack在打包处理,但前几天在教新人的情况下,遇到了一个问题,那就是:尽管网上的webpack教程满天飞,但是 ...

  4. day01的那些事

    代码之道 路漫漫其修远兮,吾将上下而求索

  5. Vue之九数据劫持实现MVVM的数据双向绑定

    vue是通过数据劫持的方式来做数据绑定的,其中最核心的方法便是通过Object.defineProperty()来实现对属性的劫持,达到监听数据变动的目的. 如果不熟悉defineProperty,猛 ...

  6. jQuery中ajax方法无法执行回调函数问题

    最近遇到一个问题,发现使用jquery的ajax方法时,回调方法无法执行,而使用$.load()方法时却能正确返回数据.经过长时间调试最终发现是自己粗心大意,原来后台返回的是json数据,而返回的数据 ...

  7. Angular开发实践(六):服务端渲染

    Angular Universal Angular在服务端渲染方面提供一套前后端同构解决方案,它就是 Angular Universal(统一平台),一项在服务端运行 Angular 应用的技术. 标 ...

  8. Spring-MongoDB 关键类的源码分析

    本文分析的是 spring-data-mongodb-1.9.2.RELEASE.jar 和 mongodb-driver-core-3.2.2.jar. 一.UML Class Diagram 核心 ...

  9. 路径字符串数据转化为树型层级对象,path to json tree

    由于项目中使用了react 及 ant-design ,在使用tree树型控件时,需要 类似下面的数据, const treeData = [{ title: '0-0', key: '0-0', c ...

  10. 自定义ArrayList

    自定义实现ArrayList很简单,只需要明白下面几点 1.ArrayList 底层是由数组组成的 2.数组有容量限制,但是ArrayList并没有(实际上也有,Integer.MAX_VALUE). ...