我是靠谱客的博主 多情鸵鸟,最近开发中收集的这篇文章主要介绍【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




最后

以上就是多情鸵鸟为你收集整理的【LEETCODE】220-Contains Duplicate III的全部内容,希望文章能够帮你解决【LEETCODE】220-Contains Duplicate III所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部