You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0000-0099/0048.Rotate Image/README_EN.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -54,9 +54,9 @@ tags:
54
54
55
55
### Solution 1: In-place Rotation
56
56
57
-
According to the problem requirements, we actually need to rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
57
+
According to the problem requirements, we need to rotate $\text{matrix}[i][j]$ to $\text{matrix}[j][n - i - 1]$.
58
58
59
-
We can first flip the matrix upside down, that is, swap $matrix[i][j]$ and $matrix[n - i - 1][j]$, and then flip the matrix along the main diagonal, that is, swap $matrix[i][j]$ and $matrix[j][i]$. This way, we can rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
59
+
We can first flip the matrix upside down, i.e., swap $\text{matrix}[i][j]$ with $\text{matrix}[n - i - 1][j]$, and then flip the matrix along the main diagonal, i.e., swap $\text{matrix}[i][j]$ with $\text{matrix}[j][i]$. This way, we can rotate $\text{matrix}[i][j]$ to $\text{matrix}[j][n - i - 1]$.
60
60
61
61
The time complexity is $O(n^2)$, where $n$ is the side length of the matrix. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md
+41-32
Original file line number
Diff line number
Diff line change
@@ -62,11 +62,11 @@ tags:
62
62
63
63
### Solution 1: Binary Search
64
64
65
-
Since all elements in each row are sorted in ascending order, we can use binary search to find the first element that is greater than or equal to `target` for each row, and then check if this element is equal to `target`. If it equals `target`, it means the target value has been found, and we directly return `true`. If it does not equal `target`, it means all elements in this row are less than `target`, and we should continue to search the next row.
65
+
Since all elements in each row are sorted in ascending order, for each row, we can use binary search to find the first element greater than or equal to $\textit{target}$, and then check if that element is equal to $\textit{target}$. If it is equal to $\textit{target}$, it means the target value is found, and we return $\text{true}$. If it is not equal to $\textit{target}$, it means all elements in this row are less than $\textit{target}$, and we should continue searching the next row.
66
66
67
-
If all rows have been searched and the target value has not been found, it means the target value does not exist, so we return `false`.
67
+
If all rows have been searched and the target value is not found, it means the target value does not exist, and we return $\text{false}$.
68
68
69
-
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. The space complexity is $O(1)$.
69
+
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
function searchMatrix(matrix:number[][], target:number):boolean {
136
136
const n =matrix[0].length;
137
137
for (const row ofmatrix) {
138
-
let left =0,
139
-
right =n;
140
-
while (left<right) {
141
-
const mid = (left+right) >>1;
142
-
if (row[mid] >=target) {
143
-
right=mid;
144
-
} else {
145
-
left=mid+1;
146
-
}
147
-
}
148
-
if (left!=n&&row[left] ==target) {
138
+
const j =_.sortedIndex(row, target);
139
+
if (j<n&&row[j] ===target) {
149
140
returntrue;
150
141
}
151
142
}
@@ -193,17 +184,8 @@ impl Solution {
193
184
varsearchMatrix=function (matrix, target) {
194
185
constn= matrix[0].length;
195
186
for (constrowof matrix) {
196
-
let left =0,
197
-
right = n;
198
-
while (left < right) {
199
-
constmid= (left + right) >>1;
200
-
if (row[mid] >= target) {
201
-
right = mid;
202
-
} else {
203
-
left = mid +1;
204
-
}
205
-
}
206
-
if (left != n && row[left] == target) {
187
+
constj=_.sortedIndex(row, target);
188
+
if (j < n && row[j] == target) {
207
189
returntrue;
208
190
}
209
191
}
@@ -233,17 +215,17 @@ public class Solution {
233
215
234
216
<!-- solution:start -->
235
217
236
-
### Solution 2: Search from the BottomLeft or TopRight
218
+
### Solution 2: Search from Bottom-Left or Top-Right
237
219
238
-
Here, we start searching from the bottomleft corner and move towards the topright direction, comparing the current element `matrix[i][j]` with `target`:
220
+
We start the search from the bottom-left or top-right corner and move towards the top-right or bottom-left direction. Compare the current element $\textit{matrix}[i][j]$ with $\textit{target}$:
239
221
240
-
- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value has been found, and we directly return `true`.
241
-
- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
242
-
- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
222
+
- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value is found, and we return $\text{true}$.
223
+
- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than $\textit{target}$, so we move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
224
+
- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than $\textit{target}$, so we move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
243
225
244
-
If the search ends and the `target` is still not found, return `false`.
226
+
If the search ends and the $\textit{target}$ is not found, return $\text{false}$.
245
227
246
-
The time complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. The space complexity is $O(1)$.
228
+
The time complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
0 commit comments