diff --git a/solution/0700-0799/0763.Partition Labels/README.md b/solution/0700-0799/0763.Partition Labels/README.md
index ade793587c0cf..705d41800366f 100644
--- a/solution/0700-0799/0763.Partition Labels/README.md
+++ b/solution/0700-0799/0763.Partition Labels/README.md
@@ -60,19 +60,19 @@ tags:
### 方法一:贪心
-我们先用数组或哈希表 $last$ 记录字符串 $s$ 中每个字母最后一次出现的位置。
+我们先用数组或哈希表 $\textit{last}$ 记录字符串 $s$ 中每个字母最后一次出现的位置。
接下来我们使用贪心的方法,将字符串划分为尽可能多的片段。
从左到右遍历字符串 $s$,遍历的同时维护当前片段的开始下标 $j$ 和结束下标 $i$,初始均为 $0$。
-对于每个访问到的字母 $c$,获取到最后一次出现的位置 $last[c]$。由于当前片段的结束下标一定不会小于 $last[c]$,因此令 $mx = \max(mx, last[c])$。
+对于每个访问到的字母 $c$,获取到最后一次出现的位置 $\textit{last}[c]$。由于当前片段的结束下标一定不会小于 $\textit{last}[c]$,因此令 $\textit{mx} = \max(\textit{mx}, \textit{last}[c])$。
-当访问到下标 $mx$ 时,意味着当前片段访问结束,当前片段的下标范围是 $[j,.. i]$,长度为 $i - j + 1$,我们将其添加到结果数组中。然后令 $j = i + 1$, 继续寻找下一个片段。
+当访问到下标 $\textit{mx}$ 时,意味着当前片段访问结束,当前片段的下标范围是 $[j,.. i]$,长度为 $i - j + 1$,我们将其添加到结果数组中。然后令 $j = i + 1$, 继续寻找下一个片段。
重复上述过程,直至字符串遍历结束,即可得到所有片段的长度。
-时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
+时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 为字符串 $s$ 的长度,而 $|\Sigma|$ 为字符集的大小。本题中 $|\Sigma| = 26$。
diff --git a/solution/0700-0799/0763.Partition Labels/README_EN.md b/solution/0700-0799/0763.Partition Labels/README_EN.md
index 4b6bcde451f13..9f4d0da5791af 100644
--- a/solution/0700-0799/0763.Partition Labels/README_EN.md
+++ b/solution/0700-0799/0763.Partition Labels/README_EN.md
@@ -58,7 +58,21 @@ A partition like "ababcbacadefegde", "hijhklij" is incorrect
-### Solution 1
+### Solution 1: Greedy
+
+We first use an array or hash table $\textit{last}$ to record the last occurrence of each letter in the string $s$.
+
+Next, we use a greedy approach to partition the string into as many segments as possible.
+
+Traverse the string $s$ from left to right, while maintaining the start index $j$ and end index $i$ of the current segment, both initially set to $0$.
+
+For each letter $c$ visited, get the last occurrence position $\textit{last}[c]$. Since the end index of the current segment must not be less than $\textit{last}[c]$, let $\textit{mx} = \max(\textit{mx}, \textit{last}[c])$.
+
+When visiting the index $\textit{mx}$, it means the current segment ends. The index range of the current segment is $[j,.. i]$, and the length is $i - j + 1$. We add this length to the result array. Then set $j = i + 1$ and continue to find the next segment.
+
+Repeat the above process until the string traversal is complete to get the lengths of all segments.
+
+Time complexity is $O(n)$, and space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 26$.
diff --git a/solution/0700-0799/0765.Couples Holding Hands/README_EN.md b/solution/0700-0799/0765.Couples Holding Hands/README_EN.md
index c90ba1f5741ac..fbd7081f05778 100644
--- a/solution/0700-0799/0765.Couples Holding Hands/README_EN.md
+++ b/solution/0700-0799/0765.Couples Holding Hands/README_EN.md
@@ -60,7 +60,17 @@ tags:
-### Solution 1
+### Solution 1: Union-Find
+
+We can assign a number to each pair of couples. Person with number $0$ and $1$ corresponds to couple $0$, person with number $2$ and $3$ corresponds to couple $1$, and so on. In other words, the person corresponding to $row[i]$ has a couple number of $\lfloor \frac{row[i]}{2} \rfloor$.
+
+If there are $k$ pairs of couples who are seated incorrectly with respect to each other, i.e., if $k$ pairs of couples are in the same permutation cycle, it will take $k-1$ swaps for all of them to be seated correctly.
+
+Why? Consider the following: we first adjust the positions of a couple to their correct seats. After this, the problem reduces from $k$ couples to $k-1$ couples. This process continues, and when $k = 1$, the number of swaps required is $0$. Therefore, if $k$ pairs of couples are in the wrong positions, we need $k-1$ swaps.
+
+Thus, we only need to traverse the array once, use union-find to determine how many permutation cycles there are. Suppose there are $x$ cycles, and the size of each cycle (in terms of couple pairs) is $y_1, y_2, \cdots, y_x$. The number of swaps required is $y_1-1 + y_2-1 + \cdots + y_x-1 = y_1 + y_2 + \cdots + y_x - x = n - x$.
+
+The time complexity is $O(n \times \alpha(n))$, and the space complexity is $O(n)$, where $\alpha(n)$ is the inverse Ackermann function, which can be considered a very small constant.
diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md
index 6c5c2d0506e65..c2b4f0a17c49b 100644
--- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md
+++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md
@@ -33,8 +33,8 @@ tags:
输入:arr = [5,4,3,2,1]
输出:1
解释:
-将数组分成2块或者更多块,都无法得到所需的结果。
-例如,分成 [5, 4], [3, 2, 1] 的结果是 [4, 5, 1, 2, 3],这不是有序的数组。
+将数组分成2块或者更多块,都无法得到所需的结果。
+例如,分成 [5, 4], [3, 2, 1] 的结果是 [4, 5, 1, 2, 3],这不是有序的数组。
示例 2:
@@ -43,8 +43,8 @@ tags:
输入:arr = [2,1,3,4,4]
输出:4
解释:
-可以把它分成两块,例如 [2, 1], [3, 4, 4]。
-然而,分成 [2, 1], [3], [4], [4] 可以得到最多的块数。
+可以把它分成两块,例如 [2, 1], [3, 4, 4]。
+然而,分成 [2, 1], [3], [4], [4] 可以得到最多的块数。
@@ -66,7 +66,7 @@ tags:
根据题目,我们可以发现,从左到右,每个分块都有一个最大值,并且这些分块的最大值呈单调递增(非严格递增)。我们可以用一个栈来存储这些分块的最大值。最后得到的栈的大小,也就是题目所求的最多能完成排序的块。
-时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
+时间复杂度 $O(n)$,其中 $n$ 表示 $\textit{arr}$ 的长度。
@@ -156,19 +156,19 @@ func maxChunksToSorted(arr []int) int {
```ts
function maxChunksToSorted(arr: number[]): number {
- const stack = [];
- for (const num of arr) {
- if (stack.length !== 0 && num < stack[stack.length - 1]) {
- const max = stack.pop();
- while (stack.length !== 0 && num < stack[stack.length - 1]) {
- stack.pop();
- }
- stack.push(max);
+ const stk: number[] = [];
+ for (let v of arr) {
+ if (stk.length === 0 || v >= stk[stk.length - 1]) {
+ stk.push(v);
} else {
- stack.push(num);
+ let mx = stk.pop()!;
+ while (stk.length > 0 && stk[stk.length - 1] > v) {
+ stk.pop();
+ }
+ stk.push(mx);
}
}
- return stack.length;
+ return stk.length;
}
```
@@ -177,19 +177,23 @@ function maxChunksToSorted(arr: number[]): number {
```rust
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec) -> i32 {
- let mut stack = vec![];
- for num in arr.iter() {
- if !stack.is_empty() && num < stack.last().unwrap() {
- let max = stack.pop().unwrap();
- while !stack.is_empty() && num < stack.last().unwrap() {
- stack.pop();
- }
- stack.push(max);
+ let mut stk = Vec::new();
+ for &v in arr.iter() {
+ if stk.is_empty() || v >= *stk.last().unwrap() {
+ stk.push(v);
} else {
- stack.push(*num);
+ let mut mx = stk.pop().unwrap();
+ while let Some(&top) = stk.last() {
+ if top > v {
+ stk.pop();
+ } else {
+ break;
+ }
+ }
+ stk.push(mx);
}
}
- stack.len() as i32
+ stk.len() as i32
}
}
```
diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md
index baa0ed6f9b2c1..65bdb6d9914a3 100644
--- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md
+++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md
@@ -61,7 +61,11 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po
-### Solution 1
+### Solution 1: Monotonic Stack
+
+According to the problem, we can find that from left to right, each chunk has a maximum value, and these maximum values are monotonically increasing (non-strictly increasing). We can use a stack to store these maximum values of the chunks. The size of the final stack is the maximum number of chunks that can be sorted.
+
+Time complexity is $O(n)$, where $n$ represents the length of $\textit{arr}$.
@@ -151,19 +155,19 @@ func maxChunksToSorted(arr []int) int {
```ts
function maxChunksToSorted(arr: number[]): number {
- const stack = [];
- for (const num of arr) {
- if (stack.length !== 0 && num < stack[stack.length - 1]) {
- const max = stack.pop();
- while (stack.length !== 0 && num < stack[stack.length - 1]) {
- stack.pop();
- }
- stack.push(max);
+ const stk: number[] = [];
+ for (let v of arr) {
+ if (stk.length === 0 || v >= stk[stk.length - 1]) {
+ stk.push(v);
} else {
- stack.push(num);
+ let mx = stk.pop()!;
+ while (stk.length > 0 && stk[stk.length - 1] > v) {
+ stk.pop();
+ }
+ stk.push(mx);
}
}
- return stack.length;
+ return stk.length;
}
```
@@ -172,19 +176,23 @@ function maxChunksToSorted(arr: number[]): number {
```rust
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec) -> i32 {
- let mut stack = vec![];
- for num in arr.iter() {
- if !stack.is_empty() && num < stack.last().unwrap() {
- let max = stack.pop().unwrap();
- while !stack.is_empty() && num < stack.last().unwrap() {
- stack.pop();
- }
- stack.push(max);
+ let mut stk = Vec::new();
+ for &v in arr.iter() {
+ if stk.is_empty() || v >= *stk.last().unwrap() {
+ stk.push(v);
} else {
- stack.push(*num);
+ let mut mx = stk.pop().unwrap();
+ while let Some(&top) = stk.last() {
+ if top > v {
+ stk.pop();
+ } else {
+ break;
+ }
+ }
+ stk.push(mx);
}
}
- stack.len() as i32
+ stk.len() as i32
}
}
```
diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs
index 503c3e28cd3f3..c255f47d04a2c 100644
--- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs
+++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs
@@ -1,17 +1,21 @@
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec) -> i32 {
- let mut stack = vec![];
- for num in arr.iter() {
- if !stack.is_empty() && num < stack.last().unwrap() {
- let max = stack.pop().unwrap();
- while !stack.is_empty() && num < stack.last().unwrap() {
- stack.pop();
- }
- stack.push(max);
+ let mut stk = Vec::new();
+ for &v in arr.iter() {
+ if stk.is_empty() || v >= *stk.last().unwrap() {
+ stk.push(v);
} else {
- stack.push(*num);
+ let mut mx = stk.pop().unwrap();
+ while let Some(&top) = stk.last() {
+ if top > v {
+ stk.pop();
+ } else {
+ break;
+ }
+ }
+ stk.push(mx);
}
}
- stack.len() as i32
+ stk.len() as i32
}
}
diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts
index 9c2c15ccfd636..7bf22c579df66 100644
--- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts
+++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts
@@ -1,15 +1,15 @@
function maxChunksToSorted(arr: number[]): number {
- const stack = [];
- for (const num of arr) {
- if (stack.length !== 0 && num < stack[stack.length - 1]) {
- const max = stack.pop();
- while (stack.length !== 0 && num < stack[stack.length - 1]) {
- stack.pop();
- }
- stack.push(max);
+ const stk: number[] = [];
+ for (let v of arr) {
+ if (stk.length === 0 || v >= stk[stk.length - 1]) {
+ stk.push(v);
} else {
- stack.push(num);
+ let mx = stk.pop()!;
+ while (stk.length > 0 && stk[stk.length - 1] > v) {
+ stk.pop();
+ }
+ stk.push(mx);
}
}
- return stack.length;
+ return stk.length;
}
diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md b/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md
index e7b8d45997f2d..f73a26d427036 100644
--- a/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md
+++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md
@@ -68,9 +68,9 @@ tags:
### 方法一:贪心 + 一次遍历
-由于 $arr$ 是 $[0,..,n-1]$ 的一个排列,若已遍历过的数中的最大值 $mx$ 与当前遍历到的下标 $i$ 相等,说明可以进行一次分割,累加答案。
+由于 $\textit{arr}$ 是 $[0,..,n-1]$ 的一个排列,若已遍历过的数中的最大值 $\textit{mx}$ 与当前遍历到的下标 $i$ 相等,说明可以进行一次分割,累加答案。
-时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
+时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
@@ -141,10 +141,10 @@ func maxChunksToSorted(arr []int) int {
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
let ans = 0;
- let max = 0;
+ let mx = 0;
for (let i = 0; i < n; i++) {
- max = Math.max(arr[i], max);
- if (max == i) {
+ mx = Math.max(arr[i], mx);
+ if (mx == i) {
ans++;
}
}
@@ -157,15 +157,15 @@ function maxChunksToSorted(arr: number[]): number {
```rust
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec) -> i32 {
- let mut res = 0;
- let mut max = 0;
+ let mut ans = 0;
+ let mut mx = 0;
for i in 0..arr.len() {
- max = max.max(arr[i]);
- if max == (i as i32) {
- res += 1;
+ mx = mx.max(arr[i]);
+ if mx == (i as i32) {
+ ans += 1;
}
}
- res
+ ans
}
}
```
@@ -176,15 +176,15 @@ impl Solution {
#define max(a, b) (((a) > (b)) ? (a) : (b))
int maxChunksToSorted(int* arr, int arrSize) {
- int res = 0;
+ int ans = 0;
int mx = -1;
for (int i = 0; i < arrSize; i++) {
mx = max(mx, arr[i]);
if (mx == i) {
- res++;
+ ans++;
}
}
- return res;
+ return ans;
}
```
@@ -202,7 +202,7 @@ int maxChunksToSorted(int* arr, int arrSize) {
以上这种解法,不仅可以解决本题,也可以解决 [768. 最多能完成排序的块 II](https://github.com/doocs/leetcode/blob/main/solution/0700-0799/0768.Max%20Chunks%20To%20Make%20Sorted%20II/README.md) 这道困难题。大家可以自行尝试。
-时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $arr$ 的长度。
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md b/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md
index e43628aea0d2b..7626ad866c576 100644
--- a/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md
+++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md
@@ -63,7 +63,11 @@ However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks po
-### Solution 1
+### Solution 1: Greedy + One Pass
+
+Since $\textit{arr}$ is a permutation of $[0,..,n-1]$, if the maximum value $\textit{mx}$ among the numbers traversed so far is equal to the current index $i$, it means a split can be made, and the answer is incremented.
+
+Time complexity is $O(n)$, and space complexity is $O(1)$. Where $n$ is the length of the array $\textit{arr}$.
@@ -134,10 +138,10 @@ func maxChunksToSorted(arr []int) int {
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
let ans = 0;
- let max = 0;
+ let mx = 0;
for (let i = 0; i < n; i++) {
- max = Math.max(arr[i], max);
- if (max == i) {
+ mx = Math.max(arr[i], mx);
+ if (mx == i) {
ans++;
}
}
@@ -150,15 +154,15 @@ function maxChunksToSorted(arr: number[]): number {
```rust
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec) -> i32 {
- let mut res = 0;
- let mut max = 0;
+ let mut ans = 0;
+ let mut mx = 0;
for i in 0..arr.len() {
- max = max.max(arr[i]);
- if max == (i as i32) {
- res += 1;
+ mx = mx.max(arr[i]);
+ if mx == (i as i32) {
+ ans += 1;
}
}
- res
+ ans
}
}
```
@@ -169,15 +173,15 @@ impl Solution {
#define max(a, b) (((a) > (b)) ? (a) : (b))
int maxChunksToSorted(int* arr, int arrSize) {
- int res = 0;
+ int ans = 0;
int mx = -1;
for (int i = 0; i < arrSize; i++) {
mx = max(mx, arr[i]);
if (mx == i) {
- res++;
+ ans++;
}
}
- return res;
+ return ans;
}
```
@@ -187,7 +191,15 @@ int maxChunksToSorted(int* arr, int arrSize) {
-### Solution 2
+### Solution 2: Monotonic Stack
+
+The solution of method one has certain limitations. If there are duplicate elements in the array, the correct answer cannot be obtained.
+
+According to the problem, we can find that from left to right, each chunk has a maximum value, and these maximum values are monotonically increasing. We can use a stack to store these maximum values of the chunks. The size of the final stack is the maximum number of chunks that can be sorted.
+
+This solution can not only solve this problem but also solve the problem 768. Max Chunks To Make Sorted II. You can try it yourself.
+
+Time complexity is $O(n)$, and space complexity is $O(n)$. Where $n$ is the length of the array $\textit{arr}$.
diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.c b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.c
index 8cb564843f83c..bb402bfec5380 100644
--- a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.c
+++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.c
@@ -1,13 +1,13 @@
#define max(a, b) (((a) > (b)) ? (a) : (b))
int maxChunksToSorted(int* arr, int arrSize) {
- int res = 0;
+ int ans = 0;
int mx = -1;
for (int i = 0; i < arrSize; i++) {
mx = max(mx, arr[i]);
if (mx == i) {
- res++;
+ ans++;
}
}
- return res;
-}
\ No newline at end of file
+ return ans;
+}
diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs
index d57cd9a5e60a8..d65cffdce7e30 100644
--- a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs
+++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs
@@ -1,13 +1,13 @@
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec) -> i32 {
- let mut res = 0;
- let mut max = 0;
+ let mut ans = 0;
+ let mut mx = 0;
for i in 0..arr.len() {
- max = max.max(arr[i]);
- if max == (i as i32) {
- res += 1;
+ mx = mx.max(arr[i]);
+ if mx == (i as i32) {
+ ans += 1;
}
}
- res
+ ans
}
}
diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts
index b749ed58edd9f..0cfcd67535fcb 100644
--- a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts
+++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts
@@ -1,10 +1,10 @@
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
let ans = 0;
- let max = 0;
+ let mx = 0;
for (let i = 0; i < n; i++) {
- max = Math.max(arr[i], max);
- if (max == i) {
+ mx = Math.max(arr[i], mx);
+ if (mx == i) {
ans++;
}
}
diff --git a/solution/0700-0799/0771.Jewels and Stones/README.md b/solution/0700-0799/0771.Jewels and Stones/README.md
index 21ca8afb9b473..bd4bd9dc97268 100644
--- a/solution/0700-0799/0771.Jewels and Stones/README.md
+++ b/solution/0700-0799/0771.Jewels and Stones/README.md
@@ -95,9 +95,13 @@ class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
int s[128] = {0};
- for (char c : jewels) s[c] = 1;
+ for (char c : jewels) {
+ s[c] = 1;
+ }
int ans = 0;
- for (char c : stones) ans += s[c];
+ for (char c : stones) {
+ ans += s[c];
+ }
return ans;
}
};
@@ -137,10 +141,10 @@ function numJewelsInStones(jewels: string, stones: string): number {
use std::collections::HashSet;
impl Solution {
pub fn num_jewels_in_stones(jewels: String, stones: String) -> i32 {
- let mut set = jewels.as_bytes().iter().collect::>();
+ let mut s = jewels.as_bytes().iter().collect::>();
let mut ans = 0;
for c in stones.as_bytes() {
- if set.contains(c) {
+ if s.contains(c) {
ans += 1;
}
}
diff --git a/solution/0700-0799/0771.Jewels and Stones/README_EN.md b/solution/0700-0799/0771.Jewels and Stones/README_EN.md
index f9edb1ef77e62..d707a1e7f41c7 100644
--- a/solution/0700-0799/0771.Jewels and Stones/README_EN.md
+++ b/solution/0700-0799/0771.Jewels and Stones/README_EN.md
@@ -44,7 +44,11 @@ tags:
-### Solution 1
+### Solution 1: Hash Table or Array
+
+We can first use a hash table or array $s$ to record all types of jewels. Then traverse all the stones, and if the current stone is a jewel, increment the answer by one.
+
+Time complexity is $O(m+n)$, and space complexity is $O(|\Sigma|)$, where $m$ and $n$ are the lengths of the strings $jewels$ and $stones$ respectively, and $\Sigma$ is the character set, which in this problem is the set of all uppercase and lowercase English letters.
@@ -82,9 +86,13 @@ class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
int s[128] = {0};
- for (char c : jewels) s[c] = 1;
+ for (char c : jewels) {
+ s[c] = 1;
+ }
int ans = 0;
- for (char c : stones) ans += s[c];
+ for (char c : stones) {
+ ans += s[c];
+ }
return ans;
}
};
@@ -124,10 +132,10 @@ function numJewelsInStones(jewels: string, stones: string): number {
use std::collections::HashSet;
impl Solution {
pub fn num_jewels_in_stones(jewels: String, stones: String) -> i32 {
- let mut set = jewels.as_bytes().iter().collect::>();
+ let mut s = jewels.as_bytes().iter().collect::>();
let mut ans = 0;
for c in stones.as_bytes() {
- if set.contains(c) {
+ if s.contains(c) {
ans += 1;
}
}
diff --git a/solution/0700-0799/0771.Jewels and Stones/Solution.cpp b/solution/0700-0799/0771.Jewels and Stones/Solution.cpp
index 431ca43782321..57c0ec8ce5076 100644
--- a/solution/0700-0799/0771.Jewels and Stones/Solution.cpp
+++ b/solution/0700-0799/0771.Jewels and Stones/Solution.cpp
@@ -2,9 +2,13 @@ class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
int s[128] = {0};
- for (char c : jewels) s[c] = 1;
+ for (char c : jewels) {
+ s[c] = 1;
+ }
int ans = 0;
- for (char c : stones) ans += s[c];
+ for (char c : stones) {
+ ans += s[c];
+ }
return ans;
}
-};
\ No newline at end of file
+};
diff --git a/solution/0700-0799/0771.Jewels and Stones/Solution.rs b/solution/0700-0799/0771.Jewels and Stones/Solution.rs
index af7a1ee26ae49..916b39bd89eee 100644
--- a/solution/0700-0799/0771.Jewels and Stones/Solution.rs
+++ b/solution/0700-0799/0771.Jewels and Stones/Solution.rs
@@ -1,10 +1,10 @@
use std::collections::HashSet;
impl Solution {
pub fn num_jewels_in_stones(jewels: String, stones: String) -> i32 {
- let mut set = jewels.as_bytes().iter().collect::>();
+ let mut s = jewels.as_bytes().iter().collect::>();
let mut ans = 0;
for c in stones.as_bytes() {
- if set.contains(c) {
+ if s.contains(c) {
ans += 1;
}
}