3ms java solution


  • 0
    F
    public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) { 
    	    int []num = nums.clone();
    	    Arrays.sort(num);
    	    for(int i = 1 ; i < nums.length;i++){
    	         if(num[i-1]==num[i]){
    	            List<Integer> list = new ArrayList<Integer>();
    	            for(int j = 0 ; j < nums.length ; j++){
    	                if(nums[j] == num[i]){
    	                   list.add(j);
    	                }
    	            }
    	            for(int j = 1 ; j < list.size(); j++){
    	                if(list.get(j) - list.get(j-1) <= k)
    	                    return true;
    	            }
    	         }
    	    }
    	    return false;
    	}
    }
    

Log in to reply
 

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