File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -394,6 +394,7 @@ mod q834;
394394mod q839;
395395mod q842;
396396mod q844;
397+ mod q846;
397398mod q849;
398399mod q852;
399400mod q856;
Original file line number Diff line number Diff line change 1+ use crate :: q:: Solution ;
2+
3+ #[ allow( unused) ]
4+ impl Solution {
5+ pub fn is_n_straight_hand ( hand : Vec < i32 > , group_size : i32 ) -> bool {
6+ // 方法1
7+ // 排序hand然后,将所有的数字的频率存入hashmap
8+ // 依次迭代hand,如果当前数字没有量了,则继续迭代
9+ // 如果还有量,取出来处理,查看hashmap中是否存在,如果存在,则继续
10+ // 直到达到group_size,并去掉hashmap中的量
11+ // 如果没有达到group_size,则返回失败
12+ // AC 16ms 2.3mb 73/73
13+ use std:: collections:: HashMap ;
14+ let n = hand. len ( ) ;
15+ let mut hand = hand;
16+ hand. sort_unstable ( ) ;
17+ Ï
18+ let mut freq = HashMap :: new ( ) ;
19+ for i in 0 ..n {
20+ * freq. entry ( hand[ i] ) . or_insert ( 0 ) += 1 ;
21+ }
22+ for i in 0 ..n {
23+ let x = hand[ i] ;
24+ let count = freq. get_mut ( & x) . unwrap ( ) ;
25+ if * count > 0 {
26+ * count -= 1 ;
27+ for j in 1 ..group_size {
28+ let y = x + j;
29+ if let Some ( count) = freq. get_mut ( & y) {
30+ if * count > 0 {
31+ * count -= 1 ;
32+ } else {
33+ return false ;
34+ }
35+ } else {
36+ return false ;
37+ }
38+ }
39+ }
40+ }
41+ true
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments