Skip to content

Commit 7a82671

Browse files
committed
feat: update solutions to lcof problems
- 面试题03. 数组中重复的数字 - 面试题53 - I. 在排序数组中查找数字 I - 面试题53 - II. 0~n-1中缺失的数字
1 parent b949258 commit 7a82671

File tree

6 files changed

+71
-65
lines changed

6 files changed

+71
-65
lines changed

lcof/面试题03. 数组中重复的数字/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ impl Solution {
177177
pub fn find_repeat_number(mut nums: Vec<i32>) -> i32 {
178178
for i in 0..nums.len() {
179179
while i as i32 != nums[i] {
180-
if nums[i] == nums[nums[i] as usize] {
180+
let j = nums[i] as usize;
181+
if nums[i] == nums[j] {
181182
return nums[i];
182183
}
183-
let j = nums[i] as usize;
184184
nums.swap(i, j);
185185
}
186186
}

lcof/面试题03. 数组中重复的数字/Solution.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ impl Solution {
22
pub fn find_repeat_number(mut nums: Vec<i32>) -> i32 {
33
for i in 0..nums.len() {
44
while i as i32 != nums[i] {
5-
if nums[i] == nums[nums[i] as usize] {
5+
let j = nums[i] as usize;
6+
if nums[i] == nums[j] {
67
return nums[i];
78
}
8-
let j = nums[i] as usize;
99
nums.swap(i, j);
1010
}
1111
}

lcof/面试题53 - I. 在排序数组中查找数字 I/README.md

+12-30
Original file line numberDiff line numberDiff line change
@@ -216,39 +216,21 @@ var search = function (nums, target) {
216216
```rust
217217
impl Solution {
218218
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
219-
fn my_search(nums: &Vec<i32>, target: i32, left: i32, right: i32) -> i32 {
220-
if left > right {
221-
return 0;
222-
}
223-
224-
let index = (right - left) / 2 + left;
225-
let num = nums[index as usize];
226-
if num > target {
227-
my_search(nums, target, left, index - 1)
228-
} else if num < target {
229-
my_search(nums, target, index + 1, right)
230-
} else {
231-
// 搜索边界
232-
let mut count = 1;
233-
for i in (0..index).rev() {
234-
if nums[i as usize] == target {
235-
count += 1;
236-
} else {
237-
break;
238-
}
219+
let help = |target| {
220+
let mut left = 0;
221+
let mut right = nums.len();
222+
while left < right {
223+
let mid = left + (right - left) / 2;
224+
if nums[mid] <= target {
225+
left = mid + 1;
226+
} else {
227+
right = mid;
239228
}
240-
for i in (index + 1)..nums.len() as i32 {
241-
if nums[i as usize] == target {
242-
count += 1;
243-
} else {
244-
break;
245-
}
246-
}
247-
count
248229
}
249-
}
230+
left as i32
231+
};
250232

251-
my_search(&nums, target, 0, nums.len() as i32 - 1)
233+
help(target) - help(target - 1)
252234
}
253235
}
254236
```
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11
impl Solution {
22
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
3-
fn my_search(nums: &Vec<i32>, target: i32, left: i32, right: i32) -> i32 {
4-
if left > right {
5-
return 0;
6-
}
7-
8-
let index = (right - left) / 2 + left;
9-
let num = nums[index as usize];
10-
if num > target {
11-
my_search(nums, target, left, index - 1)
12-
} else if num < target {
13-
my_search(nums, target, index + 1, right)
14-
} else {
15-
// 搜索边界
16-
let mut count = 1;
17-
for i in (0..index).rev() {
18-
if nums[i as usize] == target {
19-
count += 1;
20-
} else {
21-
break;
22-
}
23-
}
24-
for i in (index + 1)..nums.len() as i32 {
25-
if nums[i as usize] == target {
26-
count += 1;
27-
} else {
28-
break;
29-
}
3+
let help = |target| {
4+
let mut left = 0;
5+
let mut right = nums.len();
6+
while left < right {
7+
let mid = left + (right - left) / 2;
8+
if nums[mid] <= target {
9+
left = mid + 1;
10+
} else {
11+
right = mid;
3012
}
31-
count
3213
}
33-
}
14+
left as i32
15+
};
3416

35-
my_search(&nums, target, 0, nums.len() as i32 - 1)
17+
help(target) - help(target - 1)
3618
}
37-
}
19+
}

lcof/面试题53 - II. 0~n-1中缺失的数字/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,36 @@ public:
116116
};
117117
```
118118
119+
### **Rust**
120+
121+
```rust
122+
impl Solution {
123+
pub fn missing_number(nums: Vec<i32>) -> i32 {
124+
let n = nums.len() as i32;
125+
let mut sum = (1 + n) * n / 2;
126+
for num in nums.iter() {
127+
sum -= num;
128+
}
129+
sum
130+
}
131+
}
132+
```
133+
134+
```rust
135+
impl Solution {
136+
pub fn missing_number(nums: Vec<i32>) -> i32 {
137+
let mut prev = 0;
138+
for &num in nums.iter() {
139+
if prev != num {
140+
return prev;
141+
}
142+
prev += 1;
143+
}
144+
prev
145+
}
146+
}
147+
```
148+
119149
### **...**
120150

121151
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn missing_number(nums: Vec<i32>) -> i32 {
3+
let mut prev = 0;
4+
for &num in nums.iter() {
5+
if prev != num {
6+
return prev;
7+
}
8+
prev += 1;
9+
}
10+
prev
11+
}
12+
}

0 commit comments

Comments
 (0)