Draw 5 cards at random from playing CARDS to see if it is a coincidence, that is, if the 5 CARDS are consecutive.2 ~ 10 is the number itself, A is 1, J is 11, Q is 12, K is 13, and xiao wang and xiao wang are 0, which can be regarded as any number.A can’t be viewed as 14.
Example1:
Input: [1,2,3,4,5]
Output: True
Example2
Input: [0,0,1,2,5]
Output: True
Ideas of solving a problem
When we solve this problem, we should think about how many jokers there are.I’m going to sort the cards, sort them in ascending order.Find out how many jokers there are.Because they don’t give you the key information about how many cards there are or how many jokers there are. So we analyze different Numbers of trumps to see if the deck is a good one. The time complexity is O(n), and the space complexity is O(1). The memory consumption is 2.1Mb.
Code
package main
import (
"fmt"
"sort"
)
func main(){
arr := [] int {0,0,1,0,0}
fmt.Println(isStraight(arr))
}
func isStraight(nums []int) bool {
sort.Ints(nums)
count := 0
for i := 0; i < 5; i++ {
if nums[i] == 0 {
count = count + 1
}
}
if count == 5|4 {
return true
}
if count == 3 {
for i := 4; i < len(nums); i++ {
if nums[i]-nums[i-1] > 4 || nums[i] == nums[i-1] {
return false
}
}
}
if count == 2 {
for i := 3; i < len(nums); i++ {
if nums[i]-nums[i-1] > 3 || nums[i] == nums[i-1] {
return false
}
}
}
// 0 1 2 5 6
if count == 1 {
for i := 2; i < len(nums); i++ {
if nums[i]-nums[i-1] > 2 || nums[i] == nums[i-1] {
return false
}
}
}
if count == 0 {
for i := 1; i < len(nums); i++ {
if nums[i]-nums[i-1] > 1 || nums[i] == nums[i-1] {
return false
}
}
}
return true
}
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof