This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

The topic of dry

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

Example 1:

Input: [1,2,3,4,5] output: TrueCopy the code

Example 2:

Input: [0,0,1,2,5] output: TrueCopy the code

Source: LeetCode link: leetcode-cn.com/problems/bu… Copyright belongs to collar network. Commercial reprint please contact the official authorization, non-commercial reprint please indicate the source.

Solution: Max – min < 5

This problem is actually a skill problem, if you don’t find this pattern before, this problem is actually easy to get stuck in the problem, I just started like this, always fail the use case, and then read the analysis, show me directly, oneself laugh

Because our final structure is an ordered array, so we don’t have to consider the length of the array, actually only need to consider the impact of the maximum and minimum values, because the problem is given in the 0 to replace any number, if we meet, the relationship between the maximum and the minimum can prove this array is a continuous array.

So can I just to sort an array, the size of the location of the wang in front, just note down the number of big wang, because the first number than the size of wanda is our minimum value, also is our nums [number] big wang, you also need to a judgment is when we non-zero element, element is equal before and after can be directly returns false.

Look at the code:

 / * * *@param {number[]} nums
  * @return {boolean}* /
 var isStraight = function (nums) {
   / / sorting
     nums.sort((a, b) = > {
         return a - b
     })
     let count = 0
     for (let i = 0; i < 4; i++) {
         if (nums[i] == 0) {
           // If the element is 0, count+
             count++
         } else if (nums[i] == nums[i + 1]) {
           // If the element is not 0, the elements are equal to each other
             return false}}return nums[4] - nums[count] < 5
 };
Copy the code

This is done using sort + traversal