My solution beats 91.31 % of java submissions WITHOUT HashSet


  • 0
    T
    public class Solution {
        public int findPairs(int[] nums, int k) {
            Arrays.sort(nums);
            int count = 0;
            for (int i = 0; i < nums.length; i++) {
                if (i > 0 && nums[i - 1] == nums[i]) continue;
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[i] + k == nums[j]) {
                        count++;
                        break;
                    }
                    if (nums[i] + k < nums[j])
                        break;
                }
            }
            return count;
        }
    }
    

Log in to reply
 

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