leetcode算法: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. 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的更多相关文章
- [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] 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 ...
- [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 ...
- LeetCode算法题-Majority Element(Java实现)
这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/2的元素.假 ...
- 【算法】LeetCode算法题-Remove Element
这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...
- [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
Given a circular array (the next element of the last element is the first element of the array), pri ...
- LeetCode 496 Next Greater Element I 解题报告
题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- [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 ...
随机推荐
- 关于Unity的协程
协程 认识协程 //协程不是多线程:是一段在主程序之外执行的代码 //协程不受生命周影响 //作用:能够口直代码在特定的时间执行. //1,延时操作 //2,等待某代码执行结束之后执行 /* 特点:1 ...
- PHP 简单的加密解密方法
本算法的基础:给定字符A B,A^B=C,C^B=A,即两次异或运算可得到原字符.实现代码如下: /** * @desc加密 * @param string $str 待加密字符串 * @param ...
- FNV算法实战
HASH算法介绍 Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长 ...
- python全栈开发-Day8 函数基础
python全栈开发-Day8 函数 一 .引子 1. 为何要用函数之不用函数的问题 #1.代码的组织结构不清晰,可读性差 #2.遇到重复的功能只能重复编写实现代码,代码冗余 #3.功能需要扩展时,需 ...
- 搭建nuxtjs程序 —— 用户信息 or token怎么不丢失
框架背景:开发框架采用vue,需要更好的SEO,更快的内容到达时间,从浏览器看不到对服务器的请求接口,选用开箱即用的nuxtjs. 问题背景:1. 前后分离,需前端存储token及登录后的用户信息: ...
- iptables.sh 初始化防火墙配置
#!/bin/bash iptables -F iptables -X iptables -Z iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT ...
- CSS 剩余宽度和高度完全填充
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- DevOps实践之Gitlab安装部署
All GitLab packages are posted to our package server and can be downloaded. We maintain five repos: ...
- JavaEE HttpServlet 解析
上一篇 文章中有提到,GenericServlet 是对 Servlet 的一个通用实现,并提供了一个抽象的 service() 方法.而我们的互联网是使用 Http 协议来通信的,那针对这个&quo ...
- Bate敏捷冲刺每日报告--day5
1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285) Git链接:https://github.com/WHUSE2017/C-team 2 ...