2103. 环和杆

2103. 环和杆

解法一: 位运算

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func countPoints(rings string) int {
flag := 0
length := len(rings)
for i := 1; i < length; i += 2 {
shift := 0
if rings[i - 1] == 'R' {
shift = 1
} else if rings[i - 1] == 'G' {
shift = 1 << 1
} else if rings[i - 1] == 'B' {
shift = 1 << 2
}
flag |= shift << (int(rings[i] - '0') * 3)
}
ans := 0
for flag != 0 {
if (flag & 7) == 7 {
ans++
}
flag >>= 3
}
return ans
}
作者

wuhunyu

发布于

2023-11-02

更新于

2023-11-02

许可协议