leetcode 846.Hand of Straights
对于一个数组中的数分为W组且在每一组内的数是连续存在的。
考虑使用map映射来记录每个数的个数的,并且对于数组中的数进行从小到大的排列的。同时每次需要更新最开始的那个起始数的,可能是以及出现的也可能是没有出现过的,这些都是需要考虑到的。
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
int n=hand.size();
if(n%W!=) return false;
map<int,int> m;
for(int i=;i<n;i++){
if(!m.count(hand[i])){
m[hand[i]]=;
}else{
m[hand[i]]++;
}
}
sort(hand.begin(),hand.end());
//int cnt=0;
int start=hand[];
for(int i=;i<n/W;i++){
//int start=hand[cnt];
for(int j=;j<W;j++){
cout<<start+j<<endl;
if(!m.count(start+j))
return false;
m[start+j]--;
if(m[start+j]<)
return false;
}
int flag=; // 考虑如果所有的数都被用完的话就需要取得新的数的
for(int j=;j<W;j++){
if(m[start+j]!=){
start=start+j;
flag=;
break;
}
}
if(!flag){
start=hand[(i+)*W];
}
}
return true;
}
};
其实是可以不用排序的,因为map本身就会按照关键字进行排序的,并且需要满足其数量相等的即可的。
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
int n=hand.size();
if(n%W!=) return false;
map<int, int> m;
for(int i=;i<n;i++) m[hand[i]]++;
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
int n=it->second;
if(n!=){
for(int i=it->first; i<it->first+W; i++){
if(!m.count(i) || m[i]<n)
return false;
m[i]-=n;
}
}
}
return true;
}
};
leetcode 846.Hand of Straights的更多相关文章
- 846. Hand of Straights - LeetCode
Question 846. Hand of Straights Solution 题目大意:打牌,判断牌是否能全部按顺子出 思路:构造一个list,存储1,2,3,4,5,6,7,8并排序,构造一个m ...
- 【LeetCode】846. Hand of Straights 解题报告(Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LC 846. Hand of Straights
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- [LeetCode] Hand of Straights 一手顺子牌
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
随机推荐
- Tomcat的overview界面说明
Tomcat的overview界面说明 一.Tomcat的overview界面 双击或者open,进入Tomcat的overview界面, 一般情况workspace的子路径为.metadata.pl ...
- poj 3294 Life Forms - 后缀数组 - 二分答案
题目传送门 传送门I 传送门II 题目大意 给定$n$个串,询问所有出现在严格大于$\frac{n}{2}$个串的最长串.不存在输出'?' 用奇怪的字符把它们连接起来.然后求sa,hei,二分答案,按 ...
- linux --- 5. nginx 初始
一. 安装nginx 1.安装nginxz之前的依赖包 yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel o ...
- JavaScript 声明全局变量和局部变量
JS中声明全局变量主要分为显式声明或者隐式声明下面分别介绍. 声明方式一: 使用var(关键字)+变量名(标识符)的方式在function外部声明,即为全局变量,否则在function声明的是局部变量 ...
- RPM Yum 相关命令及参数
RPM 命令 我们可以直接使用rpm命令,对软件包进行一些操作. 安装 rpm –ivh <package_name> rpm –Uvh <package_name> # 没有 ...
- 【C++】回看面向对象与C++
本文将记录在C++与面向对象的重新学习过程中的要点: 未定义行为
- BZOJ 1269 文本编辑器editor(伸展树)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1269 思路 伸展树(\(\text{splay}\))功能比较齐全的模板,能较好的体现 \( ...
- LeetCode in action
(1) Linked List: 2-add-two-numbers,2.cpp 19-remove-nth-node-from-end-of-list,TBD 21-merge-two-sorted ...
- How to fix TFS workspace mapping error in Jenkins
Once you had update in TFS workspace for Jenkin TFS plugin, you might get error like bellow: [worksp ...
- js 字符串,new String() 与 String()
function showCase(value) { switch(value) { case 'A': console.log('Case A'); break; case 'B': console ...