diff --git a/solution/0800-0899/0831.Masking Personal Information/README_EN.md b/solution/0800-0899/0831.Masking Personal Information/README_EN.md index d90a2967dfcb1..2a0187c98e597 100644 --- a/solution/0800-0899/0831.Masking Personal Information/README_EN.md +++ b/solution/0800-0899/0831.Masking Personal Information/README_EN.md @@ -116,7 +116,11 @@ Thus, the resulting masked number is "***-***-7890". -### Solution 1 +### Solution 1: Simulation + +According to the problem description, we can first determine whether the string $s$ is an email or a phone number, and then handle it accordingly. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. diff --git a/solution/0800-0899/0832.Flipping an Image/README.md b/solution/0800-0899/0832.Flipping an Image/README.md index be1288ef2321c..b8a92c5324a40 100644 --- a/solution/0800-0899/0832.Flipping an Image/README.md +++ b/solution/0800-0899/0832.Flipping an Image/README.md @@ -75,11 +75,9 @@ tags: ### 方法一:双指针 -我们可以遍历矩阵,对于遍历到的每一行 $row$: +我们可以遍历矩阵,对于遍历到的每一行 $\textit{row}$,我们使用双指针 $i$ 和 $j$ 分别指向该行的首尾元素,如果 $\textit{row}[i] = \textit{row}[j]$,交换后两者的值仍然保持不变,因此,我们只需要对 $\textit{row}[i]$ 和 $\textit{row}[j]$ 进行异或反转即可,然后将 $i$ 和 $j$ 分别向中间移动一位,直到 $i \geq j$。如果 $\textit{row}[i] \neq \textit{row}[j]$,此时交换后再反转两者的值,仍然保持不变,因此,可以不进行任何操作。 -我们使用双指针 $i$ 和 $j$ 分别指向该行的首尾元素,如果 $row[i] = row[j]$,交换后两者的值仍然保持不变,因此,我们只需要对 $row[i]$ 和 $row[j]$ 进行异或反转即可,然后将 $i$ 和 $j$ 分别向中间移动一位,直到 $i \geq j$。如果 $row[i] \neq row[j]$,此时交换后再反转两者的值,仍然保持不变,因此,可以不进行任何操作。 - -最后,如果 $i = j$,我们直接对 $row[i]$ 进行反转即可。 +最后,如果 $i = j$,我们直接对 $\textit{row}[i]$ 进行反转即可。 时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的行数或列数。空间复杂度 $O(1)$。 diff --git a/solution/0800-0899/0832.Flipping an Image/README_EN.md b/solution/0800-0899/0832.Flipping an Image/README_EN.md index f559c3f8a74de..81cbeb0c668b9 100644 --- a/solution/0800-0899/0832.Flipping an Image/README_EN.md +++ b/solution/0800-0899/0832.Flipping an Image/README_EN.md @@ -69,7 +69,13 @@ Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]] -### Solution 1 +### Solution 1: Two Pointers + +We can traverse the matrix, and for each row $\textit{row}$, we use two pointers $i$ and $j$ pointing to the first and last elements of the row, respectively. If $\textit{row}[i] = \textit{row}[j]$, swapping them will keep their values unchanged, so we only need to XOR invert $\textit{row}[i]$ and $\textit{row}[j]$, then move $i$ and $j$ one position towards the center until $i \geq j$. If $\textit{row}[i] \neq \textit{row}[j]$, swapping and then inverting their values will also keep them unchanged, so no operation is needed. + +Finally, if $i = j$, we directly invert $\textit{row}[i]$. + +The time complexity is $O(n^2)$, where $n$ is the number of rows or columns in the matrix. The space complexity is $O(1)$. diff --git a/solution/0800-0899/0833.Find And Replace in String/README.md b/solution/0800-0899/0833.Find And Replace in String/README.md index b9ec81c138b2a..46f01f8354b7e 100644 --- a/solution/0800-0899/0833.Find And Replace in String/README.md +++ b/solution/0800-0899/0833.Find And Replace in String/README.md @@ -87,7 +87,7 @@ tags: ### 方法一:模拟 -我们遍历每个替换操作,对于当前第 $k$ 个替换操作 $(i, src)$,如果 $s[i..i+|src|-1]$ 与 $src$ 相等,此时我们记录下标 $i$ 处需要替换的是 $targets$ 的第 $k$ 个字符串,否则不需要替换。 +我们遍历每个替换操作,对于当前第 $k$ 个替换操作 $(i, \text{src})$,如果 $s[i..i+|\text{src}|-1]$ 与 $\text{src}$ 相等,此时我们记录下标 $i$ 处需要替换的是 $\text{targets}$ 的第 $k$ 个字符串,否则不需要替换。 接下来,我们只需要遍历原字符串 $s$,根据记录的信息进行替换即可。 diff --git a/solution/0800-0899/0833.Find And Replace in String/README_EN.md b/solution/0800-0899/0833.Find And Replace in String/README_EN.md index 069120db7734a..eb403df3f8195 100644 --- a/solution/0800-0899/0833.Find And Replace in String/README_EN.md +++ b/solution/0800-0899/0833.Find And Replace in String/README_EN.md @@ -81,7 +81,13 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We iterate through each replacement operation. For the current $k$-th replacement operation $(i, \text{src})$, if $s[i..i+|\text{src}|-1]$ is equal to $\text{src}$, we record that the string at index $i$ needs to be replaced with the $k$-th string in $\text{targets}$; otherwise, no replacement is needed. + +Next, we only need to iterate through the original string $s$ and perform the replacements based on the recorded information. + +The time complexity is $O(L)$, and the space complexity is $O(n)$, where $L$ is the sum of the lengths of all strings, and $n$ is the length of the string $s$. diff --git a/solution/0800-0899/0834.Sum of Distances in Tree/README_EN.md b/solution/0800-0899/0834.Sum of Distances in Tree/README_EN.md index 6182ec5ee7604..9a43d998165ed 100644 --- a/solution/0800-0899/0834.Sum of Distances in Tree/README_EN.md +++ b/solution/0800-0899/0834.Sum of Distances in Tree/README_EN.md @@ -69,7 +69,17 @@ Hence, answer[0] = 8, and so on. -### Solution 1 +### Solution 1: Tree DP (Re-rooting) + +First, we run a DFS to calculate the size of each node's subtree, recorded in the array $size$, and compute the sum of distances from node $0$ to all other nodes, recorded in $ans[0]$. + +Next, we run another DFS to enumerate the sum of distances from each node when it is considered as the root. Suppose the answer for the current node $i$ is $t$. When we move from node $i$ to node $j$, the sum of distances changes to $t - size[j] + n - size[j]$, meaning the sum of distances to node $j$ and its subtree nodes decreases by $size[j]$, while the sum of distances to other nodes increases by $n - size[j]$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree. + +Similar problems: + +- [2581. Count Number of Possible Root Nodes](https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2581.Count%20Number%20of%20Possible%20Root%20Nodes/README_EN.md) diff --git a/solution/0800-0899/0835.Image Overlap/README.md b/solution/0800-0899/0835.Image Overlap/README.md index 78ddecb75cb53..ff522c4e8610a 100644 --- a/solution/0800-0899/0835.Image Overlap/README.md +++ b/solution/0800-0899/0835.Image Overlap/README.md @@ -76,9 +76,9 @@ tags: ### 方法一:枚举 -我们可以枚举 $img1$ 和 $img2$ 的每个 $1$ 的位置,分别记为 $(i, j)$ 和 $(h, k)$。然后我们计算得到偏移量 $(i - h, j - k)$,记为 $(dx, dy)$,用哈希表 $cnt$ 记录每个偏移量出现的次数。最后我们遍历哈希表 $cnt$,找到出现次数最多的偏移量,即为答案。 +我们可以枚举 $\textit{img1}$ 和 $\textit{img2}$ 的每个 $1$ 的位置,分别记为 $(i, j)$ 和 $(h, k)$。然后我们计算得到偏移量 $(i - h, j - k)$,记为 $(dx, dy)$,用哈希表 $\textit{cnt}$ 记录每个偏移量出现的次数。最后我们遍历哈希表 $\textit{cnt}$,找到出现次数最多的偏移量,即为答案。 -时间复杂度 $O(n^4)$,空间复杂度 $O(n^2)$。其中 $n$ 是 $img1$ 的边长。 +时间复杂度 $O(n^4)$,空间复杂度 $O(n^2)$。其中 $n$ 是 $\textit{img1}$ 的边长。 diff --git a/solution/0800-0899/0835.Image Overlap/README_EN.md b/solution/0800-0899/0835.Image Overlap/README_EN.md index b01e099984f5d..405a63e25340f 100644 --- a/solution/0800-0899/0835.Image Overlap/README_EN.md +++ b/solution/0800-0899/0835.Image Overlap/README_EN.md @@ -68,7 +68,11 @@ The number of positions that have a 1 in both images is 3 (shown in red). -### Solution 1 +### Solution 1: Enumeration + +We can enumerate each position of $1$ in $\textit{img1}$ and $\textit{img2}$, denoted as $(i, j)$ and $(h, k)$ respectively. Then we calculate the offset $(i - h, j - k)$, denoted as $(dx, dy)$, and use a hash table $\textit{cnt}$ to record the number of occurrences of each offset. Finally, we traverse the hash table $\textit{cnt}$ to find the offset that appears the most, which is the answer. + +The time complexity is $O(n^4)$, and the space complexity is $O(n^2)$, where $n$ is the side length of $\textit{img1}$. diff --git a/solution/0800-0899/0836.Rectangle Overlap/README.md b/solution/0800-0899/0836.Rectangle Overlap/README.md index d34e09e0cb151..30135690d718d 100644 --- a/solution/0800-0899/0836.Rectangle Overlap/README.md +++ b/solution/0800-0899/0836.Rectangle Overlap/README.md @@ -65,16 +65,16 @@ tags: ### 方法一:判断不重叠的情况 -我们记矩形 $rec1$ 的坐标点为 $(x_1, y_1, x_2, y_2)$,矩形 $rec2$ 的坐标点为 $(x_3, y_3, x_4, y_4)$。 +我们记矩形 $\text{rec1}$ 的坐标点为 $(x_1, y_1, x_2, y_2)$,矩形 $\text{rec2}$ 的坐标点为 $(x_3, y_3, x_4, y_4)$。 -那么当满足以下任一条件时,矩形 $rec1$ 和 $rec2$ 不重叠: +那么当满足以下任一条件时,矩形 $\text{rec1}$ 和 $\text{rec2}$ 不重叠: -- 满足 $y_3 \geq y_2$,即 $rec2$ 在 $rec1$ 的上方; -- 满足 $y_4 \leq y_1$,即 $rec2$ 在 $rec1$ 的下方; -- 满足 $x_3 \geq x_2$,即 $rec2$ 在 $rec1$ 的右方; -- 满足 $x_4 \leq x_1$,即 $rec2$ 在 $rec1$ 的左方。 +- 满足 $y_3 \geq y_2$,即 $\text{rec2}$ 在 $\text{rec1}$ 的上方; +- 满足 $y_4 \leq y_1$,即 $\text{rec2}$ 在 $\text{rec1}$ 的下方; +- 满足 $x_3 \geq x_2$,即 $\text{rec2}$ 在 $\text{rec1}$ 的右方; +- 满足 $x_4 \leq x_1$,即 $\text{rec2}$ 在 $\text{rec1}$ 的左方。 -当以上条件都不满足时,矩形 $rec1$ 和 $rec2$ 重叠。 +当以上条件都不满足时,矩形 $\text{rec1}$ 和 $\text{rec2}$ 重叠。 时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -125,6 +125,16 @@ func isRectangleOverlap(rec1 []int, rec2 []int) bool { } ``` +#### TypeScript + +```ts +function isRectangleOverlap(rec1: number[], rec2: number[]): boolean { + const [x1, y1, x2, y2] = rec1; + const [x3, y3, x4, y4] = rec2; + return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1); +} +``` + diff --git a/solution/0800-0899/0836.Rectangle Overlap/README_EN.md b/solution/0800-0899/0836.Rectangle Overlap/README_EN.md index 7fb6b6b0558fa..c13fe542516eb 100644 --- a/solution/0800-0899/0836.Rectangle Overlap/README_EN.md +++ b/solution/0800-0899/0836.Rectangle Overlap/README_EN.md @@ -50,7 +50,20 @@ tags: -### Solution 1 +### Solution 1: Determine Non-Overlap Cases + +Let the coordinates of rectangle $\text{rec1}$ be $(x_1, y_1, x_2, y_2)$, and the coordinates of rectangle $\text{rec2}$ be $(x_3, y_3, x_4, y_4)$. + +The rectangles $\text{rec1}$ and $\text{rec2}$ do not overlap if any of the following conditions are met: + +- $y_3 \geq y_2$: $\text{rec2}$ is above $\text{rec1}$; +- $y_4 \leq y_1$: $\text{rec2}$ is below $\text{rec1}$; +- $x_3 \geq x_2$: $\text{rec2}$ is to the right of $\text{rec1}$; +- $x_4 \leq x_1$: $\text{rec2}$ is to the left of $\text{rec1}$. + +If none of the above conditions are met, the rectangles $\text{rec1}$ and $\text{rec2}$ overlap. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. @@ -99,6 +112,16 @@ func isRectangleOverlap(rec1 []int, rec2 []int) bool { } ``` +#### TypeScript + +```ts +function isRectangleOverlap(rec1: number[], rec2: number[]): boolean { + const [x1, y1, x2, y2] = rec1; + const [x3, y3, x4, y4] = rec2; + return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1); +} +``` + diff --git a/solution/0800-0899/0836.Rectangle Overlap/Solution.ts b/solution/0800-0899/0836.Rectangle Overlap/Solution.ts new file mode 100644 index 0000000000000..76a6fb92f1b21 --- /dev/null +++ b/solution/0800-0899/0836.Rectangle Overlap/Solution.ts @@ -0,0 +1,5 @@ +function isRectangleOverlap(rec1: number[], rec2: number[]): boolean { + const [x1, y1, x2, y2] = rec1; + const [x3, y3, x4, y4] = rec2; + return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1); +}