Question

299. Bulls and Cows

Solution

题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一个cow,注:数字对与位置对优先,一个号码不能重复判断.

思路:构造map结构,遍历实现

Java实现:实现的不漂亮,好歹能通过

public String getHint(String secret, String guess) {
Map<Character, Index> map = new HashMap<>();
for (int i=0; i<secret.length(); i++) {
Index idx = map.get(secret.charAt(i));
if (idx == null) {
idx = new Index();
map.put(secret.charAt(i), idx);
}
idx.add(i);
} int bulls = 0;
int cows = 0;
List<Character> cowsList = new ArrayList<>(); // for count cows
// count bulls
for (int i=0; i<guess.length(); i++) {
Index idx = map.get(guess.charAt(i));
if (idx != null) { // check digits
if (idx.isBull(i)) {
bulls++;
} else {
cowsList.add(guess.charAt(i));
}
}
}
// count cows
for (char c : cowsList) {
Index idx = map.get(c);
if (idx.isCow()) {
cows++;
}
}
return bulls + "A" + cows + "B";
} class Index {
List<Integer> idxList;
int count; // constructor
public Index() {
idxList = new ArrayList<>();
count = 0;
} void add(int x) {
idxList.add(x);
count++;
} boolean isCow() {
return count-- > 0;
} boolean isBull(int x) {
for (int tmp : idxList) {
if (x == tmp) {
count--;
return true;
}
}
return false;
}
}

Ref

https://leetcode.com/problems/bulls-and-cows/discuss/74621/One-pass-Java-solution

public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
int s = Character.getNumericValue(secret.charAt(i));
int g = Character.getNumericValue(guess.charAt(i));
if (s == g) bulls++;
else {
if (numbers[s] < 0) cows++;
if (numbers[g] > 0) cows++;
numbers[s] ++;
numbers[g] --;
}
}
return bulls + "A" + cows + "B";
}
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) bulls++;
else {
if (numbers[secret.charAt(i)-'0']++ < 0) cows++;
if (numbers[guess.charAt(i)-'0']-- > 0) cows++;
}
}
return bulls + "A" + cows + "B";
}

299. Bulls and Cows - LeetCode的更多相关文章

  1. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  2. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  3. LeetCode 299 Bulls and Cows

    Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  4. [leetcode]299. Bulls and Cows公牛和母牛

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  5. 299. Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  6. 299 Bulls and Cows 猜数字游戏

    你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...

  7. Bulls and Cows leetcode

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  8. [LC] 299. Bulls and Cows

    Example 1: Input: secret = "1807", guess = "7810" Output: "1A3B" Expla ...

  9. Leetcode 299 Bulls and Cows 字符串处理 统计

    A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...

随机推荐

  1. leedcode_13 罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1 .12 ...

  2. 9_根轨迹_Part3_分离点/汇合点和根的性质

  3. 关于recyclerview其item数据重复问题

    查找方法(query)的list只定义对象,不实例化,等到要添加的时候,再new一个新的对象出来. 千万不要如下图这样,否则item显示出来的永远是最新数据. (这个bug找了两天,还是基本功不扎实, ...

  4. Python:爬取全国各省疫情数据并在地图显示

    代码: import requests import pymysql import json from pyecharts import options as opts from pyecharts. ...

  5. Git本地上传口令

    $ cd /d/GitProject  (进入要上传的文件的文件夹) $ git pull                 (更新本地库) $ git add .                (添加 ...

  6. Android Studio安装及问题

    安装教程+虚拟机调试:https://blog.csdn.net/y74364/article/details/96121530 gradle下载缓慢解决办法:https://blog.csdn.ne ...

  7. 用 JS(JavaScript )实现增删改查

    JS小例题 学习内容: 需求 总结: 学习内容: 需求 用 JavaScript 实现简单增删改查 实现代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HT ...

  8. [翻译] Cassandra 分布式结构化存储系统

    Cassandra 分布式结构化存储系统 摘要 Cassandra 是一个分布式存储系统,用于管理分布在许多商品服务器上的大量结构化数据,同时提供无单点故障(no single point of fa ...

  9. 开始项目之前整理Xmind

    今天为将要做的项目整理了一份Xmind文档,每个页面,模块,功能都记了下来.带我的老师说过于详细了,但我还是新手,也不懂哪里改精简那里不该. 总结:整理Xmind文档还是很有必要的,之前这个项目我大致 ...

  10. Asynchronous Methods for Deep Reinforcement Learning

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! ICML 2016 Abstract 我们提出了一个概念上简单且轻量级的深度强化学习框架,该框架使用异步梯度下降来优化深度神经网络控制器. ...