Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 337e117

Browse files
committed
feat: add solutions to lc problems
- No.0074.Search a 2D Matrix - No.0078.Subsets - No.0090.Subsets II - No.1351.Count Negative Numbers in a Sorted Matrix
1 parent 8d045e1 commit 337e117

File tree

14 files changed

+478
-43
lines changed

14 files changed

+478
-43
lines changed

‎solution/0000-0099/0074.Search a 2D Matrix/README.md‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@
4848

4949
将二维矩阵逻辑展开,然后二分查找即可。
5050

51-
时间复杂度O(logmn)。
51+
时间复杂度:$O(logmn)$
5252

5353
**方法二:从左下角或右上角搜索**
5454

55-
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]`target 的大小关系:
55+
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]``target` 的大小关系:
5656

5757
-`matrix[i][j] == target`,说明找到了目标值,直接返回 true。
5858
-`matrix[i][j] > target`,说明这一行从当前位置开始往右的所有元素均大于 target,应该让 i 指针往上移动,即 `i--`
5959
-`matrix[i][j] < target`,说明这一列从当前位置开始往上的所有元素均小于 target,应该让 j 指针往右移动,即 `j++`
6060

6161
若搜索结束依然找不到 target,返回 false。
6262

63-
时间复杂度O(m + n)。
63+
时间复杂度:$O(m + n)$
6464

6565
<!-- tabs:start -->
6666

@@ -284,6 +284,32 @@ func searchMatrix(matrix [][]int, target int) bool {
284284
}
285285
```
286286

287+
### **TypeScript**
288+
289+
```ts
290+
function searchMatrix(matrix: number[][], target: number): boolean {
291+
const m = matrix.length;
292+
const n = matrix[0].length;
293+
let left = 0;
294+
let right = m * n;
295+
while (left < right) {
296+
const mid = (left + right) >>> 1;
297+
const i = Math.floor(mid / n);
298+
const j = mid % n;
299+
if (matrix[i][j] === target) {
300+
return true;
301+
}
302+
303+
if (matrix[i][j] < target) {
304+
left = mid + 1;
305+
} else {
306+
right = mid;
307+
}
308+
}
309+
return false;
310+
}
311+
```
312+
287313
### **Rust**
288314

289315
```rust
@@ -306,6 +332,29 @@ impl Solution {
306332
}
307333
```
308334

335+
```rust
336+
use std::cmp::Ordering;
337+
impl Solution {
338+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
339+
let m = matrix.len();
340+
let n = matrix[0].len();
341+
let mut left = 0;
342+
let mut right = m * n;
343+
while left < right {
344+
let mid = left + (right - left) / 2;
345+
let i = mid / n;
346+
let j = mid % n;
347+
match matrix[i][j].cmp(&target) {
348+
Ordering::Equal => return true,
349+
Ordering::Less => left = mid + 1,
350+
Ordering::Greater => right = mid,
351+
}
352+
}
353+
false
354+
}
355+
}
356+
```
357+
309358
### **...**
310359

311360
```

‎solution/0000-0099/0074.Search a 2D Matrix/README_EN.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ func searchMatrix(matrix [][]int, target int) bool {
236236
}
237237
```
238238

239+
### **TypeScript**
240+
241+
```ts
242+
function searchMatrix(matrix: number[][], target: number): boolean {
243+
const m = matrix.length;
244+
const n = matrix[0].length;
245+
let left = 0;
246+
let right = m * n;
247+
while (left < right) {
248+
const mid = (left + right) >>> 1;
249+
const i = Math.floor(mid / n);
250+
const j = mid % n;
251+
if (matrix[i][j] === target) {
252+
return true;
253+
}
254+
255+
if (matrix[i][j] < target) {
256+
left = mid + 1;
257+
} else {
258+
right = mid;
259+
}
260+
}
261+
return false;
262+
}
263+
```
264+
239265
### **Rust**
240266

241267
```rust
@@ -258,6 +284,29 @@ impl Solution {
258284
}
259285
```
260286

287+
```rust
288+
use std::cmp::Ordering;
289+
impl Solution {
290+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
291+
let m = matrix.len();
292+
let n = matrix[0].len();
293+
let mut left = 0;
294+
let mut right = m * n;
295+
while left < right {
296+
let mid = left + (right - left) / 2;
297+
let i = mid / n;
298+
let j = mid % n;
299+
match matrix[i][j].cmp(&target) {
300+
Ordering::Equal => return true,
301+
Ordering::Less => left = mid + 1,
302+
Ordering::Greater => right = mid,
303+
}
304+
}
305+
false
306+
}
307+
}
308+
```
309+
261310
### **...**
262311

263312
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function searchMatrix(matrix: number[][], target: number): boolean {
2+
const m = matrix.length;
3+
const n = matrix[0].length;
4+
let left = 0;
5+
let right = m * n;
6+
while (left < right) {
7+
const mid = (left + right) >>> 1;
8+
const i = Math.floor(mid / n);
9+
const j = mid % n;
10+
if (matrix[i][j] === target) {
11+
return true;
12+
}
13+
14+
if (matrix[i][j] < target) {
15+
left = mid + 1;
16+
} else {
17+
right = mid;
18+
}
19+
}
20+
return false;
21+
}

‎solution/0000-0099/0078.Subsets/README.md‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,46 @@ func subsets(nums []int) [][]int {
217217
}
218218
```
219219

220+
### **TypeScript**
221+
222+
```ts
223+
function subsets(nums: number[]): number[][] {
224+
const n = nums.length;
225+
const t: number[] = [];
226+
const res: number[][] = [];
227+
const dfs = (i: number) => {
228+
if (i === n) {
229+
res.push([...t]);
230+
return;
231+
}
232+
dfs(i + 1);
233+
t.push(nums[i]);
234+
dfs(i + 1);
235+
t.pop();
236+
};
237+
dfs(0);
238+
return res;
239+
}
240+
```
241+
220242
### **Rust**
221243

222244
```rust
223245
impl Solution {
224-
fn dfs(nums:&Vec<i32>, res: &mut Vec<Vec<i32>>, i:usize, base:&mutVec<i32>) {
225-
letn= nums.len();
226-
ifi==n {
246+
fn dfs(i:usize, t:&mutVec<i32>, res: &mut Vec<Vec<i32>>, nums:&Vec<i32>) {
247+
ifi== nums.len() {
248+
res.push(t.clone());
227249
return;
228250
}
229-
for j in i..n {
230-
base.push(nums[j]);
231-
res.push(base.clone());
232-
Self::dfs(nums, res, j + 1, base);
233-
base.pop();
234-
}
251+
Self::dfs(i + 1, t, res, nums);
252+
t.push(nums[i]);
253+
Self::dfs(i + 1, t, res, nums);
254+
t.pop();
235255
}
236256

237257
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
238-
let mut base = vec![];
239-
let mut res = vec![vec![]];
240-
Self::dfs(&nums, &mut res, 0, &mut base);
258+
let mut res = Vec::new();
259+
Self::dfs(0, &mut Vec::new(), &mut res, &nums);
241260
res
242261
}
243262
}

‎solution/0000-0099/0078.Subsets/README_EN.md‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,46 @@ func subsets(nums []int) [][]int {
205205
}
206206
```
207207

208+
### **TypeScript**
209+
210+
```ts
211+
function subsets(nums: number[]): number[][] {
212+
const n = nums.length;
213+
const t: number[] = [];
214+
const res: number[][] = [];
215+
const dfs = (i: number) => {
216+
if (i === n) {
217+
res.push([...t]);
218+
return;
219+
}
220+
dfs(i + 1);
221+
t.push(nums[i]);
222+
dfs(i + 1);
223+
t.pop();
224+
};
225+
dfs(0);
226+
return res;
227+
}
228+
```
229+
208230
### **Rust**
209231

210232
```rust
211233
impl Solution {
212-
fn dfs(nums:&Vec<i32>, res: &mut Vec<Vec<i32>>, i:usize, base:&mutVec<i32>) {
213-
letn= nums.len();
214-
ifi==n {
234+
fn dfs(i:usize, path:&mutVec<i32>, res: &mut Vec<Vec<i32>>, nums:&Vec<i32>) {
235+
ifi== nums.len() {
236+
res.push(path.clone());
215237
return;
216238
}
217-
for j in i..n {
218-
base.push(nums[j]);
219-
res.push(base.clone());
220-
Self::dfs(nums, res, j + 1, base);
221-
base.pop();
222-
}
239+
Self::dfs(i + 1, path, res, nums);
240+
path.push(nums[i]);
241+
Self::dfs(i + 1, path, res, nums);
242+
path.pop();
223243
}
224244

225245
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
226-
let mut base = vec![];
227-
let mut res = vec![vec![]];
228-
Self::dfs(&nums, &mut res, 0, &mut base);
246+
let mut res = Vec::new();
247+
Self::dfs(0, &mut Vec::new(), &mut res, &nums);
229248
res
230249
}
231250
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
impl Solution {
2-
fn dfs(nums:&Vec<i32>, res: &mut Vec<Vec<i32>>, i:usize,base:&mutVec<i32>) {
3-
let n = nums.len();
4-
if i == n {
2+
fn dfs(i:usize,t:&mutVec<i32>, res: &mut Vec<Vec<i32>>, nums:&Vec<i32>) {
3+
if i == nums.len(){
4+
res.push(t.clone());
55
return;
66
}
7-
for j in i..n {
8-
base.push(nums[j]);
9-
res.push(base.clone());
10-
Self::dfs(nums, res, j + 1, base);
11-
base.pop();
12-
}
7+
Self::dfs(i + 1, t, res, nums);
8+
t.push(nums[i]);
9+
Self::dfs(i + 1, t, res, nums);
10+
t.pop();
1311
}
1412

1513
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
16-
let mut base = vec![];
17-
let mut res = vec![vec![]];
18-
Self::dfs(&nums, &mut res, 0, &mut base);
14+
let mut res = Vec::new();
15+
Self::dfs(0, &mut Vec::new(), &mut res, &nums);
1916
res
2017
}
2118
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function subsets(nums: number[]): number[][] {
2+
const n = nums.length;
3+
const t: number[] = [];
4+
const res: number[][] = [];
5+
const dfs = (i: number) => {
6+
if (i === n) {
7+
res.push([...t]);
8+
return;
9+
}
10+
dfs(i + 1);
11+
t.push(nums[i]);
12+
dfs(i + 1);
13+
t.pop();
14+
};
15+
dfs(0);
16+
return res;
17+
}

‎solution/0000-0099/0090.Subsets II/README.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,60 @@ func subsetsWithDup(nums []int) [][]int {
148148
}
149149
```
150150

151+
### **TypeScript**
152+
153+
```ts
154+
function subsetsWithDup(nums: number[]): number[][] {
155+
nums.sort((a, b) => a - b);
156+
const n = nums.length;
157+
const t: number[] = [];
158+
const res: number[][] = [];
159+
const dfs = (i: number) => {
160+
if (i === n) {
161+
res.push([...t]);
162+
return;
163+
}
164+
t.push(nums[i]);
165+
dfs(i + 1);
166+
const num = t.pop();
167+
while (i < n && nums[i] == num) {
168+
i++;
169+
}
170+
dfs(i);
171+
};
172+
dfs(0);
173+
return res;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
impl Solution {
181+
fn dfs(mut i: usize, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>, nums: &Vec<i32>) {
182+
let n = nums.len();
183+
if i == n {
184+
res.push(t.clone());
185+
return;
186+
}
187+
t.push(nums[i]);
188+
Self::dfs(i + 1, t, res, nums);
189+
let num = t.pop().unwrap();
190+
while i < n && num == nums[i] {
191+
i += 1;
192+
}
193+
Self::dfs(i, t, res, nums);
194+
}
195+
196+
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
197+
nums.sort();
198+
let mut res = Vec::new();
199+
Self::dfs(0, &mut Vec::new(), &mut res, &nums);
200+
res
201+
}
202+
}
203+
```
204+
151205
### **...**
152206

153207
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /