加载中...
面试题 16.15-珠玑妙算(Master Mind LCCI)
发表于:2021-12-03 | 分类: 简单
字数统计: 959 | 阅读时长: 4分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/master-mind-lcci

英文原文

The Game of Master Mind is played as follows:

The computer has four slots, and each slot will contain a ball that is red (R). yellow (Y). green (G) or blue (B). For example, the computer might have RGGB (Slot #1 is red, Slots #2 and #3 are green, Slot #4 is blue).

You, the user, are trying to guess the solution. You might, for example, guess YRGB.

When you guess the correct color for the correct slot, you get a "hit:' If you guess a color that exists but is in the wrong slot, you get a "pseudo-hit:' Note that a slot that is a hit can never count as a pseudo-hit.

For example, if the actual solution is RGBY and you guess GGRR, you have one hit and one pseudo-hit. Write a method that, given a guess and a solution, returns the number of hits and pseudo-hits.

Given a sequence of colors solution, and a guess, write a method that return the number of hits and pseudo-hit answer, where answer[0] is the number of hits and answer[1] is the number of pseudo-hit.

Example:

Input:  solution="RGBY",guess="GGRR"
Output:  [1,1]
Explanation:  hit once, pseudo-hit once.

Note:

  • len(solution) = len(guess) = 4
  • There are only "R","G","B","Y" in solution and guess.

中文题目

珠玑妙算游戏(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种字符

通过代码

高赞题解

解题思路

方法零:因为solution和guess的长度都只有4,所以嵌套for循环暴力解的话,也很快。

方法一:

  1. 设置一个长26的数组map(目的是将RYGB对应到数组的index中)
  2. for循环遍历solution和guess
  3. 如果solution和guess对应元素相等,则直接real++
  4. 若不相等,判断map中sol元素是否小于0(代表之前存过guess的元素),存在则fake++,然后更新map[sol - ‘A’]++;
  5. 对map中的gue元素做同等判断
  6. 返回答案ans。

方法二:

  1. 使用了HashMap,将solution的元素保存到map中(包含元素数量)
  2. 然后判断map中是否有guess的元素,有则fake++,注意要更新元素数量
  3. 在来个for循环判断一致的数据real,最后fake - real等于伪猜对

这些题目都是简单的,但方法和思路就在那,还是要多思考和多做优化的工作。

代码

方法一:

class Solution {
    public int[] masterMind(String solution, String guess) {

        int fake = 0, real = 0;

        int[] map = new int[26];

        for(int i = 0; i < 4; i++){
            char sol = solution.charAt(i), gue = guess.charAt(i);

            if(sol == gue) real++;
            else{
                if(map[sol - 'A'] < 0) fake++;
                map[sol - 'A']++;
                
                if(map[gue - 'A'] > 0) fake++;
                map[gue - 'A']--;
            }
        }

        int[] ans = {real, fake};

        return ans;
    }
}

方法二:

class Solution {
    public int[] masterMind(String solution, String guess) {

        HashMap<Character, Integer> map = new HashMap<Character, Integer>();

        for(char c : solution.toCharArray()){
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        int fake = 0, real = 0;
        for(char c : guess.toCharArray()){
            if(map.containsKey(c) && map.get(c) > 0){
                fake++;
                map.put(c, map.get(c) - 1);
            }   
        }

        for(int i = 0; i < 4; i++){
            if(solution.charAt(i) == guess.charAt(i))
                real++;
        }

        int[] ans = {real, fake - real};

        return ans;
    }
}

统计信息

通过次数 提交次数 AC比率
9624 19100 50.4%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
面试题 16.14-最佳直线(Best Line LCCI)
下一篇:
面试题 16.16-部分排序(Sub Sort LCCI)
本文目录
本文目录