LeetCode 2103.环和杆

原题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

C++代码

常规

cpp 复制代码
class Solution {
public:
    int countPoints(string rings) {
        int count = 0;
        set<string>se[10];
        for(int i=1,j=0;i<rings.length();i+=2,j+=2){
            se[rings[i]-'0'].insert(to_string(rings[j]));
        }

        for(int i=0;i<10;i++){
            if(se[i].size()==3) count++;  
        }
        
        return count;
    }
};

Python代码

位运算

python 复制代码
class Solution:
    def countPoints(self, rings: str) -> int:
        n,count = len(rings),0
        binary_map = [0]*17  # R - B = 17
        for i in range(0,n,2):
            binary_map[ord(rings[i]) - ord('B')] |= 1 << (int(rings[i+1]) - int('0'))  
        for i in range(10):
            tmp = 0
            for ch in ['R','G','B']:
                tmp += (binary_map[ord(ch)-ord('B')] >> i) & 1  #第i根杆 上是否有ch颜色的圈
            if tmp == 3:
                count += 1
        return count
相关推荐
地平线开发者10 小时前
征程 6X 常见 kernel panic 问题
算法·自动驾驶
com_4sapi11 小时前
2025 权威认证头部矩阵系统全景对比发布 双榜单交叉验证
大数据·c语言·人工智能·算法·矩阵·机器人
前端小L11 小时前
二分查找专题(九):“降维”的魔术!将二维矩阵“拉平”为一维
数据结构·算法
Jasmine_llq11 小时前
《P7516 [省选联考 2021 A/B 卷] 图函数》
算法·弗洛伊德算法·floydwarshall算法·后缀和计算
kaikaile199511 小时前
三维CT图像重建算法
算法
她说人狗殊途11 小时前
时间复杂度(按增长速度从低到高排序)包括以下几类,用于描述算法执行时间随输入规模 n 增长的变化趋势:
数据结构·算法·排序算法
计科土狗11 小时前
算法基础入门第一章
c++·算法
与己斗其乐无穷11 小时前
算法(二)滑动窗口
算法
Ghost-Face11 小时前
恭喜自己,挑战成功!
算法
Miraitowa_cheems11 小时前
LeetCode算法日记 - Day 102: 不相交的线
数据结构·算法·leetcode·深度优先·动态规划