poker straight
Topic description
LL is in a very good mood today, because he went to buy a deck of playing cards and found that there were 2 big kings and 2 little kings in it (a deck of cards was originally 54 cards ^_^)... He randomly picked 5 cards out of them He wanted to test his luck and see if he could draw a straight. If he did, he decided to buy a sports lottery ticket, hehe! ! "Ace of Hearts, 3 of Spades, Xiao Wang, Da Wang, 5 of Diamonds", "Oh My God!" is not a straight... LL is not happy, he thought about it,
- It is decided that the big\little king can be regarded as any number, and A is regarded as 1, J is 11, Q is 12, and K is 13. The 5 cards above can become "1, 2, 3, 4, 5" (the big and small kings are regarded as 2 and 4 respectively), "So Lucky!".
- LL decided to buy a sports lottery ticket. Now, you are asked to use this card to simulate the above process, and then tell us how lucky the LL is, output true if the card makes a straight, and false otherwise.
- For convenience, you can consider the big and small kings to be 0.
Title link : Poker Straight
code
import java.util.Arrays;
/**
* 标题:扑克牌顺子
* 题目描述
* LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,
* 看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,
* 决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。
* LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。
* 为了方便起见,你可以认为大小王是0。
* 题目链接:
* https://www.nowcoder.com/practice/762836f4d43d43ca9deb273b3de8e1f4?tpId=13&&tqId=11198&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
public class Jz45 {
public static void main(String[] args) {
Jz45 jz45 = new Jz45();
int[] numbers = new int[]{3, 4, 5, 1, 6};
System.out.println(jz45.isContinuous(numbers));
}
public boolean isContinuous(int[] numbers) {
if (numbers.length < 5) {
return false;
}
Arrays.sort(numbers);
// 统计癞子数量
int cnt = 0;
for (int num : numbers) {
if (num == 0) {
cnt++;
}
}
// 使用癞子去补全不连续的顺子
for (int i = cnt; i < numbers.length - 1; i++) {
if (numbers[i + 1] == numbers[i]) {
return false;
}
cnt -= numbers[i + 1] - numbers[i] - 1;
}
return cnt >= 0;
}
}
[Daily Message] Flowers in the world will bloom, and beautiful things will surely follow.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。