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 groups so that each group is size W, and consists of Wconsecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice'shandcan be rearranged as[1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice'shandcan't be rearranged into groups of4.
Note:
1 <= hand.length <= 100000 <= hand[i] <= 10^91 <= W <= hand.length
Runtime: 80 ms, faster than 24.90% of C++ online submissions for Hand of Straights.
我的思路很直接,用map保存。
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
int n = hand.size();
if (n < || W < || n < W)
{
return false;
}
if (n % W != )
{
return false;
}
map<int,int> mp;
for(auto x : hand) mp[x]++;
for(auto it : mp){
if(it.second == ) continue;
int cntit = it.second;
for(int i=; i<W;i++){
if(!mp.count(it.first) || mp[it.first+i] < mp[it.first]) return false;
mp[it.first+i] -= mp[it.first];
}
mp[it.first] = ;
}
for(auto it : mp){
if(it.second != ) return false;
}
return true;
}
};
网上看到一个比较好的思路是,把所有的数%W,因为是连续的,所以一个连续的W个数modW后必定在0 ~ W-1 中是连续存在的,妙。
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
int n = hand.size();
if (n < || W < || n < W)
{
return false;
}
if (n % W != )
{
return false;
}
vector<int> count (W, );
for (const int& h : hand)
{
++count[h % W];
}
int expect = count.front();
for(const int& c : count)
{
if (c != expect)
{
return false;
}
}
return true;
}
};
LC 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
对于一个数组中的数分为W组且在每一组内的数是连续存在的. 考虑使用map映射来记录每个数的个数的,并且对于数组中的数进行从小到大的排列的.同时每次需要更新最开始的那个起始数的,可能是以及出现的也可能是 ...
- 【LeetCode】846. Hand of Straights 解题报告(Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。
laviewpbt 2014.8.4 编辑 Email:laviewpbt@sina.com QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
- “LC.exe”错误
错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...
- 解决VS下“LC.exe已退出,代码为-1”问题
今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...
- 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...
- Lc.exe已退出,代码为-1
编译项目,出现提示"Lc.exe已退出,代码为-1" . 解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...
随机推荐
- EF 将MSSQL 更换成 POSTRESQL
前提概要:项目里已存在MSSQL 的 DB FIRST 的EDMX, 想将项目的数据库转换成 POSTGRESQL. 解决方法: 1,新建项目, 连接MSSQL 建立模型,用来源于数据库 CODE F ...
- php--常见算法1
<?php //递归输出123321 function digui($num){ echo $num; if($num<3){ digui($num+1); } echo $num; } ...
- Linux:rm可不可以实现删除所有文件,除了demo文件
方法1: shopt -s extglob #开启扩展通配符 rm -rf !(demo) #删除除了demo的文件 方法2: find /test -not -name "demo&quo ...
- DHCP显示
两种PXE启动芯片 开机显示:Inter® Boot Agent GE V1.2.45或者Intel UNDI PXE2.0 (Build 082):其中UNDI是Universal Network ...
- jQuery获取表单全部数据
iQuery如何获取表单的全部数据,用于ajax提交 var formData = {}; var t = $('#Form').serializeArray(); $.each(t, functio ...
- JMeter函数整理
"_intSum” 功能:用于计算多个整数的和,可以是计算正整数和负整数的和,它有N个参数,最少有3个参数,最多不限.最后一个参数是函数名称,前面的其它参数是要求和的整数.这个函数在函数对话 ...
- PAT乙级1031
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392 题解 emmm.对于每个身份证号, ...
- Spring 事务相关
事务类型 数据库事务类型有本地事务和分布式事务: 本地事务:就是普通事务,能保证单台数据库上的操作的ACID,被限定在一台数据库上: 分布式事务:涉及两个或多个数据库源的事务,即跨越多台同类或异类数据 ...
- Gym - 101908J Joining Capitals (斯坦纳树)
题意:二维平面上有n(n<=100)个点,其中k个是星星(k<=10),现要构造一棵树,每个星星对应树上的一个叶子结点,求最小花费(总花费为树上所有边的长度(两点间欧几里得距离)) 斯坦纳 ...
- 数据管理必看!Kendo UI for jQuery过滤器的全球化
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...