博客
关于我
面试题 16.15. 珠玑妙算
阅读量:536 次
发布时间:2019-03-08

本文共 2479 字,大约阅读时间需要 8 分钟。

珠玑妙算游戏(the game of master mind)的玩法如下。

计算机有4个槽,每个槽放一个球,颜色可能是红色(R)、黄色(Y)、绿色(G)或蓝色(B)。例如,计算机可能有RGGB 4种(槽1为红色,槽2、3为绿色,槽4为蓝色)。作为用户,你试图猜出颜色组合。打个比方,你可能会猜YRGB。要是猜对某个槽的颜色,则算一次“猜中”;要是只猜对颜色但槽位猜错了,则算一次“伪猜中”。注意,“猜中”不能算入“伪猜中”。

给定一种颜色组合solution和一个猜测guess,编写一个方法,返回猜中和伪猜中的次数answer,其中answer[0]为猜中的次数,answer[1]为伪猜中的次数。

示例:

输入: solution="RGBY",guess="GGRR"输出: [1,1]解释: 猜中1次,伪猜中1次。

提示:

  • len(solution) = len(guess) = 4
  • solutionguess仅包含"R","G","B","Y"这4种字符
package Solutionmst1615;import java.util.Arrays;class Solution {	public int[] masterMind(String solution, String guess) {		int guessRcount = 0;		int guessYcount = 0;		int guessGcount = 0;		int guessBcount = 0;		int solutionRcount = 0;		int solutionYcount = 0;		int solutionGcount = 0;		int solutionBcount = 0;		for (int i = 0; i < guess.length(); i++) {			if (guess.charAt(i) == 'R') {				guessRcount++;			}			if (guess.charAt(i) == 'Y') {				guessYcount++;			}			if (guess.charAt(i) == 'G') {				guessGcount++;			}			if (guess.charAt(i) == 'B') {				guessBcount++;			}		}		for (int i = 0; i < solution.length(); i++) {			if (solution.charAt(i) == 'R') {				solutionRcount++;			}			if (solution.charAt(i) == 'Y') {				solutionYcount++;			}			if (solution.charAt(i) == 'G') {				solutionGcount++;			}			if (solution.charAt(i) == 'B') {				solutionBcount++;			}		}		int[] out = new int[2];		for (int i = 0; i < guess.length(); i++) {			for (int j = 0; j < solution.length(); j++) {				if (guess.charAt(i) == solution.charAt(j) && i == j) {					if (solution.charAt(i) == 'R') {						solutionRcount--;						guessRcount--;					}					if (solution.charAt(i) == 'Y') {						solutionYcount--;						guessYcount--;					}					if (solution.charAt(i) == 'G') {						solutionGcount--;						guessGcount--;					}					if (solution.charAt(i) == 'B') {						solutionBcount--;						guessBcount--;					}					out[0]++;				}			}		}		if (guessRcount > 0 && solutionRcount > 0) {			out[1] = out[1] + Math.min(guessRcount, solutionRcount);		}		if (guessYcount > 0 && solutionYcount > 0) {			out[1] = out[1] + Math.min(guessYcount, solutionYcount);		}		if (guessGcount > 0 && solutionGcount > 0) {			out[1] = out[1] + Math.min(guessGcount, solutionGcount);		}		if (guessBcount > 0 && solutionBcount > 0) {			out[1] = out[1] + Math.min(guessBcount, solutionBcount);		}		return out;	}	public static void main(String[] args) {		Solution sol = new Solution();		String solution = "YBBY", guess = "GYYB";		System.out.println(Arrays.toString(sol.masterMind(solution, guess)));	}}

 

转载地址:http://rhniz.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(51)——Linux主机Mysql数据库自动备份
查看>>
Mysql学习总结(52)——最全面的MySQL 索引详解
查看>>
Mysql学习总结(53)——使用MySql开发的Java开发者规范
查看>>
Mysql学习总结(54)——MySQL 集群常用的几种高可用架构方案
查看>>
Mysql学习总结(55)——MySQL 语句大全再温习
查看>>
Mysql学习总结(56)——MySQL用户管理和权限设置
查看>>
Mysql学习总结(57)——MySQL查询当天、本周、本月、上周、本周、上月、距离当前现在6个月数据
查看>>
Mysql学习总结(58)——深入理解Mysql的四种隔离级别
查看>>
Mysql学习总结(59)——数据库分库分表策略总结
查看>>
Mysql学习总结(5)——MySql常用函数大全讲解
查看>>
Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
查看>>
Mysql学习总结(61)——MySQL优化之DBA级优化整理汇总
查看>>
Mysql学习总结(62)——MySQL连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link问题
查看>>
Mysql学习总结(63)——Mysql数据库架构方案选择与分析
查看>>
Mysql学习总结(64)——Mysql配置文件my.cnf各项参数解读
查看>>
Mysql学习总结(65)——项目实战中常用SQL实践总结
查看>>
Mysql学习总结(66)——设置MYSQL数据库编码为UTF-8
查看>>
Mysql学习总结(67)——MYSQL慢查询日志
查看>>
Mysql学习总结(68)——MYSQL统计每天、每周、每月、每年数据 SQL 总结
查看>>
Mysql学习总结(69)——Mysql EXPLAIN 命令使用总结
查看>>