Skip to content

Commit d4f7ff5

Browse files
committed
update 737 java, union-find cheatsheet
1 parent 6bb3873 commit d4f7ff5

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

doc/cheatsheet/union_find.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## 0) Concept
1010
- Implementation
11-
- build 2 API:
11+
- build 3 API:
1212
- `Union`
1313
- `connected`
1414
- find : once "route compression" is applied, then union and connected API will be `O(1)` time complexity

leetcode_java/src/main/java/LeetCodeJava/DFS/SentenceSimilarity2.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,38 @@ public boolean areSentencesSimilarTwo_1(
4646
return false;
4747
}
4848
int n = similarPairs.size();
49-
p = new int[n << 1];
49+
50+
51+
/**
52+
*
53+
* Explain p = new int[n << 1]; ?
54+
*
55+
* The p variable is being initialized as an integer array with a size of n << 1. Here’s a breakdown of the key elements:
56+
*
57+
* 1. int[n << 1]:
58+
* • The n << 1 operation is a bitwise left shift. This means that the value of n is shifted left by 1 bit. In binary terms, this effectively multiplies n by 2.
59+
* • For example, if n = 5, its binary representation is 101. Shifting it left by one position (n << 1) gives 1010, which is 10 in decimal. Therefore, n << 1 results in 2 * n.
60+
* 2. p = new int[n << 1]:
61+
* • This allocates an integer array of size 2 * n. For instance, if n = 5, the size of the array p would be 10.
62+
*
63+
*
64+
* Purpose:
65+
*
66+
* This operation is often used when you need to allocate extra space, such as in algorithms involving data structures like stacks, arrays, or graphs. Doubling the size can be beneficial for dynamic resizing or managing additional elements.
67+
*
68+
*
69+
*
70+
*
71+
* Yes,
72+
* you can replace the bitwise operation n << 1
73+
* with a simple multiplication by 2.
74+
* The left shift operation n << 1 is equivalent to multiplying n by 2
75+
*/
76+
//p = new int[n << 1];
77+
p = new int[n * 2];
78+
79+
80+
5081
for (int i = 0; i < p.length; ++i) {
5182
p[i] = i;
5283
}

0 commit comments

Comments
 (0)