Skip to content

Commit df9d006

Browse files
authoredSep 3, 2021
feat: add java solution to lc problem: No.0846. Hand of Straights (doocs#552)
1 parent f97b4b7 commit df9d006

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed
 

‎solution/0800-0899/0846.Hand of Straights/README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,33 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```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+
}
6995
```
7096

7197
### **...**

‎solution/0800-0899/0846.Hand of Straights/README_EN.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,33 @@
5353
### **Java**
5454

5555
```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+
}
5783
```
5884

5985
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)