zoukankan      html  css  js  c++  java
  • 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 absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

    Example 1:

    Input: nums = [1,2,3,1], k = 3, t = 0
    Output: true
    

    Example 2:

    Input: nums = [1,0,1,1], k = 1, t = 2
    Output: true
    

    Example 3:

    Input: nums = [1,5,9,1,5,9], k = 2, t = 3
    Output: false

    Approach #1: C++. [Brute Froce]

    class Solution {
    public:
        bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
            int len = nums.size();
            for (int i = 0; i < len; ++i) {
                for (int j = i + 1; j - i <= k && j < len; ++j) {
                    if (abs((long)nums[j] - (long)nums[i]) <= (long)t) return true;
                }
            }
            return false;
        }
    };
    

      

    Approach #2: Java. [TreeSet]

    class Solution {
        public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
            if (nums == null || nums.length == 0 || k <= 0)
                return false;
            
            TreeSet<Integer> values = new TreeSet<>();
            for (int i = 0; i < nums.length; ++i) {
                if (i > k) values.remove(nums[i-k-1]);
                Integer ceiling = values.ceiling(nums[i]);
                Integer floor = values.floor(nums[i]);
                if ((ceiling != null && (long)ceiling - (long)nums[i] <= t) || 
                    (floor != null && (long)nums[i] - (long)floor <= t))
                    return true;
                values.add(nums[i]);
            }
            return false;
        }
    }
    

      

    Approach #3 Python. [bucket]

    class Solution(object):
        def containsNearbyAlmostDuplicate(self, nums, k, t):
            """
            :type nums: List[int]
            :type k: int
            :type t: int
            :rtype: bool
            """
            if t < 0: return False
            n = len(nums)
            d = {}
            w = t + 1
            for i in xrange(n):
                m = nums[i] / w
                if m in d:
                    return True
                if m-1 in d and abs(nums[i] - d[m-1]) < w:
                    return True
                if m+1 in d and abs(nums[i] - d[m+1]) < w:
                    return True
                d[m] = nums[i]
                if i >= k: del d[nums[i-k] / w]
            return False
    

      

    Analysis:

    In the case one we use brute froce to traverse the array. [Time Limit Exceded]

    In the case two we use the mothod of floor and ceiling to find the element which in the range of index and satisfy the absolute difference between nums[i] and nums[j] is at most t.

    In the case three we use the bucket to control the range of index.

    TreeSet in Java (https://www.geeksforgeeks.org/treeset-in-java-with-examples/)

    TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface. It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    登录模块(前端bookstrapValidator校验+加密+后台加密+后台验证)
    spring+springmvc+mybatis+redis 实现两重数据缓存
    spring+springmvc+mybatis+redis实现缓存
    获取网页上的所有QQ号码,并生成exel报表
    单点登录(因为原理一样,所以没有实现注销)
    solr 基本命令二(权重查找)
    solr 搭建 (基于solr-5.0.0)
    OC 添加导航栏item
    xcode 一些三方库版本警告 iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.2.99.
    Swift UITextView设置富文本点击, 取消一切点击事件(放大镜/复制粘贴/删除等等)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10023365.html
Copyright © 2011-2022 走看看