[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
.)
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.
Solution:
http://www.bubuko.com/infodetail-1174728.html
https://leetcode.com/discuss/67031/one-pass-java-solution
public class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] nums = new int[10];
for(int i = 0; i < secret.length(); ++i) {
if(secret.charAt(i) == guess.charAt(i))
bulls++;
else {
if(nums[secret.charAt(i) - '0'] < 0)
cows++;
if(nums[guess.charAt(i) - '0'] > 0)
cows++;
nums[secret.charAt(i) - '0']++;
nums[guess.charAt(i) - '0']--;
}
}
return bulls + "A" + cows + "B";
}
}
[Leetcode] Bulls and Cows的更多相关文章
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- LeetCode Bulls and Cows (简单题)
题意: 给出两个数字,输出(1)有多少位是相同的(2)有多少位不在正确的位置上. 思路: 扫一遍,统计相同的,并且将两串中不同的数的出现次数分别统计起来,取小者之和就是第2个答案了. class So ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 299. Bulls and Cows - LeetCode
Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
- Bulls and Cows leetcode
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- LeetCode(45)-Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
随机推荐
- Git 如何只更新项目中某个目录里的文件
Git由于在远端和本地都有一个代码库, 这样更新单个文件比SVN要麻烦一点. 1. 如果想拿远端git服务器上的最新版本(或某个特定版本)覆盖本地的修改,可以使用git pull命令, 但这会 ...
- phpcms 表单提交发送邮件
修改 phpcms\modules\formguide index.php 找到 foreach ($mails as $m) { sendmail($m, L('tips'), $this-> ...
- [原创]JavaEE在CentOS服务器上的部署
1.安装rz.sz命令 yum install lrzsz 2.配置环境变量 vi .bash_profile 默认的path:PATH=$PATH:$HOME/.local/bin:$HOME/bi ...
- windows 平台 php_Imagick 拓展遇到的那些坑!
我的php环境是使用了phpstudy 下载地址:http://www.phpstudy.net/a.php/211.html 最终并未解决问题 持续更新~ 1.首先到官网上 http://www.i ...
- BZOJ 2091: [Poi2010]The Minima Game
Description 每次可以任取数字,使用最优策略让差最大. Sol DP. 一开始我写了个单调队列贪心,然后狂WA不止... 正着做有后效性,因为前面的决策无法保证在后面是最优秀的,但如果倒这做 ...
- JSON Accelerator真是个好东西...
支持OBJC,JAVA,Python,OBJC(Core Data),Python(Django) 并实现了NSCoding和NSCoping,方便归档和复制. 再也不用辛辛苦苦的写Model了.. ...
- 用C3中的animation和transform写的一个模仿加载的时动画效果
用用C3中的animation和transform写的一个模仿加载的时动画效果! 不多说直接上代码; html标签部分 <div class="wrap"> <h ...
- box-shadow、drop-shadow 和 text-shadow
1. box-shadow 合阴影, 对象选择器 {box-shadow:[投影方式] X轴偏移量 Y轴偏移量阴影模糊半径 阴影扩展半径 阴影颜色} box-shadow属性的参数设置取值: 阴影类型 ...
- @rpath/libswiftCore.dylib问题
dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/containers/Bundle ...
- C语言中,头文件和源文件的关系(转)
简单的说其实要理解C文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程: 1.预处理阶段 2.词法与语法分析阶段 3.编译阶段,首先编译成纯汇编语句, ...