G面经prepare: Straight Partition of A Deck of Cards
Define “Straight” as 5 cards with consecutive numbers. Determine if the deck can be fully divided into sets of “Straight”. Example: 1, 2, 3, 4, 4, 5, 5, 6, 7, 8 -> TrueYou may assume the cards are sorted
这个是用一个hashtable,key是数字,value是出现次数
然后遍历原数组,每一个数字都把hash里从自己开始往后5个color数都-1,如果发现缺数则说明不能分割
很容易错!
错了好多次,是color往后5个,如果不存在该color或者color数目已经为0,报错
1 package Straight; 2 import java.util.*; 3 4 public class Solution { 5 public boolean determine(int[] arr) { 6 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 7 for (int elem : arr) { 8 if (map.containsKey(elem)) { 9 map.put(elem, map.get(elem)+1); 10 } 11 else map.put(elem, 1); 12 } 13 14 for (int i=0; i<arr.length; i++) { 15 if(map.get(arr[i]) == 0) continue; 16 for (int j=arr[i]; j<arr[i]+5; j++) { 17 if (!map.containsKey(j)) return false; 18 if (map.get(j) == 0) return false; 19 else { 20 map.put(j, map.get(j)-1); 21 } 22 } 23 if (map.get(arr[i]) > 0) i--; 24 } 25 return true; 26 } 27 28 29 /** 30 * @param args 31 */ 32 public static void main(String[] args) { 33 // TODO Auto-generated method stub 34 Solution sol = new Solution(); 35 boolean res = sol.determine(new int[]{1,2,3,4,4,5,5,5,6,6,7,7,8,8,9}); 36 if (res) System.out.println("true"); 37 else System.out.println("false"); 38 } 39 40 }
转载于:https://www.cnblogs.com/EdwardLiu/p/5132214.html
G面经prepare: Straight Partition of A Deck of Cards相关推荐
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Set Intersection Set Difference
求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- 【Gym - 101775J】Straight Master(差分,思维)
题干: A straight is a poker hand containing five cards of sequential rank, not necessarily to be the s ...
- HDU 5228 ZCC loves straight flush( BestCoder Round #41)
题目链接:ZCC loves straight flush 题面: ZCC loves straight flush Time Limit: 2000/1000 MS (Java/Others) ...
- 百亿题典之C++编程题面试题
原文地址:百亿题典之C++编程题面试题作者:百亿题典 1. 在linked list中找倒数第N个结点 2. 倒转linked list 3. 二叉树的结点有指向parent的指针,求最近公共祖先 4 ...
- PG数据库内核源码分析——UPDATE
PG中UPDATE源码分析 本文主要描述SQL中UPDATE语句的源码分析,代码为PG13.3版本. 整体流程分析 以 update dtea set id = 1;这条最简单的Update语句进行源 ...
- Leetcode题目练习总结(持续更新......)
Leetcode题目练习 数组 1.两数之和 26. 删除排序数组中的重复项 27. 移除元素 35.搜索插入位置 53.最大子序列 66.加一 88.合并两个有序数组 118.杨辉三角 119.杨辉 ...
- C++编程,数据结构,算法类面试题集
1. 在linked list中找倒数第N个结点 2. 倒转linked list 3. 二叉树的结点有指向parent的指针,求最近公共祖先 4. 给一个数组,如何打印该数组成员构成集合的全部子集合 ...
最新文章
- Halcon与QT的联合编程(2)
- 猫猫学iOS(四十五)之常用的小功能比如打电话、打开网址、发邮件、发短信打开其他应用。...
- json数据格式 与 for in
- 我非要捅穿这 Neutron(四)Open vSwitch in Neutron
- C++编程练习(5)----“实现简单的循环队列的顺序存储结构“
- 银保监局招聘计算机岗位备考,2019年中国银保监会招聘计算机类如何备考?
- 笔记-知识产权与标准化知识-GB/T-12504-1990计算机软件质量保证计划规范
- IntelliJ IDEA中文乱码问题
- c语言实参和形参占用存储单元_必须知道的C语言知识细节:函数形参和实参的区别...
- MBR与GPT的区别
- mysql 5.7.20 win64_Win10下MySQL5.7.20 Mysql(64位)解压版安装及bug修复
- 一文读懂 - 云上用户如何灵活应用定制化网络服务
- vm ubuntu设置中文_如何在本地Ubuntu Linux机器或VM上设置LAMP服务器
- Nginx配置性能优化的方法
- Unity面试题精选(2)
- 工厂方法(Factory Pattern)
- 【编译器】Mac下gcc环境配置折腾
- 小宇飞刀与xieyunc
- 文件解压和压缩io流
- GT Transceiver中的重要时钟及其关系(1)GT Transceiver参考时钟