leetcode-Bulls and Cows
2016-04-18 12:09
381 查看
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:
Hint:
Write a function to return a hint according to the secret number and friend's guess, use
Please note that both secret number and friend's guess may contain duplicate digits, for example:
In this case, the 1st
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
背景介绍:
通常由两个人玩,一方出数字,一方猜。出数字的人要想好一个没有重复数字的4个数,不能让猜的人知道。猜的人就可以开始猜。每猜一个数字,出数者就要根据这个数字给出几A几B,其中A前面的数字表示位置正确的数的个数,而B前的数字表示数字正确而位置不对的数的个数。
如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。
接着猜的人再根据出题者的几A几B继续猜,直到猜中(即 4A0B)为止。
code:
package leetcode;
import java.util.*;
public class BullsAndCows {
public static String getHint(String secret, String guess) {
//自己的思路:用集合做,考虑不全面,不是从问题出发
/*
* int bulls = 0; int cows = 0; List<Character> arr = new ArrayList<>();
* for(int i=0;i<secret.length();i++){ arr.add(secret.charAt(i)); }
* for(int i=0;i<secret.length();i++){ if(secret.charAt(i) ==
* guess.charAt(i)){ bulls ++; arr.set(i,'a'); }else
* if(arr.contains(guess.charAt(i))){ cows ++; } } if(index ==4) return
* "4A0B"; return bulls+"A"+cows+"B"; } public static void main(String[]
* args) { // TODO Auto-generated method stub String a = "01"; String b
* = "11"; System.out.print(getHint(a,b));
*/
// 思路:secret have one char, result plus; guess have one, result reduce.
// 这是用一个数组记录,+1,-1;
/*
* if(secret.length() == 0){return "0A0B";}
*
* int bull = 0; int cow = 0; int [] result = new int [10];
*
* for(int i = 0;i<secret.length();i++) { int x = secret.charAt(i) - 48; //char转成int类型1
* int y = guess.charAt(i) - 48;
*
* if(x == y) { bull++; } else { if(result[x] < 0){cow++;} result[x]++;
* if(result[y] > 0){cow++;} result[y]--; } }
*
* return bull+"A"+cow+"B";
*/
// 最简单的思路:将每位数字的出现次数记录到 数组上!! good idea!!
// 如果对应相等就不用记录,不相等对应数组就 +1,最后取这两个数组相应位置的较小值;
//这是用数组记录重复的数字,如果重复。。。。。如果不重复。。。。
int len = secret.length();
int[] secretarr = new int[10];
int[] guessarr = new int[10];
int bull = 0, cow = 0;
for (int i = 0; i < len; ++i) {
if (secret.charAt(i) == guess.charAt(i)) {
++bull;
} else {
++secretarr[secret.charAt(i) - '0']; //char转成int类型2
++guessarr[guess.charAt(i) - '0'];
}
}
for (int i = 0; i < 10; ++i) {
cow += Math.min(secretarr[i], guessarr[i]);
}
return "" + bull + "A" + cow + "B";
}
}
思考:如何用hash来做?
For example:
Secret number: "1807" Friend's guess: "7810"
Hint:
1bull and
3cows. (The bull is
8, the cows are
0,
1and
7.)
Write a function to return a hint according to the secret number and friend's guess, use
Ato indicate the bulls and
Bto 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
1in friend's guess is a bull, the 2nd or 3rd
1is 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.
背景介绍:
通常由两个人玩,一方出数字,一方猜。出数字的人要想好一个没有重复数字的4个数,不能让猜的人知道。猜的人就可以开始猜。每猜一个数字,出数者就要根据这个数字给出几A几B,其中A前面的数字表示位置正确的数的个数,而B前的数字表示数字正确而位置不对的数的个数。
如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。
接着猜的人再根据出题者的几A几B继续猜,直到猜中(即 4A0B)为止。
code:
package leetcode;
import java.util.*;
public class BullsAndCows {
public static String getHint(String secret, String guess) {
//自己的思路:用集合做,考虑不全面,不是从问题出发
/*
* int bulls = 0; int cows = 0; List<Character> arr = new ArrayList<>();
* for(int i=0;i<secret.length();i++){ arr.add(secret.charAt(i)); }
* for(int i=0;i<secret.length();i++){ if(secret.charAt(i) ==
* guess.charAt(i)){ bulls ++; arr.set(i,'a'); }else
* if(arr.contains(guess.charAt(i))){ cows ++; } } if(index ==4) return
* "4A0B"; return bulls+"A"+cows+"B"; } public static void main(String[]
* args) { // TODO Auto-generated method stub String a = "01"; String b
* = "11"; System.out.print(getHint(a,b));
*/
// 思路:secret have one char, result plus; guess have one, result reduce.
// 这是用一个数组记录,+1,-1;
/*
* if(secret.length() == 0){return "0A0B";}
*
* int bull = 0; int cow = 0; int [] result = new int [10];
*
* for(int i = 0;i<secret.length();i++) { int x = secret.charAt(i) - 48; //char转成int类型1
* int y = guess.charAt(i) - 48;
*
* if(x == y) { bull++; } else { if(result[x] < 0){cow++;} result[x]++;
* if(result[y] > 0){cow++;} result[y]--; } }
*
* return bull+"A"+cow+"B";
*/
// 最简单的思路:将每位数字的出现次数记录到 数组上!! good idea!!
// 如果对应相等就不用记录,不相等对应数组就 +1,最后取这两个数组相应位置的较小值;
//这是用数组记录重复的数字,如果重复。。。。。如果不重复。。。。
int len = secret.length();
int[] secretarr = new int[10];
int[] guessarr = new int[10];
int bull = 0, cow = 0;
for (int i = 0; i < len; ++i) {
if (secret.charAt(i) == guess.charAt(i)) {
++bull;
} else {
++secretarr[secret.charAt(i) - '0']; //char转成int类型2
++guessarr[guess.charAt(i) - '0'];
}
}
for (int i = 0; i < 10; ++i) {
cow += Math.min(secretarr[i], guessarr[i]);
}
return "" + bull + "A" + cow + "B";
}
}
思考:如何用hash来做?
相关文章推荐
- Activity之间切换(跳转)方向
- 暴力枚举
- Android学习笔记之RatingBar1>简介 RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style。 RatingBar是SeekBa
- objective c怎么修改类名
- 爱在心中【tarjan + 缩点】
- LeetCode 55. Jump Game
- linux ssh上传和下载
- Service Broker完成实例之间的会话详细解读
- 博客
- CocoaPods安装和使用教程
- python模块:re模块
- python模块:re模块
- PAM登录代码
- SSL协议详解
- simpleOS 1.0
- SQL语句oracle中如何插入Date类型的数据
- Get请求出现乱码的解决方案
- 各种view
- oracle 手工建库
- AngularJS入门教程之AngularJS指令