MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / near1

Method near1

src/class129/Code04_FindNear.java:43–61  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41
42 // 有序表的实现
43 public static void near1() {
44 TreeSet<int[]> set = new TreeSet<>((a, b) -> a[1] - b[1]);
45 for (int i = n; i >= 1; i--) {
46 to1[i] = 0;
47 dist1[i] = 0;
48 to2[i] = 0;
49 dist2[i] = 0;
50 int[] cur = new int[] { i, arr[i] };
51 int[] p1 = set.floor(cur);
52 int[] p2 = p1 != null ? set.floor(new int[] { p1[0], p1[1] - 1 }) : null;
53 int[] p3 = set.ceiling(cur);
54 int[] p4 = p3 != null ? set.ceiling(new int[] { p3[0], p3[1] + 1 }) : null;
55 update(i, p1 != null ? p1[0] : 0);
56 update(i, p2 != null ? p2[0] : 0);
57 update(i, p3 != null ? p3[0] : 0);
58 update(i, p4 != null ? p4[0] : 0);
59 set.add(cur);
60 }
61 }
62
63 // 数组手搓双向链表的实现
64 public static void near2() {

Callers 1

checkMethod · 0.95

Calls 2

updateMethod · 0.95
addMethod · 0.45

Tested by

no test coverage detected