Movatterモバイル変換


[0]ホーム

URL:


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

Commita12e2ef

Browse files
authored
Merge pull requestneetcode-gh#1986 from nirajvenkat/patch-5
Create 0074-search-a-2d-matrix.rs
2 parentsfc5ea09 +4f92246 commita12e2ef

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎rust/0074-search-a-2d-matrix.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::cmp::Ordering::{Equal,Less,Greater};
2+
3+
implSolution{
4+
pubfnsearch_matrix(matrix:Vec<Vec<i32>>,target:i32) ->bool{
5+
let(mut t,mut b) =(0, matrix.len());
6+
letmut row =0;
7+
while t < b{
8+
row = t +(b - t) /2;
9+
let first = matrix[row][0];
10+
let last =*matrix[row].last().unwrap();
11+
if target == first || target == last{
12+
returntrue;
13+
}elseif target < first{
14+
b = row;
15+
}elseif target > last{
16+
t = row +1;
17+
}else{
18+
break;
19+
}
20+
}
21+
22+
if t > b{
23+
returnfalse;
24+
}
25+
26+
let(mut l,mut r) =(0, matrix[row].len());
27+
while l < r{
28+
let col = l +(r - l) /2;
29+
match target.cmp(&matrix[row][col]){
30+
Equal =>returntrue,
31+
Less => r = col,
32+
Greater => l = col +1
33+
}
34+
}
35+
36+
false
37+
}
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp