Skip to content

Commit cdc0a9d

Browse files
committed
feat: add rust solution to lc problem: No.0594
No.0594.Longest Harmonious Subsequence
1 parent e1541b3 commit cdc0a9d

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

solution/0500-0599/0594.Longest Harmonious Subsequence/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ function findLHS(nums: number[]): number {
174174
}
175175
```
176176

177+
#### Rust
178+
179+
```rust
180+
use std::collections::HashMap;
181+
182+
impl Solution {
183+
pub fn find_lhs(nums: Vec<i32>) -> i32 {
184+
let mut cnt = HashMap::new();
185+
for &x in &nums {
186+
*cnt.entry(x).or_insert(0) += 1;
187+
}
188+
let mut ans = 0;
189+
for (&x, &c) in &cnt {
190+
if let Some(&y) = cnt.get(&(x + 1)) {
191+
ans = ans.max(c + y);
192+
}
193+
}
194+
ans
195+
}
196+
}
197+
```
198+
177199
<!-- tabs:end -->
178200

179201
<!-- solution:end -->

solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ function findLHS(nums: number[]): number {
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
use std::collections::HashMap;
177+
178+
impl Solution {
179+
pub fn find_lhs(nums: Vec<i32>) -> i32 {
180+
let mut cnt = HashMap::new();
181+
for &x in &nums {
182+
*cnt.entry(x).or_insert(0) += 1;
183+
}
184+
let mut ans = 0;
185+
for (&x, &c) in &cnt {
186+
if let Some(&y) = cnt.get(&(x + 1)) {
187+
ans = ans.max(c + y);
188+
}
189+
}
190+
ans
191+
}
192+
}
193+
```
194+
173195
<!-- tabs:end -->
174196

175197
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn find_lhs(nums: Vec<i32>) -> i32 {
5+
let mut cnt = HashMap::new();
6+
for &x in &nums {
7+
*cnt.entry(x).or_insert(0) += 1;
8+
}
9+
let mut ans = 0;
10+
for (&x, &c) in &cnt {
11+
if let Some(&y) = cnt.get(&(x + 1)) {
12+
ans = ans.max(c + y);
13+
}
14+
}
15+
ans
16+
}
17+
}

0 commit comments

Comments
 (0)