File tree 3 files changed +81
-2
lines changed
solution/0800-0899/0846.Hand of Straights
3 files changed +81
-2
lines changed Original file line number Diff line number Diff line change 65
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
66
67
67
``` java
68
-
68
+ class Solution {
69
+ public boolean isNStraightHand (int [] hand , int groupSize ) {
70
+ if (hand. length % groupSize != 0 ) {
71
+ return false ;
72
+ }
73
+ TreeMap<Integer , Integer > mp = new TreeMap<> ();
74
+ for (int item : hand) {
75
+ mp. put(item, mp. getOrDefault(item, 0 ) + 1 );
76
+ }
77
+
78
+ while (mp. size() > 0 ) {
79
+ int start = mp. firstKey();
80
+ for (int i = start; i < start + groupSize; i++ ) {
81
+ if (! mp. containsKey(i)) {
82
+ return false ;
83
+ }
84
+ int time = mp. get(i);
85
+ if (time == 1 ) {
86
+ mp. remove(i);
87
+ } else {
88
+ mp. replace(i, time - 1 );
89
+ }
90
+ }
91
+ }
92
+ return true ;
93
+ }
94
+ }
69
95
```
70
96
71
97
### ** ...**
Original file line number Diff line number Diff line change 53
53
### ** Java**
54
54
55
55
``` java
56
-
56
+ class Solution {
57
+ public boolean isNStraightHand (int [] hand , int groupSize ) {
58
+ if (hand. length % groupSize != 0 ) {
59
+ return false ;
60
+ }
61
+ TreeMap<Integer , Integer > mp = new TreeMap<> ();
62
+ for (int item : hand) {
63
+ mp. put(item, mp. getOrDefault(item, 0 ) + 1 );
64
+ }
65
+
66
+ while (mp. size() > 0 ) {
67
+ int start = mp. firstKey();
68
+ for (int i = start; i < start + groupSize; i++ ) {
69
+ if (! mp. containsKey(i)) {
70
+ return false ;
71
+ }
72
+ int time = mp. get(i);
73
+ if (time == 1 ) {
74
+ mp. remove(i);
75
+ } else {
76
+ mp. replace(i, time - 1 );
77
+ }
78
+ }
79
+ }
80
+ return true ;
81
+ }
82
+ }
57
83
```
58
84
59
85
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isNStraightHand (int [] hand , int groupSize ) {
3
+ if (hand .length % groupSize != 0 ) {
4
+ return false ;
5
+ }
6
+ TreeMap <Integer , Integer > mp = new TreeMap <>();
7
+ for (int item : hand ) {
8
+ mp .put (item , mp .getOrDefault (item , 0 ) + 1 );
9
+ }
10
+
11
+ while (mp .size () > 0 ) {
12
+ int start = mp .firstKey ();
13
+ for (int i = start ; i < start + groupSize ; i ++) {
14
+ if (!mp .containsKey (i )) {
15
+ return false ;
16
+ }
17
+ int time = mp .get (i );
18
+ if (time == 1 ) {
19
+ mp .remove (i );
20
+ } else {
21
+ mp .replace (i , time - 1 );
22
+ }
23
+ }
24
+ }
25
+ return true ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments