Skip to content

feat: add java solution to lc problem: NO.0846. Hand of Straights #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2021
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
28 changes: 27 additions & 1 deletion solution/0800-0899/0846.Hand of Straights/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,33 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length % groupSize != 0) {
return false;
}
TreeMap<Integer, Integer> mp = new TreeMap<>();
for (int item : hand) {
mp.put(item, mp.getOrDefault(item, 0) + 1);
}

while (mp.size() > 0) {
int start = mp.firstKey();
for (int i = start; i < start + groupSize; i++) {
if (!mp.containsKey(i)) {
return false;
}
int time = mp.get(i);
if (time == 1) {
mp.remove(i);
} else {
mp.replace(i, time - 1);
}
}
}
return true;
}
}
```

### **...**
Expand Down
28 changes: 27 additions & 1 deletion solution/0800-0899/0846.Hand of Straights/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,33 @@
### **Java**

```java

class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length % groupSize != 0) {
return false;
}
TreeMap<Integer, Integer> mp = new TreeMap<>();
for (int item : hand) {
mp.put(item, mp.getOrDefault(item, 0) + 1);
}

while (mp.size() > 0) {
int start = mp.firstKey();
for (int i = start; i < start + groupSize; i++) {
if (!mp.containsKey(i)) {
return false;
}
int time = mp.get(i);
if (time == 1) {
mp.remove(i);
} else {
mp.replace(i, time - 1);
}
}
}
return true;
}
}
```

### **...**
Expand Down
27 changes: 27 additions & 0 deletions solution/0800-0899/0846.Hand of Straights/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length % groupSize != 0) {
return false;
}
TreeMap<Integer, Integer> mp = new TreeMap<>();
for (int item : hand) {
mp.put(item, mp.getOrDefault(item, 0) + 1);
}

while (mp.size() > 0) {
int start = mp.firstKey();
for (int i = start; i < start + groupSize; i++) {
if (!mp.containsKey(i)) {
return false;
}
int time = mp.get(i);
if (time == 1) {
mp.remove(i);
} else {
mp.replace(i, time - 1);
}
}
}
return true;
}
}