题目:

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 01 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.

链接: http://leetcode.com/problems/bulls-and-cows/

题解:

公牛和奶牛游戏。使用HashMap存下来secret里的字符和count,然后同时遍历secret和guess就可以了。最后还要遍历一次map把多加的cow减掉。

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
public String getHint(String secret, String guess) {
if(secret == null || guess == null || secret.length() != guess.length()) {
return "0A0B";
}
int bulls = 0, cows = 0;
Map<Character, Integer> map = new HashMap<>(); for(int i = 0; i < secret.length(); i++) {
char c = secret.charAt(i);
if(!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
} for(int i = 0; i < secret.length(); i++) {
char sChar = secret.charAt(i);
char gChar = guess.charAt(i);
if(sChar == gChar) {
bulls++;
map.put(gChar, map.get(gChar) - 1);
} else if(map.containsKey(gChar)) {
cows++;
map.put(gChar, map.get(gChar) - 1);
}
} for(char c : map.keySet()) {
if(map.get(c) < 0) {
cows += map.get(c);
}
} return String.valueOf(bulls) + "A" + String.valueOf(cows) + "B";
}
}

二刷:

主要参考了Discuss里面的解。

  1. 我们可以用一个数组来存bulls和cows。用s和g来表示数组的数字值
  2. 当 s = g时,我们找到了bull, bulls++
  3. 否则我们要看
    1. nums[g] > 0的话,说明当前guess的这个数字曾经出现在secret中,这是一个cow,我们cow++
    2. 我们也要看是否nums[s] < 0, 这个表明当前ssecret的数字曾经出现在guess中,这也是一个cow,我们还是cow++
    3. 我们用正数记录下nums[s]为bull的一个位置,nums[s]++,  我们也用负数记录下guess中出现过的数字,nums[g]--,

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public String getHint(String secret, String guess) {
if(secret == null || guess == null || secret.length() != guess.length()) {
return "0A0B";
}
int[] nums = new int[10];
for (int i = 0; i < secret.length(); i++) {
int s = secret.charAt(i) - '0';
int g = guess.charAt(i) - '0';
if (s == g) {
bulls++;
} else {
if (nums[s] < 0) { // bulls can be counted as cows
cows++;
}
if (nums[g] > 0) { // found num but in diff position
cows++;
}
nums[s]++;
nums[g]--;
}
}
return bulls + "A" + cows + "B";
}
}

三刷:

延续了二刷的解法。主要使用一个count[]数组保存之前出现过的secret digits和guess digits。当前sDigit == gDigit时,bulls增加。 否则, 当count[sDigit] < 0时,说明之前出现在guess里, 当count[gDigit] > 0时,说明之前出现在secret里,这两种情况都要分别增加cows。之后再记录i这个位置的改动count[sDigit]++, count[gDigit]--。最后返回结果。

Java:

public class Solution {
public String getHint(String secret, String guess) {
if (secret == null || guess == null || secret.length() != guess.length()) {
return "0A0B";
}
int bulls = 0;
int cows = 0;
int[] count = new int[10];
for (int i = 0; i < secret.length(); i++) {
int sDigit = secret.charAt(i) - '0';
int gDigit = guess.charAt(i) - '0';
if (sDigit == gDigit) {
bulls++;
} else {
if (count[sDigit] < 0) {
cows++;
}
if (count[gDigit] > 0) {
cows++;
}
}
count[sDigit]++;
count[gDigit]--;
}
return bulls + "A" + cows + "B";
}
}

Update:

public class Solution {
public String getHint(String secret, String guess) {
if (secret == null || guess == null || secret.length() != guess.length()) return "0A0B";
int bullsCount = 0, cowsCount = 0;
int len = secret.length();
int[] count = new int[10]; for (int i = 0; i < len; i++) {
int sc = secret.charAt(i) - '0';
int gc = guess.charAt(i) - '0';
if (sc == gc) {
bullsCount++;
} else {
if (count[gc] > 0) cowsCount++;
if (count[sc] < 0) cowsCount++;
count[sc]++;
count[gc]--;
}
}
return bullsCount + "A" + cowsCount + "B";
}
}

Reference:

https://leetcode.com/discuss/67031/one-pass-java-solution

最新文章

  1. 创建一个Phone实体,完成多页面的电话簿项目
  2. iOS 阶段学习第七天笔记(函数、递归)
  3. mysql之旅【第二篇】
  4. cloudsim安装,配置(到eclipse)
  5. apache.http.MalformedChunkCodingException: Chunked stream ended unexpectedly
  6. Nginx使用的php-fpm的两种进程管理方式及优化(转)
  7. 无法定位程序输入点ucrtbase.
  8. iOS 用GDataXMLNode创建和解析XML
  9. Struts2 动态调用方法
  10. 关于weak
  11. Springboot 项目启动后执行某些自定义代码
  12. 【安富莱专题教程第7期】终极调试组件Event Recorder,各种Link通吃,支持时间和功耗测量,printf打印,RTX5及中间件调试
  13. Web API 2 添加Models and Controllers Part 2.
  14. RabbitMq(6) 如何保证消息不丢包
  15. 分析code
  16. 使用 GNU Libtool 创建库
  17. 牛客网多校赛第九场A-circulant matrix【数论】
  18. Jmeter 之 ServerAgent 在性能测试的时候通过插件监听数据库状态
  19. IBM关闭触摸板的方法
  20. 笔记之远程桌面服务(RDS)

热门文章

  1. 基于PBOC电子钱包的圈存过程详解
  2. js读取屏幕长宽
  3. xcode 运行报错 Command /usr/bin/codesign failed with exit code 1
  4. [noi2011]道路修建 树形dp
  5. 【算法】E.W.Dijkstra算术表达式求值
  6. unity android 集成指南
  7. Python中编码问题?
  8. 同一网站不同和二级域名和不同子目录的cookie
  9. POJ 1656
  10. 二、Android NDK编程预备之Java jni入门Hello World