彩色瓷砖
来源:互联网 发布:网络最新骗术扒人 编辑:程序博客网 时间:2024/06/10 22:07
题目描述:
链接:https://www.nowcoder.com/questionTerminal/31af498841fd491886b8dee6ebde9768
来源:牛客网
牛牛喜欢彩色的东西,尤其是彩色的瓷砖。牛牛的房间内铺有L块正方形瓷砖。每块砖的颜色有四种可能:红、绿、蓝、黄。给定一个字符串S, 如果S的第i个字符是’R’, ‘G’, ‘B’或’Y’,那么第i块瓷砖的颜色就分别是红、绿、蓝或者黄。
牛牛决定换掉一些瓷砖的颜色,使得相邻两块瓷砖的颜色均不相同。请帮牛牛计算他最少需要换掉的瓷砖数量。
输入描述:
输入包括一行,一个字符串S,字符串长度length(1 ≤ length ≤ 10),字符串中每个字符串都是’R’, ‘G’, ‘B’或者’Y’。
输出描述:
输出一个整数,表示牛牛最少需要换掉的瓷砖数量
代码实现
import java.util.Scanner;/** * Created by Cser_W on 2017/10/12. */public class niuniuLikeColors { public static int countReplaceColors (String str) { if (str == null) return 0; int count = 0; char[] color = new char[]{'R','G','B','Y'}; char[] colorArr = str.toCharArray(); for (int i = 0; i < colorArr.length - 1; i++) { if (colorArr[i] == colorArr[i+1]) { if (i + 2 < colorArr.length) { for (int j = 0; j < color.length; j++) { if (colorArr[i] != color[j] && colorArr[i + 2] != color[j]) { colorArr[i + 1] = color[j]; break; } } } i++; count ++; } } return count; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.next(); int res = countReplaceColors(str); System.out.println(res); }}
阅读全文
0 0
- 彩色瓷砖
- 彩色瓷砖
- 彩色瓷砖
- 彩色瓷砖
- 牛客 彩色瓷砖
- [编程题] 彩色瓷砖
- [编程题] 彩色瓷砖
- 六、彩色瓷砖
- 牛客网彩色瓷砖C/C++解决
- java面试题——编程题:彩色瓷砖
- 多米诺瓷砖
- 铺瓷砖
- 贴瓷砖
- 瓷砖覆盖
- 瓷砖问题
- 贴瓷砖
- 贴瓷砖
- 铺瓷砖
- keras配置(WIN10/GPU版本)
- 集合:set 与 映射 map
- 数据结构及算法(Python)---栈
- 面向对象六大设计原则(二)开闭原则
- springmvc的运行原理
- 彩色瓷砖
- Gym
- OSGi入门必读系列《OSGi服务:非常适合SOA的架构》
- 周中总结--2017.9.12
- 文章标题
- '乡吧'社区APP(包含安卓端和后台代码)
- 输入几个数,如输入5个数,1,2,3,4,5,输出
- 在线测试
- linux 01