标签:count 846 int 一手 TreeMap hand 顺子 card first
爱丽丝有一手(hand)由整数数组给定的牌。
现在她想把牌重新排列成组,使得每个组的大小都是 W,且由 W 张连续的牌组成。
如果她可以完成分组就返回 true,否则返回 false。
注意:此题目与 1296 重复:https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/hand-of-straights
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.TreeMap;
class Solution {
public boolean isNStraightHand(int[] hand, int W) {
TreeMap<Integer, Integer> count = new TreeMap();
for (int card: hand) {
if (!count.containsKey(card))
count.put(card, 1);
else
count.replace(card, count.get(card) + 1);
}
while (count.size() > 0) {
int first = count.firstKey();
for (int card = first; card < first + W; ++card) {
if (!count.containsKey(card)) return false;
int c = count.get(card);
if (c == 1) count.remove(card);
else count.replace(card, c - 1);
}
}
return true;
}
}
标签:count,846,int,一手,TreeMap,hand,顺子,card,first 来源: https://www.cnblogs.com/tianyiya/p/15703146.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。