当前位置: 首页 > news >正文

LeetCode每日一题——2103. Rings and Rods

文章目录

    • 一、题目
    • 二、题解

一、题目

There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.

You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:

The first character of the ith pair denotes the ith ring’s color (‘R’, ‘G’, ‘B’).
The second character of the ith pair denotes the rod that the ith ring is placed on (‘0’ to ‘9’).
For example, “R3G2B1” describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.

Return the number of rods that have all three colors of rings on them.

Example 1:

Input: rings = “B0B6G0R6R0R6G9”
Output: 1
Explanation:

  • The rod labeled 0 holds 3 rings with all colors: red, green, and blue.
  • The rod labeled 6 holds 3 rings, but it only has red and blue.
  • The rod labeled 9 holds only a green ring.
    Thus, the number of rods with all three colors is 1.
    Example 2:

Input: rings = “B0R0G0R9R0B0G0”
Output: 1
Explanation:

  • The rod labeled 0 holds 6 rings with all colors: red, green, and blue.
  • The rod labeled 9 holds only a red ring.
    Thus, the number of rods with all three colors is 1.
    Example 3:

Input: rings = “G4”
Output: 0
Explanation:
Only one ring is given. Thus, no rods have all three colors.

Constraints:

rings.length == 2 * n
1 <= n <= 100
rings[i] where i is even is either ‘R’, ‘G’, or ‘B’ (0-indexed).
rings[i] where i is odd is a digit from ‘0’ to ‘9’ (0-indexed).

二、题解

class Solution {
public:int countPoints(string rings) {int n = rings.size();unordered_map<int,vector<char>> map;char c;for(int i = 0;i < n;i++){if(i % 2 == 0) c = rings[i];else{int index = rings[i] - '0';if(map[index].size() == 0) map[index].push_back(c);else{bool exist = false;for(int j = 0;j < map[index].size();j++){if(map[index][j] == c) exist = true;}if(!exist) map[index].push_back(c);}}}int res = 0;for(int i = 0;i <= n / 2;i++){if(map[i].size() == 3) res++;}return res;}
};
http://www.lryc.cn/news/216202.html

相关文章:

  • ant-design-vue form表单自定义校验规则
  • 软件工程:小组开发过程技术(VS VSS UNIX C++)
  • 算法----从字符串中移除星号
  • JVS低代码表单引擎助你打造高效表单设计流程
  • 运行项目报错error in ./node_modules/marked/lib/marked.umd.js
  • 内置对象和方法、前端基础之BOM和DOM
  • 内网穿透配置-Cpolar-Ngrok
  • Web前端自动化测试Cypress实践总结
  • Nacos本地修改编译源码2.2.3
  • 邦芒攻略:提升职场核心竞争力的7点建议
  • Android 如何在Android studio中快速创建raw和assets文件夹
  • 功率放大器功能及用途介绍
  • 11.Linux系统:定时任务备份mysql数据库为文件并传输到其他服务器
  • 基于Python的豆瓣电影排行榜,可视化系统
  • Flink日志采集-ELK可视化实现
  • iOS NSKeyedUnarchiver归档和读取
  • 算法通关村第五关|青铜|基于链表实现队列
  • 【Vue】使用v-model实现控制子组件显隐
  • 一篇博客读懂顺序表 —— Sequence-List
  • OceanBase:02-单机部署(生产环境)
  • 【嵌入式 C 常用算法 2 -- 变量值交换函数异或方式实现】
  • Hadoop HDFS(分布式文件系统)
  • 力扣1.两数之和
  • JTA分布式事务管理器
  • 晨控CK-GW08系列网关控制器与CODESYS软件MODBUSTCP通讯手册
  • 读书笔记——labuladong算法笔记
  • Linux中阶教程:bash shell基础
  • Golang 编译原理
  • 基于深度学习的动物识别 - 卷积神经网络 机器视觉 图像识别 计算机竞赛
  • 计算机视觉基础——基于yolov5-face算法的车牌检测