对于一个数组中的数分为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. lib下的Jar包在项目打包的时候提示不能找不到

    maven 使用本地包 lib jar包 依赖一个lib目录 解决方法: <plugin> <groupId>org.apache.maven.plugins</grou ...

  2. 基于虹软的Android的人脸识别SDK使用测试

    现在有很多人脸识别的技术我们可以拿来使用:但是个人认为还是离线端的SDK比较实用:所以个人一直在搜集人脸识别的SDK:原来使用开源的OpenCV:最近有个好友推荐虹软的ArcFace, 闲来无事就下来 ...

  3. js的柯里化currying

    转载:http://www.zhangxinxu.com/wordpress/2013/02/js-currying/ 我自己的理解柯里化就是这样的,(1)必须返回匿名函数,(2)参数复用. 1. 参 ...

  4. docker 安装mysql示例

    docker pull mysql 错误的启动: [root@localhost ~]# docker run --name mysql01 -d mysql 42f09819908bb72dd99a ...

  5. 《Practical Vim》第十章:复制和粘贴

    第十章和第十一章讲了 Vim 的寄存器功能, 寄存器,是用于保存文本的特定的容器.它的内容: 既可以是类似于系统剪切板功能的,用于粘贴的文本 也可以是录制成的宏的命令. 第十章讲使用寄存器使用复制与粘 ...

  6. Petrozavodsk Winter Camp, Day 8, 2014, Mosaic

    给你三个数字a,b,c,让你用1-m的数字凑出来 结论:有2个1和2个2肯定凑不出来,然后就搜索 #include <bits/stdc++.h> using namespace std; ...

  7. web前端性能优化总结一

    转自:http://www.2cto.com/kf/201604/498725.html 网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发 ...

  8. 深入理解vue-router之keep-alive

    keep-alive 简介 keep-alive 是 Vue 内置的一个组件,可以使被包含的组件保留状态,或避免重新渲染. 用法也很简单: ? 1 2 3 4 5 <keep-alive> ...

  9. unity中Event Trigger组件应用代码

    using UnityEngine; using System.Collections; using UnityEngine.Events; using UnityEngine.EventSystem ...

  10. Forth 采用Create,Does>定义新的词(word)& 延迟词技术

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...