-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1200.最小绝对差.cs
77 lines (74 loc) · 1.46 KB
/
1200.最小绝对差.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
/*
* @lc app=leetcode.cn id=1200 lang=csharp
*
* [1200] 最小绝对差
*
* https://leetcode.cn/problems/minimum-absolute-difference/description/
*
* algorithms
* Easy (68.31%)
* Likes: 64
* Dislikes: 0
* Total Accepted: 22.4K
* Total Submissions: 32.7K
* Testcase Example: '[4,2,1,3]'
*
* 给你个整数数组 arr,其中每个元素都 不相同。
*
* 请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。
*
*
*
* 示例 1:
*
* 输入:arr = [4,2,1,3]
* 输出:[[1,2],[2,3],[3,4]]
*
*
* 示例 2:
*
* 输入:arr = [1,3,6,10,15]
* 输出:[[1,3]]
*
*
* 示例 3:
*
* 输入:arr = [3,8,-10,23,19,-4,-14,27]
* 输出:[[-14,-10],[19,23],[23,27]]
*
*
*
*
* 提示:
*
*
* 2 <= arr.length <= 10^5
* -10^6 <= arr[i] <= 10^6
*
*
*/
// @lc code=start
public class Solution {
// 60 56 192 54.6;
public IList<IList<int>> MinimumAbsDifference(int[] arr) {
Array.Sort(arr);
int minDif = int.MaxValue;
for(int i = 1; i < arr.Length; ++i){
minDif = Math.Min(minDif, arr[i] - arr[i-1]);
}
var ret = new List<IList<int>>();
for(int i = 1; i < arr.Length; ++i){
if(arr[i] - arr[i-1] == minDif){
ret.Add(new List<int>(){arr[i-1], arr[i]});
}
}
return ret;
}
}
// @lc code=end
/*
[4,2,1,3]\n
[1,3,6,10,15]\n
[3,8,-10,23,19,-4,-14,27]
*/