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 -> True
You may assume the cards are sorted

这个是用一个hashtable,key是数字,value是出现次数

然后遍历原数组,每一个数字都把hash里从自己开始往后5个color数都-1,如果发现缺数则说明不能分割

很容易错!

错了好多次,是color往后5个,如果不存在该color或者color数目已经为0,报错

 package Straight;
import java.util.*; public class Solution {
public boolean determine(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int elem : arr) {
if (map.containsKey(elem)) {
map.put(elem, map.get(elem)+1);
}
else map.put(elem, 1);
} for (int i=0; i<arr.length; i++) {
if(map.get(arr[i]) == 0) continue;
for (int j=arr[i]; j<arr[i]+5; j++) {
if (!map.containsKey(j)) return false;
if (map.get(j) == 0) return false;
else {
map.put(j, map.get(j)-1);
}
}
if (map.get(arr[i]) > 0) i--;
}
return true;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
boolean res = sol.determine(new int[]{1,2,3,4,4,5,5,5,6,6,7,7,8,8,9});
if (res) System.out.println("true");
else System.out.println("false");
} }

G面经prepare: Straight Partition of A Deck of Cards的更多相关文章

  1. 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 ...

  2. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  3. G面经prepare: X-Straight

    Define “X-Straight” as X cards with consecutive numbers (X >= 3). Determine if the deck can be fu ...

  4. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  5. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  6. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  7. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  8. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

  9. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

随机推荐

  1. Decoder with 3 Inputs and 2 3 = 8 Outputs

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION

  2. flat file

    Computer Science An Overview _J. Glenn Brookshear _11th Edition The term database refers to a collec ...

  3. Fingerprinting

    https://wiki.mozilla.org/Fingerprinting Fingerprinting   Contents 1 Overview 2 Data 2.1 Plugins 2.2 ...

  4. windows系统中ubuntu虚拟机安装及web项目到服务上(二)

    ajp方式整合apache2和tomcat 7 1:在apache2.conf配置文件中启用模块mod_proxy_ajp,在里面添加 LoadModule proxy_module modules/ ...

  5. nginx搭建http和rtmp协议的流媒体服务器

    nginx搭建http和rtmp协议的流媒体服务器 时间:2013-09-23 23:52来源:佚名 作者:本站 举报 点击:232次 实验目的:让Nginx支持flv和mp4格式文件,同时支持Rtm ...

  6. 【转】 Update和FixedUpdate的区别

    MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...

  7. Windows下mysql自动备份的最佳方案

    网上有很多关于window下Mysql自动备份的方法,其实不乏一些不好的地方和问题,现总结出一个最好的方法供大家参考: 新建一个记事本,然后重命名为: mysql_backup.bat 然后单击右键选 ...

  8. IIS绑定Active Directory账号自动登录网站的方法

    满足使用Request.ServerVariables["REMOTE_USER"]的条件: 1.IIS配置网站的目录安全性取消“启用匿名访问(&A)” 2.启用 “集成 ...

  9. 蓝牙BLE ATT剖析(二)-- PDU

    一.Error Handling Error Response The Error Responseis used to state that a given request cannot be pe ...

  10. vs2013 RTM 激活码

    BWG7X-J98B3-W34RT-33B3R-JVYW9