剑指 Offer 61. 扑克牌中的顺子
作者:互联网
从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
示例 1:
输入: [1,2,3,4,5] 输出: True
示例 2:
输入: [0,0,1,2,5] 输出: True
限制:
数组长度为 5
数组的数取值为 [0, 13] .
package SolutionOffer61;
class Solution {
public boolean isStraight(int[] nums) {
int[] count = new int[14];
for (int i = 0; i < nums.length; i++) {
count[nums[i]]++;
}
for (int i = 0; i < count.length; i++) {
if (count[i] > 1 && i != 0) {
return false;
}
}
int start = 0;
int end = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] != 0 && i != 0) {
start = i;
break;
}
}
for (int i = count.length - 1; i > -1; i--) {
if (count[i] != 0) {
end = i;
break;
}
}
int numcount = end - start + 1;
// System.out.println(start);
// System.out.println(end);
// System.out.println(numcount);
// System.out.println(nums.length);
if (numcount <= nums.length) {
return true;
}
return false;
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] nums = { 0, 0, 1, 2, 5 };
System.out.println(sol.isStraight(nums));
}
}
标签:count,end,Offer,int,System,61,length,顺子,out 来源: https://blog.csdn.net/allway2/article/details/115558117