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这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...
随机推荐
- go语言时间函数
以YY-mm-dd HH:MM:SS.9位 输出当前时间: func main() { fmt.Println(time.Now()) // 2019-11-15 16:26:12.4807588 + ...
- linux之网络命令
本文整理了在实践过程中使用的Linux网络工具,这些工具提供的功能非常强大,我们平时使用的只是冰山一角,比如lsof.ip.tcpdump.iptables等. 本文不会深入研究这些命令的强大用法,因 ...
- 适配器 1、ArrayAdapter 2.SimpleAdapter
1.ArrayAdapter(数组适配器):用于绑定格式单一的数据.数据源:可以是集合或数组 public class MainActivity extends AppCompatActivity { ...
- DA_04_解决Xshell中文乱码问题
一:解决Xshell中文乱码问题 二:解决克隆后eth0不见的问题 2.1:虚拟机克隆步骤: 1.直接修改 /etc/sysconfig/network-script/i ...
- manjaro 基本系统配置
1.更新源 vim /etc/pacman.conf [archlinuxcn] SigLevel = Never Server = http://mirrors.tuna.tsinghua.edu. ...
- 使用metamask钱包
一.安装火狐浏览器metamask插件 打开火狐浏览器的附件组件,搜索metamask 点击第一个 点击“添加到Firefox” 添加成功后,浏览器右上角有一个狐狸标志 点击这个标志,打开插件 二.创 ...
- mybatic进阶遗留
参考文章: MyBatis的架构设计以及实例分析 MyBatis缓存机制的设计与实现 MyBatis的一级缓存实现详解 及使用注意事项 MyBatis的二级缓存的设计原理
- 使用Apache Curator管理ZooKeeper(转)
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- 如何获取到一个form中的所有子控件?
使用yield关键字,非常的方便 private static IEnumerable<Control> GetChildren(Control frmRootDock) { if (fr ...
- h5构建页面(rem布局准备)
新建一个flex.js ! function () { function t(t) { return this.config = t, this } t.prototype = { reset: fu ...