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. 【C/C++】C语言基础知识【第二版】

    基础语法 输出语句 #include <stdio.h> int main(void) { printf("-------分界线1------- \n"); print ...

  2. int 越界处理

    在程序运行中,如果运算结果太大超出了所定义的类型,那么可能会影响整个程序的运行结果,甚至是程序出错. 因此,在编写程序时,加入越界处理就显得一起重要了. 用整型 int 来举个例子吧,int型占四个字 ...

  3. 一个用于学习的react项目

    React-element 根据开源项目 vue-sell进行的开发,将其改造成了react的项目.在开始学习vue的时候就是用的这个项目,发现效果不错,所以在学习React也使用了此项目. 目的:将 ...

  4. 牛客网-剑指Offer 二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  5. 在Nginx或Tengine服务器上安装证书

    阿里云SSL证书服务支持下载证书并安装到Nginx.Tengine服务器上,本文介绍了证书安装的具体操作. 前提条件 已准备远程登录工具,例如PuTTY或者Xshell. 背景信息 本文档以CentO ...

  6. Python计算身体质量指数BMI

    使用Python计算身体质量指数BMI 运行结果如下: 源代码: 1 ''' 2 3. 利用函数思想,将"身体质量指数BMI"源程序封装成为一个函数并调用. 3 :param he ...

  7. Spring Boot-使用Spring Initializer快速创建Spring Boot项目

    File->project->Spring Initializer 点击next 点击下一步即可,如果是第一次可能需要下载jar包,如下图 resources文件中的目录结构如上图所示 s ...

  8. mysql HikariCP连接池配置

    #连接池配置 #最小空闲连接,默认值10,小于0或大于maximum-pool-size,都会重置为maximum-pool-size spring.datasource.hikari.minimum ...

  9. SpringMVC踩的第一个坑——Servlet.init()引发异常

    正确的设置了第一个SpringMVC相关的配置,初始启动服务器时,报404,经过排查,是项目生成构建的时候没有导入好依赖,手动在项目结构里面新建lib目录添加依赖解决了404的问题,重新部署以后开始报 ...

  10. Java实现平滑加权轮询算法--降权和提权

    上一篇讲了普通轮询.加权轮询的两种实现方式,重点讲了平滑加权轮询算法,并在文末留下了悬念:节点出现分配失败时降低有效权重值:成功时提高有效权重值(但不能大于weight值). 本文在平滑加权轮询算法的 ...