LeetCode 496 Next Greater Element I 解题报告
题目要求
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中的元素的the next greater number是该元素在nums2的位置右起第一个比自身大的元素。如果不存在,就返回-1。可以使用列表推导式,并定义一个函数,该函数的作用是得到元素的the next greater number,具体实现是获得元素在nums2中的索引,并遍历nums2,若有元素同时满足索引和值都比已知元素的大,则获取该元素,跳出循环。nums1是
nums2的子集,要求找到nums1中的所有元素在nums2中对应的the next greater numbers
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 解题报告的更多相关文章
- 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- [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 ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- [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 ...
- LeetCode: 496 Next Greater Element I(easy)
题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- [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 ...
随机推荐
- 总结Sql Server内置函数实现MD5加密
--MD5加密 --HashBytes ('加密方式', '待加密的值') --加密方式= MD2 | MD4 | MD5 | SHA | SHA1 --返回值类型:varbinary(maximum ...
- [C++]Qt文本操作(按行读写)
资料来源:https://blog.csdn.net/flyfish1986/article/details/79487104 #include <QDebug> #include < ...
- PHP优化——从语言到业务
经常有人说php速度慢,其实语言层面的速度差异和实际的业务相比,不在一个数量级. 业务的瓶颈往往在于io,而不是CPU. 0x0 语言 语法 单引号和双引号 单引号不解析字符串里的变量,而双引号会解析 ...
- Unity读取Excel文件(附源代码)
今天想弄个Unity读取Excel的功能的,发现网上有许多方法,采用其中一种方法:加入库文件 Excel.dll 和ICSharpCode.SharpZipLib.dll库文件,(还有System.D ...
- elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor
错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor() ...
- python中的生成器函数是如何工作的?
以下内容基于python3.4 1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函 ...
- MyCAT简易入门 (Linux)
MyCAT是mysql中间件,前身是阿里大名鼎鼎的Cobar,Cobar在开源了一段时间后,不了了之.于是MyCAT扛起了这面大旗,在大数据时代,其重要性愈发彰显.这篇文章主要是MyCAT的入门部署. ...
- irc
https://www.irccloud.com/ webchat.freenode.net http://blog.csdn.net/wcc526/article/details/16993069 ...
- 仿迅雷播放器教程 -- 封装VLC (5)
虽然上个教程中10多行代码便做出了一个播放器,但如果加上快进快退等功能的话,代码都会挤在一团,阅读性很差,所以这个版本将对VLC进行封装,由于第一个教程已经进行了ffmpeg的封装,所以这里将 ...
- Linux权限详解 命令之 chmod:修改权限
权限简介 Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Li ...