题目要求

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.

题目分析及思路

给定两个数组nums1和nums2,且nums1是nums2的子集,要求找到nums1中的所有元素在nums2中对应的the next greater numbers。nums1中的元素的the next greater number是该元素在nums2的位置右起第一个比自身大的元素。如果不存在,就返回-1。可以使用列表推导式,并定义一个函数,该函数的作用是得到元素的the next greater number,具体实现是获得元素在nums2中的索引,并遍历nums2,若有元素同时满足索引和值都比已知元素的大,则获取该元素,跳出循环。

python代码

class Solution:

def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:

def great_num(i):

great_list = []

idx = nums2.index(i)

for j,val in enumerate(nums2):

if j > idx and val > i:

great_list.append(val)

break

if len(great_list) == 0:

return -1

else:

return great_list[0]

return [great_num(i) for i in nums1]

LeetCode 496 Next Greater Element I 解题报告的更多相关文章

  1. 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...

  2. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

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

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

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

  5. 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...

  6. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

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

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

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

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

随机推荐

  1. COMBINATORIAL TESTING

    COMBINATORIAL TESTING -Test note of “Essential Software Test Design” 2015-09-06 Content 16.1 Coverag ...

  2. Mysql 导入导出csv 中文乱码

    这篇文章介绍了Mysql 导入导出csv 中文乱码问题的解决方法,有需要的朋友可以参考一下   导入csv: load data infile '/test.csv' into table table ...

  3. (笔记)AT91SAM9260的启动过程详细解说

    Bootstrap的启动过程 一. 说明: Bootstrap启动代码是官方提供的一级启动代码,包括汇编和C语言两部分组成.对AT91SAM9260来说编译完成后,代码长度必须小于4KB,烧写到dat ...

  4. JConsole & JVisualVM远程监视Websphere服务器JVM的配置方法

    原文链接:http://xjsunjie.blog.51cto.com/999372/1331880/ jconsole是JDK里自带的一个工具,可以监测Java程序运行时所有对象的申请.释放等动作, ...

  5. [Python] 07 - Statements --> Functions

    故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...

  6. Mercurial (hg) Hook : PHP Syntax Check , hg 代码检测 钩子

    用百度搜了一遍hg的hook教程,发现真的是太少了.公司目前正要用到这个,正好本人负责,So. 百度是个坑,少有的几篇文章,再加上善于发现的眼睛,发现TortoiseHg的UI操作都会在控制台显示动作 ...

  7. ASP.NET MVC 4 (六) 帮助函数

    帮助函数封装一些代码,方便我们在应用程序中重用,MVC内建很多帮助函数,可以很方便的生成HTML标记.首先列出后面示例中用到的数据模型类定义: namespace HelperMethods.Mode ...

  8. OpenGL——二次曲面函数(球面-圆锥面-圆柱面)

    代码: #include<iostream> #include <math.h> #include<Windows.h> #include <GL/glut. ...

  9. android基础---->service的生命周期

    服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...

  10. fs项目---->async/await的学习(一)

    2018-07-11号,我来到了fs项目组担任后端开发的角色.这是我来thoughtworks以来首个的正式项目,不管是在技术还是在敏捷的实践中都是受益匪浅.来感受tw特殊的文化的同时,我希望自己能够 ...