资讯专栏INFORMATION COLUMN

leetcode299. Bulls and Cows

Kross / 2393人阅读

摘要:题目要求游戏简单来说就是你随手写下一个位数,并让你同学猜这个数字是什么。第二次再在此基础上计算重合的值和没有重合的值的个数。这样的话,如果下一次遇到重复但是位置不同的值,我们可以知道它是否已经在中或是中出现过。

题目要求
You are playing the following Bulls and Cows game with your friend:
You write down a number and ask your friend to guess what the number is. 
Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). 
Your friend will use successive guesses and hints to eventually derive 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.)
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".

Please note that both secret number and friend"s guess may contain duplicate digits, for example:

Secret number:  "1123"
Friend"s guess: "0111"
In this case, the 1st 1 in friend"s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".
You may assume that the secret number and your friend"s guess only contain digits, and their lengths are always equal.

Bulls and Cows游戏简单来说就是你随手写下一个n位数,并让你同学猜这个数字是什么。假设你的朋友也会猜测一个n位数,他每猜一个数字,你就需要告诉他,猜测的数字中位置正确且值正确的数字(bulls)有几个,位置不正确但是值不正确的数字(cows)有几个。

思路与代码

最开始的时候我通过两圈遍历,第一次用一个数组存储每个数字出现的次数。第二次再在此基础上计算重合的值和没有重合的值的个数。但是还有更好的方法,只需要一圈遍历就可以完成这个过程。

在一圈遍历中,每当遇到重合值,则bulls的数字加一,否则我们将secret number中的数字计数加1,guess number中的数字计数减一。这样的话,如果下一次遇到重复但是位置不同的值,我们可以知道它是否已经在secret中或是guess中出现过。

    public String getHint(String secret, String guess){
        int bulls=0,cows=0;
        int[] counter = new int[10];
        int n=secret.length();
        for(int i =0; i0) cows++;
                if(counter[s]<0) cows++;
                counter[s]++;
                counter[g]--;
            }   
        }    
        return bulls + "A" + cows + "B";
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68670.html

相关文章

  • [LeetCode] 299. Bulls and Cows

    Problem You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a ...

    stefan 评论0 收藏0
  • 299. Bulls and Cows

    摘要:题目这里主要是想记录一下这个很聪明的解法我规规矩矩的解法 题目:You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend...

    hlcc 评论0 收藏0
  • [译][Tkinter 教程13] Mastermind 游戏

    摘要:已获原作者授权原系列地址游戏本章我们演示一个进阶例子我们用编写了游戏这个游戏也被称作或者或者是一个古老的益智解谜游戏由两名玩家参与早在世纪人们就在用铅笔和纸来玩这个游戏了在年发明的游戏正是受到这个游戏的启发和在基本理念上是一样的但被盒装出售 已获原作者授权. 原系列地址: Python Tkinter Mastermind 游戏 本章我们演示一个进阶例子. 我们用 Tkinter 编...

    Jaden 评论0 收藏0
  • JS数组:push vs concat

    摘要:使用这么久对于数组的相关方法一直都是拿来就用对于方法更是常用。不过对于多个数组合并的时候因为返回的是新数组,可以链式下去。 使用JS这么久, 对于JS数组的相关方法一直都是拿来就用,对于push方法更是常用。但是在一次用到contact方法的时候自问了一句: push和contact到底有哪些区别? 先看下MDN的定义: 【push】:adds one or more element...

    animabear 评论0 收藏0
  • ES6 的模块系统

    摘要:的模块系统被设计成让你可以一次性引入多个变量。动态静态,或者说规矩和如何打破规矩作为一门动态编程语言,令人惊讶地拥有一个静态的模块系统。只要你的需求都是静态的话,这个模块系统还是很的。 此文为翻译,原文地址在这儿:https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ES6 是 ECMAScript 第 6 版本的简称,这是新一...

    MudOnTire 评论0 收藏0

发表评论

0条评论

Kross

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<