当前位置:网站首页>[leetcode] sword finger offer 61. shunzi in playing cards
[leetcode] sword finger offer 61. shunzi in playing cards
2022-07-24 02:31:00 【Spring-_- Bear】
Draw at random from several sets of playing cards 5 card , Judge whether it's a down son , Is this 5 Are cards continuous .2~10 For the number itself ,A by 1,J by 11,Q by 12,K by 13, And big 、 Wang Wei 0 , It can be seen as any number .A Can't be regarded as 14.
Example 1:
Input : [1,2,3,4,5]
Output : True
Example 2:
Input : [0,0,1,2,5]
Output : True
Answer 1 :
/** * The finger of the sword Offer 61. Shunzi in playing cards */
public boolean isStraight(int[] nums) {
/* * 5 Cards should form shunzi , There can only be two duplicates at most 0, The remaining figures cannot be repeated , Return directly when repeating false * because 0 Can be used as any number of cards , So discuss the situation to clarify the thinking : * 1. Zero 0: The maximum number of points when forming a shunzi - Minimum points === 4 * 2. One 0: Such as 0,5,7,8,9, because 0 Can be viewed as 6, Then the maximum number of points (9) - Minimum points (5) = 4 * 3. Two 0: Such as 0,0,6,7,8, because 0 Can be viewed as 4,5 or 9,10, At this time, the maximum number of points (8) - Minimum points (6) = 2 * summary : When the maximum number of cards - Minimum points <= 4 Time can form shunzi */
Set<Integer> set = new HashSet<>();
int max = 0;
int min = 14;
for (int num : nums) {
// If it is 0 Then continue to traverse , Find the maximum number of points and the minimum number of points
if (0 == num) {
continue;
}
// Determine whether the current element is repeated , Return directly if repeated false
if (!set.add(num)) {
return false;
}
max = Math.max(max, num);
min = Math.min(min, num);
}
return max - min <= 4;
}

Explanation 2 :
/** * The finger of the sword Offer 61. Shunzi in playing cards */
public boolean isStraight(int[] nums) {
/* * Sort the array first , Count the number of size kings in the process of traversing the array , * And judge the Division 0 Whether there are duplicate points outside , Return directly if it exists false * notes : After sorting the array , The maximum number of points must be nums[4], The minimum number of points must be nums[ Number of king and King ] * 1. When there is no king of size, the minimum number of points must be nums[0], Such as 1,2,3,4,5 * 2. When there is a king of size, the minimum number of points must be nums[1], Such as 0,1,2,4,5( because 0 Can act as any point , Then it must act as a number larger than the minimum number of points in the current array ) * 3. The minimum number of points must be nums[2], Such as 0,0,2,4,5( because 0 Can act as any point , Then it must act as a number larger than the minimum number of points in the current array ) */
Arrays.sort(nums);
int joker = 0;
for (int i = 0; i < 4; i++) {
// Count the number of kings and kings
if (nums[i] == 0) {
joker++;
}
// There are duplicate points
else if (nums[i + 1] == nums[i]) {
return false;
}
}
// The difference between the maximum number of points and the minimum number of points is less than or equal to 4 And shunzi
return nums[4] - nums[joker] <= 4;
}
Limit :
- The array length is 5
- The value of the array is [0, 13] .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/bu-ke-pai-zhong-de-shun-zi-lcof
边栏推荐
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- pbootcms模板调用标签序数从2开始或者自动数开始
- Mysql数据库,排序与单行处理函数篇
- Qml- use listview to build a three-level treeview architecture
- Idea's gradle project Chinese garbled
- [datasets] - downloading some datasets of flyingthings3d optical flow
- Leetcode exercise -- two questions about the nearest common ancestor of binary trees
- [diary of supplementary questions] [2022 Hangdian summer school 1] c-backpack
- Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power
- Leetcode 70 climbing stairs, 199 right view of binary tree, 232 realizing queue with stack, 143 rearranging linked list
猜你喜欢

Chinese scientists have made new progress in high security quantum key distribution networks

Tensorflow 2.0 deep learning tutorial

Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series

组件el-scrollbar的使用

Summary of the first change to open source middleware keycloak

响应式布局一个网页在不同设备显示不同效果)meta:vp
![[datasets] - downloading some datasets of flyingthings3d optical flow](/img/00/5d87b378ebab49e9dc400d8e3634f3.png)
[datasets] - downloading some datasets of flyingthings3d optical flow

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team

MySQL---four JDBC

1000 okaleido tiger launched binance NFT, triggering a rush to buy
随机推荐
Running around, market and quantitative page function optimization! Stock quantitative analysis tool qtyx-v2.4.5
微信小程序实现折线面积图-玫瑰图-立体柱状图
Qml- use listview to build a three-level treeview architecture
Canvas drawing (mouse click to draw and lift to end)
Rylstim Screen Recorder
Halide:: generator instructions
How to judge null for different types of fields, sets, lists / sets / maps, and objects
Go基础笔记_5_数组切片
Reconnaître le Protocole de couche de transport - TCP / UDP
Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power
[Luogu] p1972 HH Necklace
【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
How to do a good job of accompanying translation
Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
STM32 installation tutorial and j-link burning driver installation tutorial [the next day]
2022.7.22 JS entry common data types and methods
BPG笔记(三)
Codeworks 5 questions per day (average 1500) - day 23
Share an API Gateway project based on ABP and yarp