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
相关推荐
参.商.4 分钟前
【Day49】236.二叉树的最近公共祖先
leetcode·golang
B325帅猫-量子前沿技术研究所17 分钟前
PSD和FFT的关系
人工智能·算法
闻缺陷则喜何志丹18 分钟前
【排序】P6149 [USACO20FEB] Triangles S|普及+
c++·算法·排序·洛谷
avocado_green25 分钟前
【LeetCode】90. 子集 II
算法·leetcode
tankeven30 分钟前
HJ178 【模板】双指针
c++·算法
君义_noip40 分钟前
信息学奥赛一本通 4131:【GESP2506六级】学习小组 | 洛谷 P13015 [GESP202506 六级] 学习小组
算法·动态规划·gesp·信息学奥赛
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 300. 最长递增子序列 | C++ 动态规划 & 贪心二分
c++·leetcode·动态规划
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 72. 编辑距离 | C++ 经典 DP 增删改状态转移
c++·算法·leetcode
穿条秋裤到处跑1 小时前
每日一道leetcode(2026.04.16):距离最小相等元素查询
算法·leetcode·职场和发展
XY_墨莲伊2 小时前
【实战项目】基于B/S结构Flask+Folium技术的出租车轨迹可视化分析系统(文末含完整源代码)
开发语言·后端·python·算法·机器学习·flask