From baf7ed122b1cb78b89565b83b343a4f3c5f5ec82 Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:18:04 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=201207.=E7=8B=AC=E4=B8=80=E6=97=A0?= =?UTF-8?q?=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0=E6=AC=A1=E6=95=B0,?= =?UTF-8?q?=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1207.独一无二的出现次数.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index cd89522e..781badf5 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -221,6 +221,28 @@ func uniqueOccurrences(arr []int) bool { } ``` +### Rust + +```rust +use std::collections::{HashMap, HashSet}; +impl Solution { + pub fn unique_occurrences(arr: Vec) -> bool { + let mut hash = HashMap::::new(); + for x in arr { + *hash.entry(x).or_insert(0) += 1; + } + let mut set = HashSet::::new(); + for (_k, v) in hash { + if set.contains(&v) { + return false + } else { + set.insert(v); + } + } + true + } +} +``` From e43323b1e1f3556dc7bc38b4ae6bdd13508e0aea Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:18:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=201365.=E6=9C=89=E5=A4=9A=E5=B0=91?= =?UTF-8?q?=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E5=AD=97=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E5=AD=97,=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1365.有多少小于当前数字的数字.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 94c1eb77..64f61096 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -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) -> Vec { + 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() + } +} +```

From 30685b815cc8787fd84d760045a4e26456a51ef7 Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:19:43 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=200941.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=B1=B1=E8=84=89=E6=95=B0=E7=BB=84,=E6=96=B0=E5=A2=9Erust?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0941.有效的山脉数组.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index 9f63f441..77167df0 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -196,7 +196,22 @@ public class Solution { } ``` - +### Rust +```rust +impl Solution { + pub fn valid_mountain_array(arr: Vec) -> 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 + } +} +```

From 83de8ac2a1e3f6203f25e5b792acb22bc187676d Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:25:53 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=200283.=E7=A7=BB=E5=8A=A8=E9=9B=B6,?= =?UTF-8?q?=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0283.移动零.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 4d8fd9a1..cbce0295 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -169,7 +169,20 @@ void moveZeroes(int* nums, int numsSize){ } ``` - +### Rust +```rust +impl Solution { + pub fn move_zeroes(nums: &mut Vec) { + let mut slow = 0; + for fast in 0..nums.len() { + if nums[fast] != 0 { + nums.swap(slow, fast); + slow += 1; + } + } + } +} +``` From 90255b837ceb114caefd6a5e57eddadf2e0ace61 Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:31:49 +0800 Subject: [PATCH 5/5] =?UTF-8?q?feat:=200189.=E6=97=8B=E8=BD=AC=E6=95=B0?= =?UTF-8?q?=E7=BB=84,=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0189.旋转数组.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index b47ee4b9..e91109c6 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -199,7 +199,17 @@ function reverseByRange(nums: number[], left: number, right: number): void { } ``` - +### Rust +```rust +impl Solution { + pub fn rotate(nums: &mut Vec, k: i32) { + let k = k as usize % nums.len(); + nums.reverse(); + nums[..k].reverse(); + nums[k..].reverse(); + } +} +```