Skip to content

Commit 04bdc12

Browse files
refactor 323
1 parent 21fdca2 commit 04bdc12

2 files changed

Lines changed: 19 additions & 26 deletions

File tree

  • paginated_contents/algorithms/1st_thousand
  • src/main/java/com/fishercoder/solutions/firstthousand

‎paginated_contents/algorithms/1st_thousand/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@
507507
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_326.java) | | Easy | Math
508508
| 325 | [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_325.java) | | Medium | HashTable
509509
| 324 | [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_324.java) | | Medium | Sort
510-
| 323 | [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_323.java) | | Medium |
510+
| 323 | [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_323.java) | | Medium | BFS
511511
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_322.java) | | Medium | DP
512512
| 321 | [Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_321.java) | | Hard
513513
| 320 | [Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_320.java) | | Medium | Backtracking, Bit Manipulation

‎src/main/java/com/fishercoder/solutions/firstthousand/_323.java‎

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,35 @@
77

88
public class _323 {
99
public static class Solution1 {
10-
10+
/**
11+
* Use BFS to iterate through all nodes and their connected neighbors
12+
*/
1113
public int countComponents(int n, int[][] edges) {
12-
if (n <= 1) {
13-
return n;
14-
}
15-
16-
List<List<Integer>> adList = new ArrayList<>();
14+
//build the adjacency list;
15+
List<List<Integer>> adj = new ArrayList<>();
1716
for (int i = 0; i < n; i++) {
18-
adList.add(new ArrayList<>());
17+
adj.add(new ArrayList<>());
1918
}
20-
2119
for (int[] edge : edges) {
22-
adList.get(edge[0]).add(edge[1]);
23-
adList.get(edge[1]).add(edge[0]);
24-
}
25-
26-
for (List<Integer> list : adList) {
27-
for (int i : list) {
28-
System.out.print(i + ", ");
29-
}
30-
System.out.println();
20+
adj.get(edge[0]).add(edge[1]);
21+
adj.get(edge[1]).add(edge[0]);
3122
}
3223

3324
boolean[] visited = new boolean[n];
3425
int count = 0;
26+
3527
for (int i = 0; i < n; i++) {
3628
if (!visited[i]) {
3729
count++;
38-
Queue<Integer> q = new LinkedList<>();
39-
q.offer(i);
40-
while (!q.isEmpty()) {
41-
int index = q.poll();
42-
visited[index] = true;
43-
for (int j : adList.get(index)) {
44-
if (!visited[j]) {
45-
q.offer(j);
30+
Queue<Integer> queue = new LinkedList<>();
31+
queue.offer(i);
32+
33+
while (!queue.isEmpty()) {
34+
int currNode = queue.poll();
35+
visited[currNode] = true;
36+
for (int neighbor : adj.get(currNode)) {
37+
if (!visited[neighbor]) {
38+
queue.offer(neighbor);
4639
}
4740
}
4841
}

0 commit comments

Comments
 (0)