package com.patience.interview.algorithms;
import java.util.*;
/**
* @author Green.Gee
* @date 2022/3/4 18:59
* @email green.gee.lu@gmail.com
*/
public class SolveQ2 {
/***
* 算法 扑克牌四种花色,1-13为一种,14-26为一种,27-39为一种,40-52为一种,
* 判断一组数据是否有同花顺子————同一花色三张,比如 234
* 比如[2,3,4,10,5,14] 中 234 返回 true
*/
public static void main(String[] args) {
int [] nums = new int[]{2,3,4,10,5,14};
List<Integer> black = new ArrayList<>();
List<Integer> red = new ArrayList<>();
List<Integer> flower = new ArrayList<>();
List<Integer> chip = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if(nums[i] <= 13)
black.add(nums[i]);
else if(nums[i] <= 26)
red.add(nums[i]);
else if(nums[i] <= 39)
flower.add(nums[i]);
else if(nums[i] <= 52)
chip.add(nums[i]);
}
boolean one = check(black);
if(!one)
one = check(red);
if(!one)
one = check(flower);
if(!one)
one = check(chip);
System.out.printf("同花顺:%s\n",one);
}
public static boolean check(List<Integer> set){
if(set.size() == 13) return true;
Collections.sort(set);
for (int i = 0; i < set.size(); i++) {
// 13 边界
boolean one = set.get(i) % 13 != 0;
boolean two = (set.get(i) + 1) % 13 != 0;
if(one && two ){
// i 边界
boolean $3 = i + 1 <= set.size() - 1;
boolean $4 = i + 2 <= set.size() - 1;
if($3 && $4){
boolean $1 = (set.get(i) + 1) == set.get(i + 1);
boolean $2 = (set.get(i) + 2 == set.get(i + 2));
if($1 && $2)
return true;
}
}
}
return false;
}
}
判断一组数据是否有同花顺子————同一花色三张【234】
于 2022-03-04 20:26:04 首次发布