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 -> 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的更多相关文章
- 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: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: X-Straight
Define “X-Straight” as X cards with consecutive numbers (X >= 3). Determine if the deck can be fu ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- 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 ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- 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 ...
随机推荐
- SQL查询(二)
常用查询技巧 1.获取数据的前3(n)行 ; 2.SQL语句中if语句 在SQL语句中没有直接的if语句,但是有两个函数:DECODE和CASE,他们能够实现if语句的功能 2.1)decode -- ...
- delimiter
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx The first command is ...
- Delphi格式化输出函数(1): Format
vars: string;begin//指令类型 types := Format('最大整数是: %d; 最小整数是: %d',[MaxInt,Low(Integer)]);//返回: 最大整数是: ...
- 发现了一个很好的Pasical编程软件Lazarus
下载地址:http://www.lazarus.freepascal.org/ 中文社区:http://www.fpccn.com/ 该软件有几点如下特征: 1.跨平台.体积小(只有100多M) 2. ...
- synchronized的使用方法
[转自] http://blog.csdn.net/witsmakemen/article/details/6966116 记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的 ...
- VC 中 UpdateData() 函数的使用
UpdateData(FALSE)与UpdateData(TRUE)是相反的过程 UpdateData(FALSE)是把程序中改变的值更新到控件中去 UpdateData(TRUE)是把在控件中输入的 ...
- ASP.NET WebForm与ASP.NET MVC的不同点
ASP.NET WebForm ASP.NET MVC ASP.NET Web Form 遵循传统的事件驱动开发模型 ASP.NET MVC是轻量级的遵循MVC模式的请求处理响应的基本开发模型 ASP ...
- 用正则验证字符串格式,形如:A)XXX B)XXXX C)XXX
今天遇到个小功能,要验证某个英文选项是否正确,例如:A)accumulate B)circling C)communities D)competition E)domestic F)financi ...
- SDP协议译稿(Part 1)
本文的翻译内容是基于Bluetooth Core Spec 2.1+EDR 协议中对SDP的描述,很多都是个人的理解,难免有疏漏,有争议或者疑问的地方,欢迎在此留言进行探讨. 2. Overview ...
- ios 消息传递机制
引用文章 一.KVO 1.当对象中的某个属性值发生了改变,可以对这些值的观察者做出通知. 2.接受者(会接收到值发生改变的消息) 必须知道发送者(值将发生改变的那个对象). 3.接收者同样还需要知道发 ...