feat: 1365.有多少小于当前数字的数字,新增rust解法
This commit is contained in:
parent
baf7ed122b
commit
e43323b1e1
|
|
@ -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">
|
||||
|
|
|
|||
Loading…
Reference in New Issue