題目大意:打牌,判斷牌是否能所有按順子出code
思路:構造一個list,存儲1,2,3,4,5,6,7,8並排序,構造一個map存儲每一個數對應出現的次數排序
Java實現:ip
public boolean isNStraightHand(int[] hand, int W) { List<Integer> nums = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<>(); // num, count for (int tmp : hand) { Integer count = map.get(tmp); if (count == null) { count = 0; nums.add(tmp); } map.put(tmp, count+1); } Collections.sort(nums); // sort int i=0; while (i < nums.size()) { int tmp = nums.get(i); int offset = 0; while (offset < W) { Integer count = map.get(tmp + offset); if (count == null || count < 1) return false; map.put(tmp + offset, count-1); offset++; } while (i < nums.size() && map.get(nums.get(i)) == 0) i++; } return true; }