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
相关推荐
凌肖战1 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
weixin_478689762 小时前
十大排序算法汇总
java·算法·排序算法
luofeiju2 小时前
使用LU分解求解线性方程组
线性代数·算法
SKYDROID云卓小助手3 小时前
无人设备遥控器之自动调整编码技术篇
人工智能·嵌入式硬件·算法·自动化·信号处理
ysa0510303 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
GEEK零零七3 小时前
Leetcode 1103. 分糖果 II
数学·算法·leetcode·等差数列
今天背单词了吗9803 小时前
算法学习笔记:7.Dijkstra 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·数据结构·笔记·算法
重庆小透明5 小时前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
desssq5 小时前
力扣:70. 爬楼梯
算法·leetcode·职场和发展
clock的时钟6 小时前
暑期数据结构第一天
数据结构·算法