Skip to content

Commit 1139bb9

Browse files
committed
feat: add rust solutions to lc/lcof problems
- lcof No.01 - lc No.0189,0528
1 parent 602738b commit 1139bb9

File tree

8 files changed

+212
-0
lines changed

8 files changed

+212
-0
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ function swap(nums: number[], i: number, j: number): void {
163163
}
164164
```
165165

166+
### **Rust**
167+
168+
```rs
169+
impl Solution {
170+
pub fn find_repeat_number(mut nums: Vec<i32>) -> i32 {
171+
for i in 0..nums.len() {
172+
while i as i32 != nums[i] {
173+
if nums[i] == nums[nums[i] as usize] {
174+
return nums[i];
175+
}
176+
let j = nums[i] as usize;
177+
nums.swap(i, j);
178+
}
179+
}
180+
-1
181+
}
182+
}
183+
```
184+
166185
### **...**
167186

168187
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn find_repeat_number(mut nums: Vec<i32>) -> i32 {
3+
for i in 0..nums.len() {
4+
while i as i32 != nums[i] {
5+
if nums[i] == nums[nums[i] as usize] {
6+
return nums[i];
7+
}
8+
let j = nums[i] as usize;
9+
nums.swap(i, j);
10+
}
11+
}
12+
-1
13+
}
14+
}

solution/0100-0199/0189.Rotate Array/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ func reverse(nums []int, i, j int) {
157157
}
158158
```
159159

160+
### **Rust**
161+
162+
```rs
163+
impl Solution {
164+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
165+
let n = nums.len();
166+
let k = k as usize % n;
167+
if n == 1 || k == 0 {
168+
return;
169+
}
170+
171+
nums.reverse();
172+
nums[..k].reverse();
173+
nums[k..].reverse();
174+
}
175+
}
176+
```
177+
160178
### **...**
161179

162180
```

solution/0100-0199/0189.Rotate Array/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ func reverse(nums []int, i, j int) {
132132
}
133133
```
134134

135+
### **Rust**
136+
137+
```rs
138+
impl Solution {
139+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
140+
let n = nums.len();
141+
let k = k as usize % n;
142+
if n == 1 || k == 0 {
143+
return;
144+
}
145+
146+
nums.reverse();
147+
nums[..k].reverse();
148+
nums[k..].reverse();
149+
}
150+
}
151+
```
152+
135153
### **...**
136154

137155
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
3+
let n = nums.len();
4+
let k = k as usize % n;
5+
if n == 1 || k == 0 {
6+
return;
7+
}
8+
9+
nums.reverse();
10+
nums[..k].reverse();
11+
nums[k..].reverse();
12+
}
13+
}

solution/0500-0599/0528.Random Pick with Weight/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,51 @@ Solution.prototype.pickIndex = function () {
250250
*/
251251
```
252252

253+
### **Rust**
254+
255+
```rs
256+
use rand::{thread_rng, Rng};
257+
258+
struct Solution {
259+
sum: Vec<i32>,
260+
}
261+
262+
/**
263+
* `&self` means the method takes an immutable reference.
264+
* If you need a mutable reference, change it to `&mut self` instead.
265+
*/
266+
impl Solution {
267+
fn new(w: Vec<i32>) -> Self {
268+
let n = w.len();
269+
let mut sum = vec![0; n + 1];
270+
for i in 1..=n {
271+
sum[i] = sum[i - 1] + w[i - 1];
272+
}
273+
Self { sum }
274+
}
275+
276+
fn pick_index(&self) -> i32 {
277+
let x = thread_rng().gen_range(1, self.sum.last().unwrap() + 1);
278+
let (mut left, mut right) = (1, self.sum.len() - 1);
279+
while left < right {
280+
let mid = (left + right) >> 1;
281+
if self.sum[mid] < x {
282+
left = mid + 1;
283+
} else {
284+
right = mid;
285+
}
286+
}
287+
(left - 1) as i32
288+
}
289+
}
290+
291+
/**
292+
* Your Solution object will be instantiated and called as such:
293+
* let obj = Solution::new(w);
294+
* let ret_1: i32 = obj.pick_index();
295+
*/
296+
```
297+
253298
### **...**
254299

255300
```

solution/0500-0599/0528.Random Pick with Weight/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,51 @@ Solution.prototype.pickIndex = function () {
240240
*/
241241
```
242242

243+
### **Rust**
244+
245+
```rs
246+
use rand::{thread_rng, Rng};
247+
248+
struct Solution {
249+
sum: Vec<i32>,
250+
}
251+
252+
/**
253+
* `&self` means the method takes an immutable reference.
254+
* If you need a mutable reference, change it to `&mut self` instead.
255+
*/
256+
impl Solution {
257+
fn new(w: Vec<i32>) -> Self {
258+
let n = w.len();
259+
let mut sum = vec![0; n + 1];
260+
for i in 1..=n {
261+
sum[i] = sum[i - 1] + w[i - 1];
262+
}
263+
Self { sum }
264+
}
265+
266+
fn pick_index(&self) -> i32 {
267+
let x = thread_rng().gen_range(1, self.sum.last().unwrap() + 1);
268+
let (mut left, mut right) = (1, self.sum.len() - 1);
269+
while left < right {
270+
let mid = (left + right) >> 1;
271+
if self.sum[mid] < x {
272+
left = mid + 1;
273+
} else {
274+
right = mid;
275+
}
276+
}
277+
(left - 1) as i32
278+
}
279+
}
280+
281+
/**
282+
* Your Solution object will be instantiated and called as such:
283+
* let obj = Solution::new(w);
284+
* let ret_1: i32 = obj.pick_index();
285+
*/
286+
```
287+
243288
### **...**
244289

245290
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use rand::{thread_rng, Rng};
2+
3+
struct Solution {
4+
sum: Vec<i32>,
5+
}
6+
7+
/**
8+
* `&self` means the method takes an immutable reference.
9+
* If you need a mutable reference, change it to `&mut self` instead.
10+
*/
11+
impl Solution {
12+
fn new(w: Vec<i32>) -> Self {
13+
let n = w.len();
14+
let mut sum = vec![0; n + 1];
15+
for i in 1..=n {
16+
sum[i] = sum[i - 1] + w[i - 1];
17+
}
18+
Self { sum }
19+
}
20+
21+
fn pick_index(&self) -> i32 {
22+
let x = thread_rng().gen_range(1, self.sum.last().unwrap() + 1);
23+
let (mut left, mut right) = (1, self.sum.len() - 1);
24+
while left < right {
25+
let mid = (left + right) >> 1;
26+
if self.sum[mid] < x {
27+
left = mid + 1;
28+
} else {
29+
right = mid;
30+
}
31+
}
32+
(left - 1) as i32
33+
}
34+
}
35+
36+
/**
37+
* Your Solution object will be instantiated and called as such:
38+
* let obj = Solution::new(w);
39+
* let ret_1: i32 = obj.pick_index();
40+
*/

0 commit comments

Comments
 (0)