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
相关推荐
沙威玛_LHE2 小时前
树和二叉树
数据结构·算法
py有趣4 小时前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗4 小时前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
吃着火锅x唱着歌6 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
十八岁讨厌编程6 小时前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
橘颂TA6 小时前
【剑斩OFFER】算法的暴力美学——最小覆盖字串
算法·c/c++·就业
wearegogog1236 小时前
基于混合蛙跳算法和漏桶算法的无线传感器网络拥塞控制与分簇新方法
网络·算法
Tiandaren7 小时前
大模型应用03 || 函数调用 Function Calling || 概念、思想、流程
人工智能·算法·microsoft·数据分析
2301_795167208 小时前
玩转Rust高级应用 如何进行理解Refutability(可反驳性): 模式是否会匹配失效
开发语言·算法·rust
小当家.1058 小时前
[LeetCode]Hot100系列.贪心总结+思想总结
算法·leetcode·职场和发展