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这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...
随机推荐
- MySql学习笔记【二、库相关操作】
命令规范 关键字.函数名称大写 数据库.表名.字段名小写 语句须以分号结尾 切换使用数据库 USE database_name 如:USE test 查看数据库列表 SHOW {DATABASES|S ...
- Windows问题
常用工具 DisplayFusion 官网 电脑分屏,V9.4 Pro 破解版 问题解决 Win64位注册表导入方法 64位Windows操作系统注册表不同于32位Windows操作系统,Win64 ...
- Delphi ClearCommError函数
- pandas中Series对象下的str所拥有的方法(df["xx"].str)
在使用pandas的时候,经常要对DataFrame的某一列进行操作,一般都会使用df["xx"].str下的方法,但是都有哪些方法呢?我们下面来罗列并演示一下.既然是df[&qu ...
- Ubuntu18.04安装 NVIDIA驱动
Ubuntu18.04安装 NVIDIA驱动 参考自博客:https://blog.csdn.net/jsjason1/article/details/88086904 我确定这篇文章是否很有必要,我 ...
- LeetCode03 - 无重复字符的最长子串(Java 实现)
LeetCode03 - 无重复字符的最长子串(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substri ...
- 第六章 组件 61 动画-小球动画flag标识符的作用分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- linux 安装weblogic(转载)
整个过程大同小异,不再笔记,就摘了网上的一篇. 原文地址:http://www.cnblogs.com/walk-the-Line/p/5409094.html 一.安装weblogic10.3.6 ...
- Zabbix Agent 安装指南和 Zabbix Server 设置自动发现
Zabbix Agent分为两种模式,被动模式(Passive)和主动模式( 我们实验在node1.yulongjun.com 和node2.yulongjun.com上分别配置Zabbix Agen ...
- java虚拟机的基本结构如图
1 java虚拟机的基本结构如图: 1)类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放于一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行时常量池信息,包括字 ...