当前位置:网站首页>[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}));
}
}
边栏推荐
- Esphone Feixun DC1 soft change access homeassstant
- Business modeling | process of software model
- [three tier architecture and JDBC summary]
- go依赖注入--google开源库wire
- Sword finger offer 06 Print linked list from end to end
- Various types of questions judged by prime numbers within 100 (C language)
- STM32 --- GPIO configuration & GPIO related library functions
- U8g2 drawing
- Stm32--- systick timer
- 2022.7.4-----leetcode. one thousand and two hundred
猜你喜欢
STM32---IIC
Lori remote control LEGO motor
[noi simulation] juice tree (tree DP)
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Arduino burning program and Arduino burning bootloader
Low code platform | apaas platform construction analysis
MySQL之MHA高可用集群
[three tier architecture]
How apaas is applied in different organizational structures
Guess riddles (8)
随机推荐
Count the number of inputs (C language)
Guess riddles (8)
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Guess riddles (6)
STM32 --- NVIC interrupt
Business modeling | process of software model
剑指 Offer 06. 从尾到头打印链表
Xrosstools tool installation for X-Series
Guess riddles (5)
关于线性稳压器的五个设计细节
【日常训练】1200. 最小绝对差
【三层架构】
猜谜语啦(6)
MATLAB小技巧(28)模糊综合评价
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
【日常训练--腾讯精选50】557. 反转字符串中的单词 III
Classification of plastic surgery: short in long long long
猜谜语啦(9)
实例008:九九乘法表