break;
case 'G':
counter[index] |= 2;
break;
default:
counter[index] |= 4;
break;
}
}
for (int c : counter) {
if (c == 7) {
++ans;
}
}
return ans;
}
}
---
### c
int countPoints(char * rings){
int ans = 0;
int counter[10] = {0};
while (\*rings) {
char color = \*rings;
++rings;
int index = \*rings - '0';
++rings;
switch (color) {
case 'R':
counter[index] |= 1;
break;
case 'G':
counter[index] |= 2;
break;
default:
counter[index] |= 4;
break;
}
}
for (int i = 0; i < 10; ++i) {
if (counter[i] == 7) {
++ans;
}
}
return ans;
}
---
### c++
class Solution {
public:
int countPoints(string rings) {
int ans = 0;
int n = rings.size();
int counter[10] = {0};
for (int i = 0; i < n; i += 2) {
char color = rings[i];
int index = rings[i + 1] - '0';
switch (color) {
case 'R':
counter[index] |= 1;
break;
case 'G':
counter[index] |= 2;
break;
default:
counter[index] |= 4;
break;
}
}
for (int n: counter) {
if (n == 7) {
++ans;
}
}
return ans;
}
};
---
### python
class Solution:
def countPoints(self, rings: str) -> int:
ans = 0
counter = [0] * 10
for i in range(0, len(rings), 2):
c = rings[i]
idx = int(rings[i + 1])
if c == ‘R’:
counter[idx] |= 1
elif c == ‘G’:
counter[idx] |= 2
else:
counter[idx] |= 4
for n in counter:
if n == 7:
ans += 1
return ans
---
### go
func countPoints(rings string) int {
ans := 0
counter := make([]int, 10)
n := len(rings)
for i := 0; i < n; i += 2 {
c := rings[i]
idx := rings[i+1] - '0'
switch c {
case 'R':
counter[idx] |= 1
case 'G':
counter[idx] |= 2
default:
counter[idx] |= 4
}
}
for \_, n := range counter {
if n == 7 {
ans++
}
}
return ans
}
---
### rust
impl Solution {
pub fn count_points(rings: String) -> i32 {
let mut counter = vec![0; 10];
rings.as_bytes().chunks(2).for_each(|c| {
let color = match c[0] as char {
‘R’ => 1,
‘G’ => 2,
_ => 4
};
let index = (c[1] - ‘0’ as u8) as usize;
counter[index] |= color;
});
counter.iter().filter(|n| {
\*\*n == 7
}).count() as i32
}
}
---
![在这里插入图片描述](https://img-blog.csdnimg.cn/3fc8cae06a934243836c633a48777386.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqM5b2T5a6255qE55m95bi95a2Q,size_20,color_FFFFFF,t_70,g_se,x_16)
---
## [原题传送门:https://leetcode-cn.com/problems/rings-and-rods/]( )
---
>
> 非常感谢你阅读本文~
> 欢迎【👍点赞】【⭐收藏】【📝评论】~
> 放弃不难,但坚持一定很酷~
> 希望我们大家都能每天进步一点点~
> 本文由 [二当家的白帽子:https://le-yi.blog.csdn.net/]( ) 博客原创~
>
>
>
---
![img](https://img-blog.csdnimg.cn/img_convert/5067b4f622595e4584e1eb38fc8b4af8.png)
![img](https://img-blog.csdnimg.cn/img_convert/33c8ad2266cab0b5c41a7ec66f7b216e.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
客原创~
>
>
>
---
[外链图片转存中...(img-qwXelFVa-1725747659683)]
[外链图片转存中...(img-orlkjESW-1725747659684)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0