leetcode 1200. Minimum Absolute Difference
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.Return a list of pairs in ascending order(with respect to pairs), each pair follows
a, b are from arr
a < b
b - a equals to the minimum absolute difference of any two elements in arr
Example 1:
Input: arr =
Output: [,,]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
Example 2:
Input: arr =
Output: []
Example 3:
Input: arr =
Output: [[-14,-10],,]
Constraints:
2 <= arr.length <= 10^5
-10^6 <= arr <= 10^6
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(arr);
int min = arr - arr;
int len = arr.length;
for(int i =0 ; i <len - 1 ; i++){
if( arr -arr < min) min = arr -arr;
}
for(int i =0 ; i <len - 1 ; i++){
List<Integer> list = new ArrayList<>();
if( arr -arr ==min) {
list.add(arr);
list.add(arr);
}
if(list.size() == 2){
res.add(list);
list = new ArrayList<>();
}
if(list.size() == 2){
res.add(list);
list = new ArrayList<>();
}
}
return res;
}
}
页:
[1]