leectcode 496. Next Greater Element I

来源:互联网 发布:无线 网络摄像头 编辑:程序博客网 时间:2024/06/03 21:53

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.

num1为num2子集 且无重复数字 找到num1中的每个数在num2中位置并返回之后比其大的数 若无返回-1

代码:

class Solution(object):
    def nextGreaterElement(self, findNums, nums):
        """
        :type findNums: List[int]
        :type nums: List[int]
        :rtype: List[int]
        """
        
        s=[]
        for i in range(0,len(findNums)):
            p=0
            for n in range(nums.index(findNums[i]),len(nums)):
                 if findNums[i]<nums[n]:
                     s.append(nums[n])
                     p=1
                     break
            if p==0:
                s.append(-1)
        return s

2 0
原创粉丝点击