当前位置:网站首页>[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}));
}
}
边栏推荐
- Speech recognition learning summary
- Business modeling of software model | overview
- 319. 灯泡开关
- 696. 计数二进制子串
- 關於線性穩壓器的五個設計細節
- MySQL之MHA高可用集群
- 实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
- Several problems to be considered and solved in the design of multi tenant architecture
- 287. 寻找重复数-快慢指针
- Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
猜你喜欢

Agile project management of project management

STM32---ADC

猜谜语啦(5)
![[noi simulation] juice tree (tree DP)](/img/19/bc71e8dc3958e4cb87b31423a74617.png)
[noi simulation] juice tree (tree DP)
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?

Typical low code apaas manufacturer cases

Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)

Guess riddles (142)

实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。

Business modeling of software model | vision
随机推荐
每日一题——输入一个日期,输出它是该年的第几天
实例009:暂停一秒输出
EA introduction notes
Guess riddles (4)
go依赖注入--google开源库wire
Count the number of inputs (C language)
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Run菜单解析
STM32 outputs 1PPS with adjustable phase
【三层架构】
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Typical low code apaas manufacturer cases
猜谜语啦(2)
Guess riddles (6)
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
Sword finger offer 05 Replace spaces
如何写Cover Letter?
STM32 single chip microcomputer - external interrupt
Various types of questions judged by prime numbers within 100 (C language)