Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions solution/0500-0599/0594.Longest Harmonious Subsequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ function findLHS(nums: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn find_lhs(nums: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
let mut ans = 0;
for (&x, &c) in &cnt {
if let Some(&y) = cnt.get(&(x + 1)) {
ans = ans.max(c + y);
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ function findLHS(nums: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn find_lhs(nums: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
let mut ans = 0;
for (&x, &c) in &cnt {
if let Some(&y) = cnt.get(&(x + 1)) {
ans = ans.max(c + y);
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
17 changes: 17 additions & 0 deletions solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::collections::HashMap;

impl Solution {
pub fn find_lhs(nums: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
let mut ans = 0;
for (&x, &c) in &cnt {
if let Some(&y) = cnt.get(&(x + 1)) {
ans = ans.max(c + y);
}
}
ans
}
}