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. A BRIEF HISTORY OF COMPUTERS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Vacuum Tubes   Transi ...

  2. JS初学者必备的几个经典案例(二)!!!

    一.写出当前年份的前后5年的日期表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  3. C++ - 扩展欧几里德算法非递归实现

    #include <iostream> using namespace std; int x, y; void get_x_y(int a, int b){ int q, r[3], s[ ...

  4. C#中Attribute介绍

    什么是特性? MSDN中定义为:公共语言运行时运行添加类似关键字的描述声明,叫做Attribute,它对程序中的元素进行标注,如类型.方法.字段和属性等.attribute和Microsoft.Net ...

  5. shopping cart<代码>

    i = ["iphone 6000", "bicycle 1000", "coffee 50", "python book 100 ...

  6. 其他常用HTML 片段

    1.input placeholder 文字居中 字体大小+上下padding值等于设计稿宽度 设计稿中总高度为86px   padding:27px 0;font-size:30px;   2.英文 ...

  7. sublime text2 操作及插件

    sublime text2 1. 文件快速导航: 这是sublime上面很好用的功能之一,ctrl+p可以调出窗口,菜单上的解释是gotoanythings ,确实如其所言,调出窗口后,直接输入关键字 ...

  8. Swift-05-SizeOf&&SizeOfValue

    在C中定义的运算符,可以作用于类型或者某个实际的变量,并返回其在内存中的尺寸size_t. 在Cocoa中,我们一般用来将数组生成NSData的时候传入数据长度.因为在OC里面,sizeOf这个C运算 ...

  9. qt 控件 背景色 透明 除去边框

    在调试ui的时候,需要将背景色变为透明,与母控件的颜色一致,并且除去边框. 参考链接: http://www.qtcentre.org/threads/12148-how-QTextEdit-tran ...

  10. [代码片段]读取BMP文件(二)

    #include <stdio.h> #include <stdlib.h> #pragma pack(2) /*定义WORD为两个字节的类型*/ typedef unsign ...