File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
solution/0500-0599/0594.Longest Harmonious Subsequence Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments