zoukankan      html  css  js  c++  java
  • Java for 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.

    解题思路:

    暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可以返回一个在某个范围的subSet,同时判断这个subSet是否为空即可解决,主要,本题需要使用long类型!

    JAVA实现如下:

    import java.util.*;
    public class Solution {
        public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {  
            if(nums==null || nums.length<2||k<1 || t<0) 
            	return false;  
            SortedSet<Long> set = new TreeSet<Long>();  
            for(int j=0; j<nums.length; j++) {  
                SortedSet<Long> subSet =  set.subSet((long)nums[j]-t, (long)nums[j]+t+1);  
                if(!subSet.isEmpty()) 
                	return true;  
                if(j>=k) 
                    set.remove((long)nums[j-k]);   
                set.add((long)nums[j]);       
            }  
            return false;  
        }
    }
    
  • 相关阅读:
    相关系数
    T检验
    Python模块常用的几种安装方式
    DOM与SAX读取XML方式的不同
    Base64编码
    node.js网页爬虫
    Node.js Express 框架
    Node.js Web 模块
    Node.js GET/POST请求
    Node.js 常用工具
  • 原文地址:https://www.cnblogs.com/tonyluis/p/4578657.html
Copyright © 2011-2022 走看看