Skip to content

Commit e43323b

Browse files
author
junjie2.luo
committed
feat: 1365.有多少小于当前数字的数字,新增rust解法
1 parent baf7ed1 commit e43323b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

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)