当前位置:网站首页>LeetCode 1200. Minimum absolute difference

LeetCode 1200. Minimum absolute difference

2022-07-06 06:23:00 Sasakihaise_

1200. Minimum absolute difference

 

【 analysis 】 Judge after sorting .

List and Set Empty available clear Or just re new One , The efficiency of the two is the same .

However, it should be noted that when defining a large value, it is best to use (int)1e9 such , Do not use 1<<30.

class Solution {
    public List<List<Integer>> minimumAbsDifference(int[] arr) {
        Arrays.sort(arr);
        List<List<Integer>> ans = new ArrayList();
        int min = (int)1e7;
        for (var i = 1; i < arr.length; i++) {
            int t = arr[i] - arr[i - 1];
            if (t < min) {
                min = t;
                ans.clear();
            } 
            if (t == min) {
                ArrayList<Integer> pair = new ArrayList();
                pair.add(arr[i - 1]);
                pair.add(arr[i]);
                ans.add(pair);
            }
        }
        return ans;
    }
}

 

 

原网站

版权声明
本文为[Sasakihaise_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060601579644.html