当前位置:网站首页>Force buckle 1200 Minimum absolute difference
Force buckle 1200 Minimum absolute difference
2022-07-05 19:28:00 【Ruthless young Fisherman】
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
Input :arr = [4,2,1,3]
Output :[[1,2],[2,3],[3,4]]
Input :arr = [1,3,6,10,15]
Output :[[1,3]]
Input :arr = [3,8,-10,23,19,-4,-14,27]
Output :[[-14,-10],[19,23],[23,27]]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-absolute-difference
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: simulation
Java Realization
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
int n = arr.length;
Arrays.sort(arr);
List<List<Integer>> res = new ArrayList<>();
int min = Integer.MAX_VALUE;
for (int i = 0; i < n - 1; i++) {
int cur = Math.abs(arr[i] - arr[i + 1]);
if (cur == min) {
res.add(Arrays.asList(arr[i], arr[i + 1]));
} else if (cur < min) {
min = cur;
res = new ArrayList<>();
res.add(Arrays.asList(arr[i], arr[i + 1]));
}
}
return res;
}
}

边栏推荐
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- 块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
- What are general items
- 开源 SPL 消灭数以万计的数据库中间表
- Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
- IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
- Reptile exercises (II)
- Android面试,android音视频开发
- What is the core value of testing?
- How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
猜你喜欢

使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)

Summer Challenge database Xueba notes, quick review of exams / interviews~

C# 语言的高级应用
How MySQL queries and modifies JSON data

软件测试工程师是做什么的?待遇前景怎么样?

力扣 729. 我的日程安排表 I
PHP uses ueditor to upload pictures and add watermarks

毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用

Postman核心功能解析-参数化和测试报告

众昂矿业:2022年全球萤石行业市场供给现状分析
随机推荐
面试官:Redis中集合数据类型的内部实现方式是什么?
司空见惯 - 英雄扫雷鼠
MySql中的longtext字段的返回问题及解决
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
[AI framework basic technology] automatic derivation mechanism (autograd)
Necessary skills for interview in large factories, 2022android will not die, I will not fall
众昂矿业:2022年全球萤石行业市场供给现状分析
Postman核心功能解析-参数化和测试报告
Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
Fundamentals of shell programming (Part 8: branch statements -case in)
Django uses mysqlclient service to connect and write to the database
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
Password reset of MariaDB root user and ordinary user
MySQL中字段类型为longtext的值导出后显示二进制串方式
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
如何在2022年更明智地应用智能合约?
Realizing deep learning framework from zero -- LSTM from theory to practice [practice]