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博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
随机推荐
- 剑指offer(33)丑数
题目描述 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题目分析 ...
- 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.
异常:ArgumentException: The Assembly System.Configuration is referenced by System.Data. But the dll is ...
- jsp/servlet学习二之servlet详解
Servlet API概览 Servlet API有一下四个java包: 1,javax.servlet,其中包含定义servlet和servlet容器之间契约的类和接口. 2,javax.servl ...
- 表达式引擎aviator
Aviator是一个轻量级.高性能的Java表达式执行引擎, 本文内容主要来自于官方文档 简介 包依赖 使用手册 执行表达式 使用变量 exec 方法 调用函数 自定义函数 编译表达式 访问数组和集合 ...
- 优雅的重载toString方法,打印对象内容而不是打印内存地址的方法
如果直接在日志或者System.out.println中打印java对象,会打印这个对象的内存地址,而不是具体内容. 为了便于调试,一般的做法有2种: 1.重写toStrong方法 2.将对象传入JS ...
- Android 回退键监听
回退键(back)监听:方法1:回调方法onBackPressed String LOG_TAG="TAG"; @Override public void onBackPr ...
- itsdangerous
将字典进行加密.解密 通过私钥保证安全性 serializer=TimedJSONWebSignatureSerializer(私钥,过期时间) dumps(字典)=====>返回加密字符串 l ...
- Rails6.0 Beta版本1: Action Text的简单使用
主要功能是新增2个主要的框架Mailbox和action Text. 和2个重要的可扩展的升级: multiple databases support和parallel testing. Action ...
- 码云 git sourceTree 私有
1:首先注册码云账号,并建立一个私有项目 2:私有项目连接需要通过SSH验证,我们先在window上安装好git,然后打开git cmd 3:执行命令 ssh-keygen -t rsa -C &qu ...
- php搜索附近人及显示男生女生分开
// 滚动切换标签样式 switchTab: function (e) { this.setData({ currentTab: e.detail.current }); this.checkCor( ...