Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f658666

Browse files
committed
8/27
1 parent dc24418 commit f658666

File tree

9 files changed

+244
-15
lines changed

9 files changed

+244
-15
lines changed

5/#KSmallest.java#

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* find the k smallest numbers in an unsorted integer array. The returned numbers should be in ascending order. */
2+
/* A = {3,4,1,2,5}; K = 3; => 3 smallest numbers {1,2,3} */
3+
4+
import java.util.*;
5+
6+
public class KSmallest {
7+
8+
// method 1: K sized max heap 最大堆
9+
public int[] kSmallestI(int[] array, int k) {
10+
if (array.length == 0 || k == 0) {
11+
return new int[0];
12+
}
13+
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
14+
@Override
15+
public int compare(Integer o1, Integer o2) {
16+
if (o1.equals(o2)) {
17+
return 0;
18+
}
19+
return o1 > o2 ? -1 : 1;
20+
}
21+
});
22+
for (int i = 0; i < array.length; i++) {
23+
if (i < k) {
24+
maxHeap.offer(array[i]);
25+
} else if (array[i] < maxHeap.peek()) {
26+
maxHeap.poll();
27+
maxHeap.offer(array[i]);
28+
}
29+
}
30+
int[] result = new int[k];
31+
for (int i = k - 1; i >= 0; i--) {
32+
result[i] = maxHeap.poll();
33+
}
34+
return result;
35+
}
36+
37+
38+
// methor 2: quick select 快选
39+
public int[] kSmallestII(int[] array, int k) {
40+
if (array.length == 0 || k == 0) {
41+
return new int[0];
42+
}
43+
quickSelect(array, 0, array.length - 1, k - 1); // [3,8,5,1], 0, 3, 2
44+
System.out.println(Arrays.toString(array));
45+
int[] result = Arrays.copyOf(array, k); // K是新数组的大小
46+
Arrays.sort(result); // 整理排序result数组
47+
return result;
48+
}
49+
50+
private void quickSelect(int[] array, int left, int right, int target) {
51+
// System.out.println(target); // 2
52+
int mid = partition(array, left, right); // partition([3,8,5,1], 0, 3)
53+
if (mid == target) { //
54+
return;
55+
} else if (target < mid) {
56+
quickSelect(array, left, mid - 1, target);
57+
} else {
58+
quickSelect(array, mid + 1, right, target);
59+
}
60+
}
61+
62+
private int partition (int[] array, int left, int right) {
63+
int pivot = array[right]; // 1
64+
int start = left; // 0
65+
int end = right - 1; // 2
66+
while (start <= end) { // [0,2] [
67+
if (array[start] < pivot) { // [3,1]
68+
start++; // -
69+
} else if (array[end] >= pivot) { // [5,1]
70+
end--; // end = 1
71+
} else {
72+
swap(array, start++, end--);
73+
}
74+
}
75+
swap(array, start, right);
76+
return start;
77+
}
78+
private void swap(int[] array, int a, int b) {
79+
int tmp = array[a];
80+
array[a] = array[b];
81+
array[b] = tmp;
82+
}
83+
84+
public static void main(String[] args) {
85+
KSmallest ks = new KSmallest();
86+
int[] data = {3,4,1,2,5};
87+
System.out.println(Arrays.toString(ks.kSmallestI(data, 3))); // output: [1,2,3]
88+
System.out.println(Arrays.toString(ks.kSmallestII(data, 3)));
89+
}
90+
}
91+
92+
93+
/*
94+
a = [3,8,5,1] k = 3
95+
96+
97+
98+
99+
*/

5/GraphNode.class

386 Bytes
Binary file not shown.

5/GraphNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java.util.*;
2+
3+
public class GraphNode {
4+
public int key;
5+
public List<GraphNode> neighbors;
6+
public GraphNode(int key) {
7+
this.key = key;
8+
this.neighbors = new ArrayList<GraphNode>();
9+
}
10+
}

5/GraphNode.java~

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
public class GraphNode {
4+
public int key;
5+
public List<GraphNode> neighbors;
6+
public GraphNode(int key) {
7+
this.key = key;
8+
this.neighbors = new ArrayList<GraphNode>();
9+
}
10+
11+
}

5/IsBipartite.class

1.95 KB
Binary file not shown.

5/IsBipartite.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* 检查一个 undirected graph(双向的图) 是否是bipartite/bai-pa-tai-t/(就是节点可以分成两组,自己组内的节点是不能连到自己组里面的点) */
2+
3+
import java.util.*;
4+
5+
public class IsBipartite {
6+
public boolean isBipartite(List<GraphNode> graph) {
7+
// use 0 and 1 to denote 2 different groups 使用0和1去【表示】2个不同组
8+
// the map maintains for each node which group it belongs to 对于每个组的节点
9+
HashMap<GraphNode, Integer> visited = new HashMap<GraphNode, Integer>();
10+
11+
// 那个图可以通过一组节点来表示。我们必须从每个节点来用BFS
12+
for (GraphNode node : graph) {
13+
if (!BFS(node, visited)) { return false; } // 如果不是BFS,返回false
14+
}
15+
return true;
16+
}
17+
18+
private boolean BFS(GraphNode node, HashMap<GraphNode, Integer> visited) {
19+
// 如果这个节点已经被检查过,冇必要去再做BFS多次
20+
if (visited.containsKey(node)) { return true; }
21+
Queue<GraphNode> queue = new LinkedList<GraphNode>();
22+
queue.offer(node);
23+
24+
// 开始宽度优先搜索。因为节点未被访问,所以我们能分配这个节点到任何一组。我们默认放入0组。
25+
visited.put(node,0);
26+
while (!queue.isEmpty()) {
27+
GraphNode cur = queue.poll();
28+
// 为当前节点分配组
29+
int curGroup = visited.get(cur); // HashMap字典.get(key) 得到 value
30+
// 为当前节点的邻居分配一个组
31+
int neiGroup = curGroup == 0 ? 1 : 0; // 组的值是0, neiGroup就是1
32+
for (GraphNode nei : cur.neighbors) {
33+
// 如果邻居neighbor未被检查,那么就把邻居放入队列并且选择正确组
34+
if (!visited.containsKey(nei)) {
35+
visited.put(nei, neiGroup);
36+
queue.offer(nei);
37+
} else if (visited.get(nei) != neiGroup) {
38+
// 如果neighbor未被检查,组未匹配到正确的,返回false
39+
return false;
40+
}
41+
}
42+
43+
}
44+
return true;
45+
}
46+
47+
public static void main(String[] args) {
48+
GraphNode n1 = new GraphNode(1);
49+
GraphNode n2 = new GraphNode(2);
50+
GraphNode n3 = new GraphNode(3);
51+
GraphNode n4 = new GraphNode(4);
52+
n1.neighbors.add(n2);
53+
n2.neighbors.add(n3);
54+
n3.neighbors.add(n4);
55+
List<GraphNode> no = new LinkedList<GraphNode>();
56+
no.add(n1);
57+
no.add(n2);
58+
no.add(n3);
59+
no.add(n4);
60+
IsBipartite is = new IsBipartite();
61+
/*
62+
Examples
63+
64+
1 -- 2
65+
66+
/
67+
68+
3 -- 4
69+
70+
is bipartite (1, 3 in group 1 and 2, 4 in group 2).
71+
*/
72+
73+
System.out.println(is.isBipartite(no)); // True
74+
75+
//---------------------------------------------------------
76+
n2.neighbors.add(n4);
77+
/*
78+
1 -- 2
79+
80+
/ |
81+
82+
3 -- 4
83+
84+
is not bipartite.
85+
*/
86+
87+
System.out.println(is.isBipartite(no)); // False
88+
}
89+
}

5/IsBipartite.java~

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* 检查一个 undirected graph(双向的图) 是否是bipartite/bai-pa-tai-t/(就是节点可以分成两组,自己组内的节点是不能连到自己组里面的点) */
2+
3+
import java.util.*;
4+
5+
public class IsBipartite {
6+
public boolean is Bipartite(List<Graph> graph) {
7+
return false;
8+
}
9+
}

5/KSmallest.class

43 Bytes
Binary file not shown.

5/KSmallest.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ public int compare(Integer o1, Integer o2) {
3636

3737

3838
// methor 2: quick select 快选
39-
public int[] kSmallestII(int[] array, int k) {
39+
public int[] kSmallestII(int[] array, int k) {
4040
if (array.length == 0 || k == 0) {
4141
return new int[0];
4242
}
43-
quickSelect(array, 0, array.length - 1, k - 1);
44-
int[] result = Arrays.copyOf(array, k);
45-
Arrays.sort(result);
43+
quickSelect(array, 0, array.length - 1, k - 1); // [3,8,5,1], 0, 3, 2
44+
System.out.println(Arrays.toString(array));
45+
int[] result = Arrays.copyOf(array, k); // K是新数组的大小
46+
Arrays.sort(result); // 整理排序result数组
4647
return result;
4748
}
4849

4950
private void quickSelect(int[] array, int left, int right, int target) {
50-
int mid = partition(array, left, right);
51-
if (mid == target) {
51+
// System.out.println(target); // 2
52+
int mid = partition(array, left, right); // partition([3,8,5,1], 0, 3)
53+
if (mid == target) { //
5254
return;
5355
} else if (target < mid) {
5456
quickSelect(array, left, mid - 1, target);
@@ -57,15 +59,15 @@ private void quickSelect(int[] array, int left, int right, int target) {
5759
}
5860
}
5961

60-
private int partition (int[] array, int left, int right) {
61-
int pivot = array[right];
62-
int start = left;
63-
int end = right - 1;
64-
while (start <= end) {
65-
if (array[start] < pivot) {
66-
start++;
67-
} else if (array[end] >= pivot) {
68-
end--;
62+
private int partition (int[] array, int left, int right) {
63+
int pivot = array[right]; // 1
64+
int start = left; // 0
65+
int end = right - 1; // 2
66+
while (start <= end) { // [0,2] [
67+
if (array[start] < pivot) { // [3,1]
68+
start++; // -
69+
} else if (array[end] >= pivot) { // [5,1]
70+
end--; // end = 1
6971
} else {
7072
swap(array, start++, end--);
7173
}
@@ -86,3 +88,12 @@ public static void main(String[] args) {
8688
System.out.println(Arrays.toString(ks.kSmallestII(data, 3)));
8789
}
8890
}
91+
92+
93+
/*
94+
a = [3,8,5,1] k = 3
95+
96+
97+
98+
99+
*/

0 commit comments

Comments
 (0)