Solution of LCOF

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值