2103:环和杆

This commit is contained in:
轩辕龙儿 2022-04-14 08:49:31 +08:00
parent 90bbf1be85
commit 1a41f1a398
2 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,90 @@
//总计有 n 个环环的颜色可以是红绿蓝中的一种这些环分布穿在 10 根编号为 0 9 的杆上
//
// 给你一个长度为 2n 的字符串 rings 表示这 n 个环在杆上的分布rings 中每两个字符形成一个 颜色位置对 用于描述每个环
//
//
// i 对中的 第一个 字符表示第 i 个环的 颜色'R''G''B'
// i 对中的 第二个 字符表示第 i 个环的 位置也就是位于哪根杆上'0' '9'
//
//
// 例如"R3G2B1" 表示共有 n == 3 个环红色的环在编号为 3 的杆上绿色的环在编号为 2 的杆上蓝色的环在编号为 1 的杆上
//
// 找出所有集齐 全部三种颜色 环的杆并返回这种杆的数量
//
//
//
// 示例 1
//
// 输入rings = "B0B6G0R6R0R6G9"
//输出1
//解释
//- 编号 0 的杆上有 3 个环集齐全部颜色绿
//- 编号 6 的杆上有 3 个环但只有红蓝两种颜色
//- 编号 9 的杆上只有 1 个绿色环
//因此集齐全部三种颜色环的杆的数目为 1
//
//
// 示例 2
//
// 输入rings = "B0R0G0R9R0B0G0"
//输出1
//解释
//- 编号 0 的杆上有 6 个环集齐全部颜色绿
//- 编号 9 的杆上只有 1 个红色环
//因此集齐全部三种颜色环的杆的数目为 1
//
//
// 示例 3
//
// 输入rings = "G4"
//输出0
//解释
//只给了一个环因此不存在集齐全部三种颜色环的杆
//
//
//
//
// 提示
//
//
// rings.length == 2 * n
// 1 <= n <= 100
// i 偶数 rings[i] 的值可以取 'R''G' 'B'下标从 0 开始计数
// i 奇数 rings[i] 的值可以取 '0' '9' 中的一个数字下标从 0 开始计数
//
// Related Topics 哈希表 字符串 👍 13 👎 0
package leetcode.editor.cn;
//2103:环和杆
public class RingsAndRods {
public static void main(String[] args) {
Solution solution = new RingsAndRods().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countPoints(String rings) {
int[][] arrs = new int[10][3];
for (int i = 0; i < rings.length(); i += 2) {
if (rings.charAt(i) == 'R') {
arrs[rings.charAt(i + 1) - '0'][0]++;
} else if (rings.charAt(i) == 'G') {
arrs[rings.charAt(i + 1) - '0'][1]++;
} else {
arrs[rings.charAt(i + 1) - '0'][2]++;
}
}
int count = 0;
for (int[] arr : arrs) {
if (arr[0] * arr[1] * arr[2] > 0) {
count++;
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,55 @@
<p>总计有 <code>n</code> 个环,环的颜色可以是红、绿、蓝中的一种。这些环分布穿在 10 根编号为 <code>0</code><code>9</code> 的杆上。</p>
<p>给你一个长度为 <code>2n</code> 的字符串 <code>rings</code> ,表示这 <code>n</code> 个环在杆上的分布。<code>rings</code> 中每两个字符形成一个 <strong>颜色位置对</strong> ,用于描述每个环:</p>
<ul>
<li><code>i</code> 对中的 <strong>第一个</strong> 字符表示第 <code>i</code> 个环的 <strong>颜色</strong><code>'R'</code><code>'G'</code><code>'B'</code>)。</li>
<li><code>i</code> 对中的 <strong>第二个</strong> 字符表示第 <code>i</code> 个环的 <strong>位置</strong>,也就是位于哪根杆上(<code>'0'</code><code>'9'</code>)。</li>
</ul>
<p>例如,<code>"R3G2B1"</code> 表示:共有 <code>n == 3</code> 个环,红色的环在编号为 3 的杆上,绿色的环在编号为 2 的杆上,蓝色的环在编号为 1 的杆上。</p>
<p>找出所有集齐 <strong>全部三种颜色</strong> 环的杆,并返回这种杆的数量。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/23/ex1final.png" style="width: 258px; height: 130px;">
<pre><strong>输入:</strong>rings = "B0B6G0R6R0R6G9"
<strong>输出:</strong>1
<strong>解释:</strong>
- 编号 0 的杆上有 3 个环,集齐全部颜色:红、绿、蓝。
- 编号 6 的杆上有 3 个环,但只有红、蓝两种颜色。
- 编号 9 的杆上只有 1 个绿色环。
因此,集齐全部三种颜色环的杆的数目为 1 。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/23/ex2final.png" style="width: 266px; height: 130px;">
<pre><strong>输入:</strong>rings = "B0R0G0R9R0B0G0"
<strong>输出:</strong>1
<strong>解释:</strong>
- 编号 0 的杆上有 6 个环,集齐全部颜色:红、绿、蓝。
- 编号 9 的杆上只有 1 个红色环。
因此,集齐全部三种颜色环的杆的数目为 1 。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>rings = "G4"
<strong>输出:</strong>0
<strong>解释:</strong>
只给了一个环,因此,不存在集齐全部三种颜色环的杆。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>rings.length == 2 * n</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>i</code><strong>偶数</strong> ,则&nbsp;<code>rings[i]</code> 的值可以取 <code>'R'</code><code>'G'</code><code>'B'</code>(下标从 <strong>0</strong> 开始计数)</li>
<li><code>i</code><strong>奇数</strong> ,则&nbsp;<code>rings[i]</code> 的值可以取 <code>'0'</code><code>'9'</code> 中的一个数字(下标从 <strong>0</strong> 开始计数)</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 13</li><li>👎 0</li></div>