-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61_continuous_test.go
More file actions
65 lines (60 loc) · 1.42 KB
/
61_continuous_test.go
File metadata and controls
65 lines (60 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package jzoffer
import (
"testing"
)
// 面试题61:扑克牌的顺子
// 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。
// 2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王可以看成任意数字。
// 桶排序,0表示大小王
func isContinuous(data []int) bool {
buf := make([]bool, 14)
cnt0 := 0
for _, v := range data {
if v == 0 {
cnt0++
} else {
if buf[v] == true {
return false //有重牌
}
buf[v] = true
}
}
// 找最小值
i := 1
for ; i < 14 && buf[i] == false; i++ {
}
for j := i; j < i+5 && j < 14; j++ {
if buf[j] == false {
cnt0--
}
if cnt0 < 0 {
return false
}
}
return true
}
func Test_IsContinuous(t *testing.T) {
type testType struct {
d []int
f bool
}
testCase := []testType{
testType{[]int{1, 3, 2, 5, 4}, true},
testType{[]int{1, 3, 2, 6, 4}, false},
testType{[]int{0, 3, 2, 6, 4}, true},
testType{[]int{0, 3, 1, 6, 4}, false},
testType{[]int{1, 3, 0, 5, 0}, true},
testType{[]int{1, 3, 0, 7, 0}, false},
testType{[]int{1, 0, 0, 5, 0}, true},
testType{[]int{1, 0, 0, 7, 0}, false},
testType{[]int{3, 0, 0, 0, 0}, true},
testType{[]int{0, 0, 0, 0, 0}, true},
testType{[]int{1, 0, 0, 1, 0}, false},
}
for _, test := range testCase {
ans := isContinuous(test.d)
if test.f != ans {
t.Error("测试失败,测试集", test, "实际输出", ans)
}
}
}