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 1bc6338

Browse files
Merge pull request youngyangyang04#2784 from LLjjj-zed/master
feat: 新增部分题目Rust解法
2 parents 48fc6cf + 90255b8 commit 1bc6338

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

‎problems/0189.旋转数组.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,17 @@ function reverseByRange(nums: number[], left: number, right: number): void {
199199
}
200200
```
201201

202-
202+
### Rust
203+
```rust
204+
impl Solution {
205+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
206+
let k = k as usize % nums.len();
207+
nums.reverse();
208+
nums[..k].reverse();
209+
nums[k..].reverse();
210+
}
211+
}
212+
```
203213

204214

205215
<p align="center">

‎problems/0283.移动零.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,20 @@ void moveZeroes(int* nums, int numsSize){
169169
}
170170
```
171171
172-
172+
### Rust
173+
```rust
174+
impl Solution {
175+
pub fn move_zeroes(nums: &mut Vec<i32>) {
176+
let mut slow = 0;
177+
for fast in 0..nums.len() {
178+
if nums[fast] != 0 {
179+
nums.swap(slow, fast);
180+
slow += 1;
181+
}
182+
}
183+
}
184+
}
185+
```
173186

174187

175188

‎problems/0941.有效的山脉数组.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,22 @@ public class Solution {
196196
}
197197
```
198198

199-
199+
### Rust
200+
```rust
201+
impl Solution {
202+
pub fn valid_mountain_array(arr: Vec<i32>) -> bool {
203+
let mut i = 0;
204+
let mut j = arr.len() - 1;
205+
while i < arr.len() - 1 && arr[i] < arr[i + 1] {
206+
i += 1;
207+
}
208+
while j > 0 && arr[j] < arr[j - 1] {
209+
j -= 1;
210+
}
211+
i > 0 && j < arr.len() - 1 && i == j
212+
}
213+
}
214+
```
200215

201216
<p align="center">
202217
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/1207.独一无二的出现次数.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ func uniqueOccurrences(arr []int) bool {
221221
}
222222
```
223223

224+
### Rust
225+
226+
```rust
227+
use std::collections::{HashMap, HashSet};
228+
impl Solution {
229+
pub fn unique_occurrences(arr: Vec<i32>) -> bool {
230+
let mut hash = HashMap::<i32, i32>::new();
231+
for x in arr {
232+
*hash.entry(x).or_insert(0) += 1;
233+
}
234+
let mut set = HashSet::<i32>::new();
235+
for (_k, v) in hash {
236+
if set.contains(&v) {
237+
return false
238+
} else {
239+
set.insert(v);
240+
}
241+
}
242+
true
243+
}
244+
}
245+
```
224246

225247

226248

‎problems/1365.有多少小于当前数字的数字.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ function smallerNumbersThanCurrent(nums: number[]): number[] {
260260
};
261261
```
262262

263+
### rust
264+
```rust
265+
use std::collections::HashMap;
266+
impl Solution {
267+
pub fn smaller_numbers_than_current(nums: Vec<i32>) -> Vec<i32> {
268+
let mut v = nums.clone();
269+
v.sort();
270+
let mut hash = HashMap::new();
271+
for i in 0..v.len() {
272+
// rust中使用or_insert插入值, 如果存在就不插入,可以使用正序遍历
273+
hash.entry(v[i]).or_insert(i as i32);
274+
}
275+
nums.into_iter().map(|x| *hash.get(&x).unwrap()).collect()
276+
}
277+
}
278+
```
263279

264280

265281
<p align="center">

0 commit comments

Comments
(0)

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