题目
代码
执行用时:40 ms, 在所有 Python3 提交中击败了12.02% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了91.17% 的用户
通过测试用例:66 / 66
class Solution:
def countPoints(self, rings: str) -> int:
cnt=dict()
idx=0
lgth=len(rings)
while idx<lgth:
cnt.setdefault(rings[idx+1],set())
cnt[rings[idx+1]].add(rings[idx])
idx+=2
ans=0
for key in cnt:
ans+=(len(cnt[key])==3)
return ans
【写法1】
执行用时:36 ms, 在所有 Python3 提交中击败了38.13% 的用户
内存消耗:15 MB, 在所有 Python3 提交中击败了56.55% 的用户
通过测试用例:66 / 66
class Solution:
def countPoints(self, rings: str) -> int:
cnt=dict()
idx=0
lgth=len(rings)
while idx<lgth:
cnt.setdefault(rings[idx+1],set())
cnt[rings[idx+1]].add(rings[idx])
idx+=2
return sum([len(cnt[key])==3 for key in cnt])
【方法2】使用位运算,记录每个位置的状态位,R=1 G=2 B=7
执行用时:32 ms, 在所有 Python3 提交中击败了69.94% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了80.29% 的用户
通过测试用例:66 / 66
class Solution:
def countPoints(self, rings: str) -> int:
status=[0]*10
idx=0
cnt={"R":1,"G":2,"B":4}
while idx<len(rings):
status[int(rings[idx+1])]|=cnt[rings[idx]]
idx+=2
return sum([item==7 for item in status])