当前位置:网站首页>Leetcode simple question ring and rod
Leetcode simple question ring and rod
2022-07-05 22:03:00 【·Starry Sea】
subject
A total of n Annulus , The color of the ring can be red 、 green 、 One of the blue . These rings are distributed across 10 The root number is 0 To 9 On the rod .
Give you a length of 2n String rings , It means n The distribution of rings on the rod .rings Every two characters in the form of a The color position is right , Used to describe each ring :
The first i Right first The character represents the second i A ring Color (‘R’、‘G’、‘B’).
The first i Right the second The character represents the second i A ring Location , That is, on which rod (‘0’ To ‘9’).
for example ,“R3G2B1” Express : share n == 3 Annulus , The red ring is numbered 3 On the rod , The green ring is numbered 2 On the rod , The blue ring is numbered 1 On the rod .
Find all sets All three colors The rod of the ring , And return the number of such rods .
Example 1:
Input :rings = “B0B6G0R6R0R6G9”
Output :1
explain :
- Number 0 On the pole 3 Annulus , Gather all colors : red 、 green 、 blue .
- Number 6 On the pole 3 Annulus , But only red 、 Blue two colors .
- Number 9 There is only 1 A green ring .
therefore , The number of rods with all three color rings is 1 .
Example 2:
Input :rings = “B0R0G0R9R0B0G0”
Output :1
explain : - Number 0 On the pole 6 Annulus , Gather all colors : red 、 green 、 blue .
- Number 9 There is only 1 A red ring .
therefore , The number of rods with all three color rings is 1 .
Example 3:
Input :rings = “G4”
Output :0
explain :
Only one ring was given , therefore , There is no rod that gathers all three color rings .
Tips :
rings.length == 2 * n
1 <= n <= 100
Such as i yes even numbers , be rings[i] The value of can be taken as ‘R’、‘G’ or ‘B’( Subscript from 0 Start counting )
Such as i yes Odd number , be rings[i] The value of can be taken as ‘0’ To ‘9’ A number in ( Subscript from 0 Start counting )
source : Power button (LeetCode)
Their thinking
This problem only needs to count the colors on each pole , You can solve this problem by embedding a hash table in a dictionary .
class Solution:
def countPoints(self, rings: str) -> int:
d={
'0':[],'1':[],'2':[],'3':[],'4':[],'5':[],'6':[],'7':[],'8':[],'9':[]}
for i in range(len(rings)):
if rings[i].isdigit():
if rings[i-1] not in d[rings[i]]:
d[rings[i]].append(rings[i-1])
count=0
for i in d.values():
if len(i)==3:
count+=1
return count
边栏推荐
- Oracle views the data size of a table
- How to develop and introduce applet plug-ins
- Advantages of robot framework
- Yolov5 training custom data set (pycharm ultra detailed version)
- Oracle HugePages没有被使用导致服务器很卡的解决方法
- The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
- 科技云报道:算力网络,还需跨越几道坎?
- K210 learning notes (IV) k210 runs multiple models at the same time
- 华为快游戏调用登录接口失败,返回错误码 -1
- Serializability of concurrent scheduling
猜你喜欢
总结出现2xx、3xx、4xx、5xx状态码的原因
How to view Apache log4j 2 remote code execution vulnerability?
Oracle检查点队列–实例崩溃恢复原理剖析
Sentinel production environment practice (I)
Official clarification statement of Jihu company
How to use tensorflow2 for cat and dog classification and recognition
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Index optimization of performance tuning methodology
Type of fault
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
随机推荐
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Oracle HugePages没有被使用导致服务器很卡的解决方法
Livelocks and deadlocks of concurrency control
Shell script, awk uses if, for process control
K210 learning notes (IV) k210 runs multiple models at the same time
Codeforces 12D Ball 树形阵列模拟3排序元素
CA certificate trampled pit
Web3为互联网带来了哪些改变?
科技云报道:算力网络,还需跨越几道坎?
Installation of VMware Workstation
Type of fault
Multiplexing of Oracle control files
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Daily question brushing record (XIV)
元宇宙中的三大“派系”
MMAP学习
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Defect detection - Halcon surface scratch detection
总结出现2xx、3xx、4xx、5xx状态码的原因
854. String BFS with similarity K