【LEETCODE】220-Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array 

such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.


参考:

http://bookshadow.com/weblog/2015/06/03/leetcode-contains-duplicate-iii/

用这个例子,返回值是false,应该是true呢

nums=[10,2,40,3,60,4]
k=4
t=3


如果找不到collections,记得import collections

http://stackoverflow.com/questions/15711755/converting-dict-to-ordereddict


不可不知的Python模块: collections

http://www.zlovezl.cn/articles/collections-in-python/

在Python中,dict这个数据结构由于hash的特性,是无序的,这在有的时候会给我们带来一些麻烦, 幸运的是,collections模块为我们提供了OrderedDict,当你要获得一个有序的字典对象时,用它就对了。


参考:

http://yucoding.blogspot.jp/2015/10/leetcode-question-contains-duplicate-iii.html


asking for whether two indices exists ... , we have to search the array in some way

two constrains:  one is related to element values in the array, the other is related to indices.  So, we have to consider the two at the same time

Since we have already sorted the array, if the value difference between P[0] and P[1] is greater than t, the value difference between P[0] and P[2], P[3] ... P[end] must also greater than t. 

If current comparison is not satisfied, all the other comparisons are not even necessary. We can move P[0] to next element.

While on the other hand, if the value difference <= t, but the index difference > k, we still have to  keep the current P[i] and move P[j] until meets the above scenario or the end of the array.


nums=[10,2,40,3,60,4]

zip(nums, range(len(nums)))

⬇️

[(10, 0), (2, 1), (40, 2), (3, 3), (60, 4), (4, 5)]


http://stackoverflow.com/questions/16310015/what-does-this-mean-key-lambda-x-x1

key=lambda x: x[0]

⬇️

def element_1(x):
      return x[0]

sort(mylist, key=lambda x: x[0]) sorts mylist based on the value of key as applied to each element of the list

max() will return the element in that array whose second element (x[1]) is larger than all of the other elements' second elements

按x[0]排序,并返回x整体




class Solution(object):
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        p=sorted(zip(nums,range(len(nums))),key=lambda x:x[0])
        
        #----Time Limit Exceed---感觉和下面的while一样,为什么会超时呢?
        #for i in range(len(nums)):
        #    for j in range(i+1,len(nums)):
            
        #      if abs(p[i][0]-p[j][0])<=t and abs(p[i][1]-p[j][1])<=k :
        #          return True
        #      elif abs(p[i][0]-p[j][0])>t:
        #          break
        
        #return False
        
        i=0
        while i<len(nums):
            j=i+1
            while j<len(nums):
                if abs(p[i][0]-p[j][0])<=t and abs(p[i][1]-p[j][1])<=k:
                    return True
                elif abs(p[i][0]-p[j][0])>t:
                    break
                else:
                    j+=1
            i+=1
        return False




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值