题目要求

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. ASP.NET Core Razor Pages

    Razor 页面是Asp.Net Core2.0新增的一个功能.Razor 页面是 ASP.NET Core MVC 的一个新特性,它可以使基于页面的编码方式更简单高效. 环境:vs2017 .net ...

  2. (笔记)Linux内核学习(一)之内核介绍

    内核与操作系统: 内核是操作系统的核心部分,包含了系统运行的核心过程,决定系统的性能,操作系统启动内核被装入到RAM中: 操作系统与底层硬件设备交互和为运行应用程序提供执行环境. Linux内核与微内 ...

  3. Linux下MySQL5.7.18二进制包安装(无默认配置文件my_default.cnf)

    最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5.7.18)二进制包,CentOS7.2下测试安装,方便以后折腾.大概步骤如下,安装删除反复折腾了几 ...

  4. Spark学习笔记——房屋价格预测

    先翻译了一下给的房屋数据的特征,这里定义了一个case class,方便理解每个特征的含义, Kaggle的房价数据集使用的是Ames Housing dataset,是美国爱荷华州的艾姆斯镇2006 ...

  5. 【QT】打开文件对话框,选择路径下文件

    0.头文件中加入 public: QString fileName; public slots: void showImage(); 1.添加两个头文件 #include<qfiledialog ...

  6. <aop:aspectj-autoproxy />作用

    通过配置织入@Aspectj切面 虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作. 通过aop命名空间的<aop:aspectj-a ...

  7. JS 字符ASCII转换

    var a="1368628429"; String.fromCharCode( a.substring(a.length-1,1).charCodeAt())=>" ...

  8. Linux驱动技术(二) _访问I/O内存

    ARM是对内存空间和IO空间统一编址的,所以,通过读写SFR来控制硬件也就变成了通过读写相应的SFR地址来控制硬件.这部分地址也被称为I/O内存.x86中对I/O地址和内存地址是分开编址的,这样的IO ...

  9. C语言程序设计--执行命令

    1.system函数 1.1函数原型 int system(char *command); 1.2解释 system()会调用fork()产生子进程,由子进程来调用/bin/sh -c string来 ...

  10. IOS多线程处理

    http://www.jianshu.com/p/0b0d9b1f1f19 首页专题下载手机应用 显示模式登录     注册登录 添加关注 作者 伯恩的遗产2015.07.29 00:37* 写了35 ...