Skip to content

Commit de46a43

Browse files
committed
feat: update solutions to lcof problems: 04,50
- 面试题04. 二维数组中的查找 - 面试题50. 第一个只出现一次的字符
1 parent 4a2f472 commit de46a43

File tree

4 files changed

+30
-60
lines changed

4 files changed

+30
-60
lines changed

lcof/面试题04. 二维数组中的查找/README.md

+9-17
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,19 @@ function findNumberIn2DArray(matrix: number[][], target: number): boolean {
188188
### **Rust**
189189

190190
```rust
191+
use std::cmp::Ordering;
191192
impl Solution {
192193
pub fn find_number_in2_d_array(matrix: Vec<Vec<i32>>, target: i32) -> bool {
193-
let len_y = matrix.len();
194-
if len_y == 0 {
194+
if matrix.len() == 0 || matrix[0].len() == 0 {
195195
return false;
196196
}
197-
let len_x = matrix[0].len();
198-
if len_x == 0 {
199-
return false;
200-
}
201-
202-
let mut x = len_x - 1;
203-
let mut y = 0;
204-
while y < len_y {
205-
match target.cmp(&matrix[y][x]) {
206-
std::cmp::Ordering::Greater => y += 1,
207-
std::cmp::Ordering::Equal => return true,
208-
std::cmp::Ordering::Less => match x {
209-
0 => return false,
210-
_ => x -= 1,
211-
},
197+
let (m, n) = (matrix.len(), matrix[0].len());
198+
let (mut i, mut j) = (0, n);
199+
while i < m && j > 0 {
200+
match target.cmp(&matrix[i][j - 1]) {
201+
Ordering::Less => j -= 1,
202+
Ordering::Greater => i += 1,
203+
Ordering::Equal => return true,
212204
}
213205
}
214206
false

lcof/面试题04. 二维数组中的查找/Solution.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1+
use std::cmp::Ordering;
12
impl Solution {
23
pub fn find_number_in2_d_array(matrix: Vec<Vec<i32>>, target: i32) -> bool {
3-
let len_y = matrix.len();
4-
if len_y == 0 {
4+
if matrix.len() == 0 || matrix[0].len() == 0 {
55
return false;
66
}
7-
let len_x = matrix[0].len();
8-
if len_x == 0 {
9-
return false;
10-
}
11-
12-
let mut x = len_x - 1;
13-
let mut y = 0;
14-
while y < len_y {
15-
match target.cmp(&matrix[y][x]) {
16-
std::cmp::Ordering::Greater => y += 1,
17-
std::cmp::Ordering::Equal => return true,
18-
std::cmp::Ordering::Less => match x {
19-
0 => return false,
20-
_ => x -= 1,
21-
},
7+
let (m, n) = (matrix.len(), matrix[0].len());
8+
let (mut i, mut j) = (0, n);
9+
while i < m && j > 0 {
10+
match target.cmp(&matrix[i][j - 1]) {
11+
Ordering::Less => j -= 1,
12+
Ordering::Greater => i += 1,
13+
Ordering::Equal => return true,
2214
}
2315
}
2416
false

lcof/面试题50. 第一个只出现一次的字符/README.md

+7-14
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
对字符串进行两次遍历:
3030

31-
第一遍,先用 hash 表(或数组)统计字符串中每个字符出现的次数。
31+
第一遍,使用 hash 表(或数组)统计字符串中每个字符出现的次数。
3232

33-
第二遍,只要遍历到一个只出现一次的字符,那么就返回该字符,否则在遍历结束后返回空格
33+
第二遍,只要遍历到一个只出现一次的字符,那么就返回该字符,否则在遍历结束后,返回 `' '`
3434

3535
<!-- tabs:start -->
3636

@@ -130,22 +130,15 @@ function firstUniqChar(s: string): string {
130130

131131
```rust
132132
use std::collections::HashMap;
133-
134133
impl Solution {
135134
pub fn first_uniq_char(s: String) -> char {
136135
let mut map = HashMap::new();
137-
let s = s.chars().collect::<Vec<char>>();
138-
for c in s.iter() {
139-
match map.contains_key(c) {
140-
true => map.insert(c, false),
141-
false => map.insert(c, true),
142-
};
136+
for c in s.as_bytes() {
137+
map.insert(c, !map.contains_key(c));
143138
}
144-
for c in s.iter() {
145-
if let Some(is_single) = map.get(c) {
146-
if *is_single {
147-
return *c;
148-
}
139+
for c in s.as_bytes() {
140+
if map[c] {
141+
return char::from(*c);
149142
}
150143
}
151144
' '

lcof/面试题50. 第一个只出现一次的字符/Solution.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
use std::collections::HashMap;
2-
32
impl Solution {
43
pub fn first_uniq_char(s: String) -> char {
54
let mut map = HashMap::new();
6-
let s = s.chars().collect::<Vec<char>>();
7-
for c in s.iter() {
8-
match map.contains_key(c) {
9-
true => map.insert(c, false),
10-
false => map.insert(c, true),
11-
};
5+
for c in s.as_bytes() {
6+
map.insert(c, !map.contains_key(c));
127
}
13-
for c in s.iter() {
14-
if let Some(is_single) = map.get(c) {
15-
if *is_single {
16-
return *c;
17-
}
8+
for c in s.as_bytes() {
9+
if map[c] {
10+
return char::from(*c);
1811
}
1912
}
2013
' '

0 commit comments

Comments
 (0)