java 5ms solution


  • 0
    Z

    public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
    int length=String.valueOf(k).length();
    double[] result=new double[nums.length];
    double step=k;
    double p1=Math.pow(10, -length);
    double p2=Math.pow(10, length);
    for(int i=0;i<nums.length;i++){
    result[i]=nums[i]+ip1;
    if(result[i]<0)
    result[i]=nums[i]-i
    p1;
    }
    Arrays.sort(result);
    for(int i=0;i<result.length-1;i++){
    if((((int)result[i+1]==(int)result[i]))&&((result[i+1]-result[i])*p2<step+0.1))
    return true;
    }
    return false;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.