Return to school: the third question-the sword refers to the Offer 61. Straight in the poker

Sword refers to Offer 61. Straight in poker

Randomly draw 5 cards from the playing cards to determine if it is a straight, that is, if the 5 cards are consecutive. 2 to 10 are the numbers themselves, A is 1, J is 11, Q is 12, K is 13, and the big and small kings are 0, which can be regarded as any number. A cannot be regarded as 14.

示例 1:

输入: [1,2,3,4,5]
输出: True
 

示例 2:

输入: [0,0,1,2,5]
输出: True
 

限制:

数组长度为 5 

数组的数取值为 [0, 13] .

answer:

The main point of this question lies in the existence of the omnipotent "0" , which mainly lies in the application of mathematical ideas . First, we use the hash table idea to remove duplicate numbers, and after sorting the five cards, we then analyze:

If there are no zeros in the five cards drawn, then this is the simplest case one . We only need to verify whether the five numbers are consecutive. Since there are no repeated numbers at this time, if the last number minus the first number is 4, then it must be a straight at this time, because the middle three pits, three different numbers, so it must be true.

Next is case 2 , that is, there is a 0 case. At this time, if the largest minus the smallest of the remaining four numbers is 4, it is the same as the situation. At this time , the number used by 0 must not be the largest or smallest number , because the distance is not enough, so this Time is three pits, two known numbers and a 0, 0 can serve as any number, so it must be able to serve as the number corresponding to the remaining pit after the three pits are filled with two known numbers;
if The largest minus the smallest of the remaining four numbers is 3, then the situation is similar to "2, 3, 4, 5", that is, 0 acts as the smallest or largest, so the pit is determined, and the number is also Yes, it is true; the largest minus the smallest of the remaining four numbers is the other, because the distance is too large, so it must be impossible to form a straight.

Next is case 3 , that is, two 0s. At this time, the analysis process is similar to that of case 2. When the maximum reduction of the three numbers other than 0 is 4, the number of pits has been fixed and just enough for the remaining numbers;
when the result is The same is true for 3 and 2 hours. It can always keep the pits continuous and sufficient. When it is for other things, it is found that there are too many pits, and the number is not filled enough, so it does not hold.
So when 0 is 4, 5, 6... and so on, the same is true.

Code:

int cmp(int*x,int*y)
{
    
    
    return *x>*y;
}
bool isStraight(int* nums, int numsSize){
    
    
    qsort(nums,numsSize,sizeof(int),cmp);
    int zero = 0;
    int hash[14]={
    
    0};
    for(int i=0;i<numsSize;i++)
    {
    
    
        hash[nums[i]]++;
    }
    for(int i=1;i<14;i++)
    {
    
    
        if(hash[i]>=2)//如果有重复元素
        {
    
    
            return 0;
        }
    }
    for(int i=0;i<numsSize;i++)
    {
    
    
        if(nums[i]==0)
        {
    
    
            zero++;
        }
    }
    if(nums[4]-nums[0]==4)
    {
    
    
        return 1;
    }
    else
    {
    
    
        if(zero==0)
        {
    
    
            return 0;
        }
        else if(zero==1)
        {
    
    
            if(nums[4]-nums[1]==3||nums[4]-nums[1]==4)
            {
    
    
                return 1;
            }
            else
            {
    
    
                return 0;
            }
        }
        else if(zero==2)
        {
    
    
            if(nums[4]-nums[2]==2||nums[4]-nums[2]==2+1||nums[4]-nums[2]==4)
            {
    
    
                return 1;
            }
            else
            {
    
    
                return 0;
            }
        }
        else
        {
    
    
            if(nums[4]-nums[3]==1||nums[4]-nums[3]==2||nums[4]-nums[3]==3||nums[4]-nums[3]==4)
            {
    
    
                return 1;
            }
            else
            {
    
    
                return 0;
            }
        }
    }
    return 1;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/114379835