当前位置:网站首页>[daily training] 1200 Minimum absolute difference
[daily training] 1200 Minimum absolute difference
2022-07-05 08:37:00 【Puppet__】
subject
Here's an array of integers arr, Each of these elements is inequality .
Please find all the elements with the least absolute difference , And return in ascending order .
Example 1:
Input :arr = [4,2,1,3]
Output :[[1,2],[2,3],[3,4]]
Example 2:
Input :arr = [1,3,6,10,15]
Output :[[1,3]]
Example 3:
Input :arr = [3,8,-10,23,19,-4,-14,27]
Output :[[-14,-10],[19,23],[23,27]]
Tips :
2 <= arr.length <= 105
-106 <= arr[i] <= 106
Code
package dayLeetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class dayleetcode1200 {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> ansList = new ArrayList<>();
int maxCnt = Integer.MAX_VALUE;
for (int i = 0; i < arr.length - 1; i++){
int tmp = arr[i + 1] - arr[i];
maxCnt = Math.min(tmp, maxCnt);
}
for (int i = 0; i < arr.length - 1; i++){
int tmp = arr[i + 1] - arr[i];
if (tmp == maxCnt){
List<Integer> tmpList = new ArrayList<>();
tmpList.add(arr[i]);
tmpList.add(arr[i + 1]);
ansList.add(tmpList);
}
}
return ansList;
}
public static void main(String[] args) {
dayleetcode1200 obj = new dayleetcode1200();
System.out.println(obj.minimumAbsDifference(new int[]{
4, 2, 1, 3}));
}
}
边栏推荐
- Several problems to be considered and solved in the design of multi tenant architecture
- Example 006: Fibonacci series
- C language data type replacement
- [NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
- Negative pressure generation of buck-boost circuit
- 猜谜语啦(8)
- Example 007: copy data from one list to another list.
- Apaas platform of TOP10 abroad
- Low code platform | apaas platform construction analysis
- Installation and use of libjpeg and ligpng
猜你喜欢
Guess riddles (142)
剑指 Offer 05. 替换空格
Guess riddles (2)
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Pytorch entry record
Sword finger offer 06 Print linked list from end to end
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
How apaas is applied in different organizational structures
Guess riddles (6)
Arduino operation stm32
随机推荐
99 multiplication table (C language)
Run菜单解析
Array integration initialization (C language)
Daily question - input a date and output the day of the year
Agile project management of project management
Speech recognition learning summary
L298N module use
leetcode - 445. Add two numbers II
图解八道经典指针笔试题
MySQL之MHA高可用集群
Guess riddles (9)
Synchronization of QT multithreading
QEMU STM32 vscode debugging environment configuration
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
Stm32--- systick timer
DCDC circuit - function of bootstrap capacitor
Negative pressure generation of buck-boost circuit
Briefly talk about the identification protocol of mobile port -bc1.2
Void* C is a carrier for realizing polymorphism