zoukankan      html  css  js  c++  java
  • [leetcode] Bulls and Cows

    题目:

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"), your friend will use those hints to find out the secret number.
    
    For example:
    
    Secret number:  1807
    Friend's guess: 7810
    Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
    According to Wikipedia: "Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking mind or paper and pencil game for two or more players, predating the similar commercially marketed board game Mastermind. The numerical version of the game is usually played with 4 digits, but can also be played with 3 or any other number of digits."
    
    Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows, in the above example, your function should return 1A3B.
    
    You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

    解题思路:

    1. guess中总共猜对多少个secret中的字符(包括下标不对应的情况),记为sum.

    2. 统计下标相同时,guess猜对的字符数,记为numA.

    3. 统计 wrong position情况下,guess猜对的字符数,为sum-numA.

    Java代码如下:

        public String getHint(String secret, String guess) { 
            HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
            for (int i = 0; i < secret.length(); i++) { //借助HashMap,统计secret中每个字符出现的次数
                if (! hm.containsKey(secret.charAt(i))) {
                    hm.put(secret.charAt(i), 1);
                } else {
                    hm.put(secret.charAt(i), hm.get(secret.charAt(i)) + 1);
                }
            }
            for (int i = 0; i < guess.length(); i++) { //分析guess,看猜对了多少个secret中的字符
                if (hm.containsKey(guess.charAt(i)) && hm.get(guess.charAt(i)) > 0) {
                    hm.put(guess.charAt(i), hm.get(guess.charAt(i)) - 1);
                }
            }
            int tmp = 0;
            for (Integer i : hm.values()) {
                tmp += i;
            }
            int sum = secret.length() - tmp; //guess中总共猜对多少个secret中的字符(包括下标不对应的情况)
            
            int numA = 0;
            for (int i = 0; i < secret.length(); i++) { //统计下标相同时,guess猜对的字符数
                if (secret.charAt(i) == guess.charAt(i)) {
                    numA++;
                }
            }
            int numB = sum - numA; //统计 wrong position情况下,guess猜对的字符数
            return "" + numA + "A" + numB + "B";
        }
  • 相关阅读:
    超赞!推荐一个专注于Java后端源码分析的Github项目!
    SpringApplication对象是如何构建的? SpringBoot源码(八)
    Java是如何实现自己的SPI机制的? JDK源码(一)
    SpringBoot的启动流程是怎样的?SpringBoot源码(七)
    SpringBoot内置的各种Starter是怎样构建的?--SpringBoot源码(六)
    外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)
    SpringBoot是如何实现自动配置的?--SpringBoot源码(四)
    设计模式目录
    桥接模式
    常见的HTTP状态码
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4926350.html
Copyright © 2011-2022 走看看