@@ -199,7 +199,17 @@ function reverseByRange(nums: number[], left: number, right: number): void {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
|
||||||
|
let k = k as usize % nums.len();
|
||||||
|
nums.reverse();
|
||||||
|
nums[..k].reverse();
|
||||||
|
nums[k..].reverse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
|||||||
@@ -169,7 +169,20 @@ void moveZeroes(int* nums, int numsSize){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn move_zeroes(nums: &mut Vec<i32>) {
|
||||||
|
let mut slow = 0;
|
||||||
|
for fast in 0..nums.len() {
|
||||||
|
if nums[fast] != 0 {
|
||||||
|
nums.swap(slow, fast);
|
||||||
|
slow += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -196,7 +196,22 @@ public class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn valid_mountain_array(arr: Vec<i32>) -> bool {
|
||||||
|
let mut i = 0;
|
||||||
|
let mut j = arr.len() - 1;
|
||||||
|
while i < arr.len() - 1 && arr[i] < arr[i + 1] {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
while j > 0 && arr[j] < arr[j - 1] {
|
||||||
|
j -= 1;
|
||||||
|
}
|
||||||
|
i > 0 && j < arr.len() - 1 && i == j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
|||||||
@@ -221,6 +221,28 @@ func uniqueOccurrences(arr []int) bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
impl Solution {
|
||||||
|
pub fn unique_occurrences(arr: Vec<i32>) -> bool {
|
||||||
|
let mut hash = HashMap::<i32, i32>::new();
|
||||||
|
for x in arr {
|
||||||
|
*hash.entry(x).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
let mut set = HashSet::<i32>::new();
|
||||||
|
for (_k, v) in hash {
|
||||||
|
if set.contains(&v) {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
set.insert(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -260,6 +260,22 @@ function smallerNumbersThanCurrent(nums: number[]): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### rust
|
||||||
|
```rust
|
||||||
|
use std::collections::HashMap;
|
||||||
|
impl Solution {
|
||||||
|
pub fn smaller_numbers_than_current(nums: Vec<i32>) -> Vec<i32> {
|
||||||
|
let mut v = nums.clone();
|
||||||
|
v.sort();
|
||||||
|
let mut hash = HashMap::new();
|
||||||
|
for i in 0..v.len() {
|
||||||
|
// rust中使用or_insert插入值, 如果存在就不插入,可以使用正序遍历
|
||||||
|
hash.entry(v[i]).or_insert(i as i32);
|
||||||
|
}
|
||||||
|
nums.into_iter().map(|x| *hash.get(&x).unwrap()).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
|||||||
Reference in New Issue
Block a user