当前位置:网站首页>[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}));
}
}
边栏推荐
- Guess riddles (11)
- Count the number of inputs (C language)
- MATLAB小技巧(28)模糊综合评价
- 每日一题——替换空格
- 猜谜语啦(5)
- Meizu Bluetooth remote control temperature and humidity access homeassistant
- 287. 寻找重复数-快慢指针
- [NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
- Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
- Guess riddles (4)
猜你喜欢

Example 008: 99 multiplication table

STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)

Business modeling | process of software model
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

Typical low code apaas manufacturer cases

Guess riddles (7)

Run menu analysis

STM32 single chip microcomputer -- debug in keil5 cannot enter the main function

Arduino operation stm32

UE像素流,来颗“减肥药”吧!
随机推荐
Run菜单解析
Speech recognition learning summary
STM32 single chip microcomputer - external interrupt
Void* C is a carrier for realizing polymorphism
Guess riddles (10)
猜谜语啦(2)
Apaas platform of TOP10 abroad
Guess riddles (11)
第十八章 使用工作队列管理器(一)
MATLAB小技巧(28)模糊综合评价
Arduino operation stm32
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Business modeling of software model | vision
Business modeling | process of software model
Briefly talk about the identification protocol of mobile port -bc1.2
Example 009: pause output for one second
Array integration initialization (C language)
Agile project management of project management
99 multiplication table (C language)
QEMU demo makefile analysis