对于一个数组中的数分为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的更多相关文章

  1. 846. Hand of Straights - LeetCode

    Question 846. Hand of Straights Solution 题目大意:打牌,判断牌是否能全部按顺子出 思路:构造一个list,存储1,2,3,4,5,6,7,8并排序,构造一个m ...

  2. 【LeetCode】846. Hand of Straights 解题报告(Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 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 ...

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. [LeetCode] Hand of Straights 一手顺子牌

    Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  8. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  9. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

随机推荐

  1. vue style background

    vue 动态加载背景图 :style="{backgroundImage: 'url('+ item.imgList[0] +')',backgroundRepeat:'no-repeat' ...

  2. 经典算法问题的java实现 (一)

    原文链接: http://liuqing-2010-07.iteye.com/blog/1396859   1.如何计算闰年(Leap Year)?   四年一闰:百年不闰:四百年再闰.   具体参照 ...

  3. #const#const int *p 为何可以不初始化

    摘自http://www.myexception.cn/cpp/1900041.html const int *p 为什么可以不初始化?c++ primer 5th   P53 写道:const 对象 ...

  4. Web开发中button与submit区别

    submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了. 如果表单在点击提交按钮后需要用JS进行处理(包括输入验证)后再提交的话,通常都必须把submit改成butt ...

  5. spring cloud 版本号与 boot版本之间的对应关系(版本不对,会导致pom无法引入)

    版本号规则 Spring Cloud并没有熟悉的数字版本号,而是对应一个开发代号. 开发代号看似没有什么规律,但实际上首字母是有顺序的,比如:Dalston版本,我们可以简称 D 版本,对应的 Edg ...

  6. 00-python-常用命令

    1. pip 加速命令 pip install --index-url https://pypi.douban.com/simple 或者 pip install -i https://pypi.tu ...

  7. OPENAPI规范Swagger

    OPENAPI规范 是一种规范,Swagger是一种工具,Swagger帮我们使用OPENAPI更具体更完善,更好. 博客1:https://app.swaggerhub.com/help/index ...

  8. Caused by: java.lang.NoClassDefFoundError: com/google/common/base/MoreObjects

    环境:jdk1.8 开发工具:IDEA 说明:今天在做springboot集成swagger2的时候,在启动程序的时候,报错 报错信息: Error starting ApplicationConte ...

  9. ml交叉验证

    https://blog.csdn.net/guanyuqiu/article/details/86006474 https://blog.csdn.net/weixin_42660173/artic ...

  10. yii中通过HTTP post接收

    创建一个CURL.php的文件然后引用 1 <?php namespace frontend\models; class Curl { public function getCurl($url, ...