496Next Greater Element

one day one cannon

or 

one week five cannons


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.

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:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.


#读懂题目也是关键啊

#coding=utf-8
class Soulution(object):
    def nextGreaterElement(self,findNums,nums):#这个函数是错误的,没有理解题目
        result=[]
        for x in findNums:
            #y=nums[nums.count(x)+1]:
            l=nums.index(x)
            if (l==len(nums)-1)or nums[l+1]<=x:
                result.append(-1)
            else:
                result.append(nums[l+1])
        return result

    def nextGreaterElement1(self,findnums,nums):#这个函数是正确的,可以解决nums为[4,3,2,1,5]的形式
        def find(num):
            for tem in nums[nums.index(num):]:
                if tem>num:
                    return tem
            return -1

        return map(find,findnums)



X=Soulution()
print X.nextGreaterElement([2,4],[4,3,2,1,5])
print X.nextGreaterElement1([2,4],[4,3,2,1,5])



涉及到的相关知识,map内置函数。
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。
举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个list [1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map()实现如下:
很方便
现在,我们用Python代码实现:

>>> deff(x):
...   returnx * x
...
>>> map(f, [1,2, 3,4, 5,6, 7,8, 9])
[1,4, 9,16, 25,36, 49,64, 81]
map()传入的第一个参数是f,即函数对象本身。
你可能会想,不需要map()函数,写一个循环,也可以计算出结果:

L =[]
for n in [1,2, 3,4, 5,6, 7,8, 9]:
  L.append(f(n))
print L
的确可以,但是,从上面的循环代码,能一眼看明白“把f(x)作用在list的每一个元素并把结果生成一个新的list”吗?
所以,map()作为高阶函数,事实上它把运算规则抽象了,因此,我们不但可以计算简单的f(x)=x2,还可以计算任意复杂的函数,比如,把这个list所有数字转为字符串:

>>> map(str, [1,2, 3,4, 5,6, 7,8, 9])
['1','2', '3','4', '5','6', '7','8', '9']


再看reduce的用法。reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
比方说对一个序列求和,就可以用reduce实现:

>>> defadd(x, y):
...   returnx + y
...
>>> reduce(add, [1,3, 5,7, 9])
25


如果考虑到字符串str也是一个序列,对上面的例子稍加改动,配合map(),我们就可以写出把str转换为int的函数:

>>> deffn(x, y):
...   returnx * 10 + y
...
>>> defchar2num(s):
...   return{'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}[s]
...
>>> reduce(fn,map(char2num, '13579'))
13579
整理成一个str2int的函数就是:

def str2int(s):
  deffn(x, y):
    returnx * 10 + y
  defchar2num(s):
    return{'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}[s]
  returnreduce(fn, map(char2num, s))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值