题解 | #扑克牌顺子#
扑克牌顺子
https://www.nowcoder.com/practice/762836f4d43d43ca9deb273b3de8e1f4
import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Solution { public boolean IsContinuous(int [] numbers) { int zero = 0; Set<Integer> set = new HashSet<>(); for (int number : numbers) { // 不能超过 1-13 if (number < 0 || number > 13) { return false; } // 零的数量不能超过4 if (number == 0) { zero++; } else { set.add(number); } } // 零的数量不能超过4 if (zero > 4) { return false; } if(zero == 4){ return true; } // 排除重复 if (zero != numbers.length - set.size()) { return false; } Object[] array = set.toArray(); if (zero == 0) { for (int i = 1; i < array.length; i++) { if ((int) array[i] - (int) array[i - 1] != 1) { return false; } } } else if(zero == 1) { if ((int) array[set.size() - 1] - (int) array[0] == 4) { return true; } else { return false; } }else { if((int)array[0] < (int) array[set.size() - 1] - 4 ){ return false; } for (int i = 1; i < array.length; i++) { if ((int) array[i] - (int) array[i - 1] == 1) { return true; } } } return true; } }#java#