Skip to content

Commit ccd7d90

Browse files
committed
modify code
1 parent 7b5faf9 commit ccd7d90

File tree

4 files changed

+202
-47
lines changed

4 files changed

+202
-47
lines changed

src/class03/Code01_BSExist.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,36 @@
44

55
public class Code01_BSExist {
66

7-
public static boolean exist(int[] sortedArr, int num) {
8-
if (sortedArr == null || sortedArr.length == 0) {
7+
// arr保证有序
8+
public static boolean find(int[] arr, int num) {
9+
if (arr == null || arr.length == 0) {
910
return false;
1011
}
1112
int L = 0;
12-
int R = sortedArr.length - 1;
13-
int mid = 0;
14-
// L..R
15-
while (L < R) { // L..R 至少两个数的时候
16-
mid = L + ((R - L) >> 1);
17-
if (sortedArr[mid] == num) {
13+
int R = arr.length - 1;
14+
while (L <= R) {
15+
int mid = (L + R) / 2;
16+
if (arr[mid] == num) {
1817
return true;
19-
} else if (sortedArr[mid] > num) {
20-
R = mid - 1;
21-
} else {
18+
} else if (arr[mid] < num) {
2219
L = mid + 1;
20+
} else {
21+
R = mid - 1;
2322
}
2423
}
25-
return sortedArr[L] == num;
24+
return false;
2625
}
27-
26+
2827
// for test
2928
public static boolean test(int[] sortedArr, int num) {
30-
for(int cur : sortedArr) {
31-
if(cur == num) {
29+
for (int cur : sortedArr) {
30+
if (cur == num) {
3231
return true;
3332
}
3433
}
3534
return false;
3635
}
37-
38-
36+
3937
// for test
4038
public static int[] generateRandomArray(int maxSize, int maxValue) {
4139
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
@@ -44,7 +42,7 @@ public static int[] generateRandomArray(int maxSize, int maxValue) {
4442
}
4543
return arr;
4644
}
47-
45+
4846
public static void main(String[] args) {
4947
int testTime = 500000;
5048
int maxSize = 10;
@@ -54,7 +52,8 @@ public static void main(String[] args) {
5452
int[] arr = generateRandomArray(maxSize, maxValue);
5553
Arrays.sort(arr);
5654
int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
57-
if (test(arr, value) != exist(arr, value)) {
55+
if (test(arr, value) != find(arr, value)) {
56+
System.out.println("出错了!");
5857
succeed = false;
5958
break;
6059
}

src/class03/Code02_BSNearLeft.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
public class Code02_BSNearLeft {
66

7-
// 在arr上,找满足>=value的最左位置
8-
public static int nearestIndex(int[] arr, int value) {
7+
// arr有序的,>=num 最左
8+
public static int mostLeftNoLessNumIndex(int[] arr, int num) {
9+
if (arr == null || arr.length == 0) {
10+
return -1;
11+
}
912
int L = 0;
1013
int R = arr.length - 1;
11-
int index = -1; // 记录最左的对号
12-
while (L <= R) { // 至少一个数的时候
13-
int mid = L + ((R - L) >> 1);
14-
if (arr[mid] >= value) {
15-
index = mid;
14+
int ans = -1;
15+
while (L <= R) {
16+
int mid = (L + R) / 2;
17+
if (arr[mid] >= num) {
18+
ans = mid;
1619
R = mid - 1;
1720
} else {
1821
L = mid + 1;
1922
}
2023
}
21-
return index;
24+
return ans;
2225
}
2326

2427
// for test
@@ -39,7 +42,7 @@ public static int[] generateRandomArray(int maxSize, int maxValue) {
3942
}
4043
return arr;
4144
}
42-
45+
4346
// for test
4447
public static void printArray(int[] arr) {
4548
if (arr == null) {
@@ -60,11 +63,11 @@ public static void main(String[] args) {
6063
int[] arr = generateRandomArray(maxSize, maxValue);
6164
Arrays.sort(arr);
6265
int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
63-
if (test(arr, value) != nearestIndex(arr, value)) {
66+
if (test(arr, value) != mostLeftNoLessNumIndex(arr, value)) {
6467
printArray(arr);
6568
System.out.println(value);
6669
System.out.println(test(arr, value));
67-
System.out.println(nearestIndex(arr, value));
70+
System.out.println(mostLeftNoLessNumIndex(arr, value));
6871
succeed = false;
6972
break;
7073
}

src/class03/Code04_BSAwesome.java

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,90 @@
22

33
public class Code04_BSAwesome {
44

5-
public static int getLessIndex(int[] arr) {
5+
// arr 整体无序
6+
// arr 相邻的数不相等!
7+
public static int oneMinIndex(int[] arr) {
68
if (arr == null || arr.length == 0) {
7-
return -1; // no exist
9+
return -1;
810
}
9-
if (arr.length == 1 || arr[0] < arr[1]) {
11+
int N = arr.length;
12+
if (N == 1) {
1013
return 0;
1114
}
12-
if (arr[arr.length - 1] < arr[arr.length - 2]) {
13-
return arr.length - 1;
14-
}
15-
int left = 1;
16-
int right = arr.length - 2;
17-
int mid = 0;
18-
while (left < right) {
19-
mid = (left + right) / 2;
20-
if (arr[mid] > arr[mid - 1]) {
21-
right = mid - 1;
22-
} else if (arr[mid] > arr[mid + 1]) {
23-
left = mid + 1;
24-
} else {
15+
if (arr[0] < arr[1]) {
16+
return 0;
17+
}
18+
if (arr[N - 1] < arr[N - 2]) {
19+
return N - 1;
20+
}
21+
int L = 0;
22+
int R = N - 1;
23+
// L...R 肯定有局部最小
24+
while (L < R - 1) {
25+
int mid = (L + R) / 2;
26+
if (arr[mid] < arr[mid - 1] && arr[mid] < arr[mid + 1]) {
2527
return mid;
28+
} else {
29+
if (arr[mid] > arr[mid - 1]) {
30+
R = mid - 1;
31+
} else {
32+
L = mid + 1;
33+
}
34+
}
35+
}
36+
return arr[L] < arr[R] ? L : R;
37+
}
38+
39+
// 生成随机数组,且相邻数不相等
40+
public static int[] randomArray(int maxLen, int maxValue) {
41+
int len = (int) (Math.random() * maxLen);
42+
int[] arr = new int[len];
43+
if (len > 0) {
44+
arr[0] = (int) (Math.random() * maxValue);
45+
for (int i = 1; i < len; i++) {
46+
do {
47+
arr[i] = (int) (Math.random() * maxValue);
48+
} while (arr[i] == arr[i - 1]);
49+
}
50+
}
51+
return arr;
52+
}
53+
54+
// 也用于测试
55+
public static boolean check(int[] arr, int minIndex) {
56+
if (arr.length == 0) {
57+
return minIndex == -1;
58+
}
59+
int left = minIndex - 1;
60+
int right = minIndex + 1;
61+
boolean leftBigger = left >= 0 ? arr[left] > arr[minIndex] : true;
62+
boolean rightBigger = right < arr.length ? arr[right] > arr[minIndex] : true;
63+
return leftBigger && rightBigger;
64+
}
65+
66+
public static void printArray(int[] arr) {
67+
for (int num : arr) {
68+
System.out.print(num + " ");
69+
}
70+
System.out.println();
71+
}
72+
73+
public static void main(String[] args) {
74+
int maxLen = 100;
75+
int maxValue = 200;
76+
int testTime = 1000000;
77+
System.out.println("测试开始");
78+
for (int i = 0; i < testTime; i++) {
79+
int[] arr = randomArray(maxLen, maxValue);
80+
int ans = oneMinIndex(arr);
81+
if (!check(arr, ans)) {
82+
printArray(arr);
83+
System.out.println(ans);
84+
break;
2685
}
2786
}
28-
return left;
87+
System.out.println("测试结束");
88+
2989
}
3090

3191
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package class03;
2+
3+
import java.util.HashMap;
4+
import java.util.TreeMap;
5+
6+
public class Code05_HashMapTreeMap {
7+
8+
public static class Node {
9+
public int value;
10+
11+
public Node(int v) {
12+
value = v;
13+
}
14+
}
15+
16+
// (K V)表
17+
public static void main(String[] args) {
18+
HashMap<String, String> map = new HashMap<>();
19+
map.put("zuochengyun", "我是左程云");
20+
System.out.println(map.containsKey("zuochengyun"));
21+
System.out.println(map.containsKey("zuo"));
22+
System.out.println(map.get("zuochengyun"));
23+
24+
map.put("zuochengyun", "他是左程云");
25+
System.out.println(map.get("zuochengyun"));
26+
27+
// map.remove("zuochengyun");
28+
// System.out.println(map.containsKey("zuochengyun"));
29+
// System.out.println(map.get("zuochengyun"));
30+
31+
String test1 = "zuochengyun";
32+
String test2 = "zuochengyun";
33+
System.out.println(map.containsKey(test1));
34+
System.out.println(map.containsKey(test2));
35+
36+
HashMap<Integer, String> map2 = new HashMap<>();
37+
map2.put(1234567, "我是1234567");
38+
39+
Integer a = 1234567;
40+
Integer b = 1234567;
41+
42+
System.out.println(a == b);
43+
System.out.println(map2.containsKey(a));
44+
System.out.println(map2.containsKey(b));
45+
46+
Node node1 = new Node(1);
47+
Node node2 = new Node(1);
48+
HashMap<Node, String> map3 = new HashMap<>();
49+
map3.put(node1, "我进来了!");
50+
System.out.println(map3.containsKey(node1));
51+
System.out.println(map3.containsKey(node2));
52+
53+
System.out.println("===================");
54+
55+
TreeMap<Integer, String> treeMap1 = new TreeMap<>();
56+
57+
treeMap1.put(3, "我是3");
58+
treeMap1.put(0, "我是3");
59+
treeMap1.put(7, "我是3");
60+
treeMap1.put(2, "我是3");
61+
treeMap1.put(5, "我是3");
62+
treeMap1.put(9, "我是3");
63+
64+
System.out.println(treeMap1.containsKey(7));
65+
System.out.println(treeMap1.containsKey(6));
66+
System.out.println(treeMap1.get(3));
67+
68+
treeMap1.put(3, "他是3");
69+
System.out.println(treeMap1.get(3));
70+
71+
treeMap1.remove(3);
72+
System.out.println(treeMap1.get(3));
73+
74+
System.out.println(treeMap1.firstKey());
75+
System.out.println(treeMap1.lastKey());
76+
// <=5 离5最近的key告诉我
77+
System.out.println(treeMap1.floorKey(5));
78+
// <=6 离6最近的key告诉我
79+
System.out.println(treeMap1.floorKey(6));
80+
// >=5 离5最近的key告诉我
81+
System.out.println(treeMap1.ceilingKey(5));
82+
// >=6 离6最近的key告诉我
83+
System.out.println(treeMap1.ceilingKey(6));
84+
85+
// Node node3 = new Node(3);
86+
// Node node4 = new Node(4);
87+
// TreeMap<Node, String> treeMap2 = new TreeMap<>();
88+
// treeMap2.put(node3, "我是node3");
89+
// treeMap2.put(node4, "我是node4");
90+
91+
}
92+
93+
}

0 commit comments

Comments
 (0)