-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode719.java
More file actions
35 lines (31 loc) · 900 Bytes
/
Copy pathleetcode719.java
File metadata and controls
35 lines (31 loc) · 900 Bytes
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
package leetcode;
import java.util.*;
public class leetcode719 {
/*
* 超出空间限制
* */
ArrayList<Integer> list=new ArrayList<>();
LinkedList<Integer> num=new LinkedList<>();
public int smallestDistancePair(int[] nums, int k) {
//if (nums.length==0||k<=0) return 0;
helper(nums,0,nums.length-1,2);
Collections.sort(list);
return list.get(k-1).intValue();
}
public void helper(int [] nums,int start,int end,int n){
if (n==0){
list.add(Math.abs(num.get(0)-num.get(1)));
return;
}
for (int i = start; i <=end; i++) {
num.addLast(nums[i]);
helper(nums,i+1,end,n-1);
num.removeLast();
}
}
public static void main(String[] args) {
new leetcode719().smallestDistancePair(new int[]{
1,3,1
},1);
}
}