当前位置:网站首页>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

边栏推荐
- Evolution of large website architecture and knowledge system
- Robot operation mechanism
- Cross end solutions to improve development efficiency
- matlab绘制hsv色轮图
- HYSBZ 2243 染色 (树链拆分)
- EBS Oracle 11g cloning steps (single node)
- MMAP
- PyGame practical project: write Snake games with 300 lines of code
- Granularity of blocking of concurrency control
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
猜你喜欢

Lightweight dynamic monitorable thread pool based on configuration center - dynamictp

Drawing HSV color wheel with MATLAB

Granularity of blocking of concurrency control

database mirroring

matlab绘制hsv色轮图

EBS Oracle 11g cloning steps (single node)

How to use tensorflow2 for cat and dog classification and recognition

MMAP学习

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

Oracle checkpoint queue - Analysis of the principle of instance crash recovery
随机推荐
Type of fault
Installation of VMware Workstation
让开发效率提升的跨端方案
大约SQL现场“这包括”与“包括在”字符串的写法
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Analysis and test of ModbusRTU communication protocol
Defect detection - Halcon surface scratch detection
Two stage locking protocol for concurrency control
Index optimization of performance tuning methodology
如何向mongoDB中添加新的字段附代码(全)
C language knowledge points link
POJ 3237 tree (tree chain splitting)
Database recovery strategy
matlab绘制hsv色轮图
Basic grammar of interview (Part 1)
How to view Apache log4j 2 remote code execution vulnerability?
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Livelocks and deadlocks of concurrency control
How can Huawei online match improve the success rate of player matching
[Yugong series] go teaching course 003-ide installation and basic use in July 2022