题目描述:

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.

解题思路:

对于secret列每次读取相应的部分,如果与guess对应的部分相同,则bulls变量加一;否则,对于secret的部分相应字典的数值加一,guess对应的字典减一。

再判断secret对应字典的数值是否小于等于0,若是则把bulls的变量加一(表明之前guess中已经出现该字符,且配对成功)

判断guess对应字典的数值是否大于等于0,若是则把bulls的变量加一(表明之前secret中已经出现该字符,且配对成功)

代码如下:

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

  

Java [Leetcode 229]Bulls and Cows的更多相关文章

  1. LeetCode 299 Bulls and Cows

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

  2. LeetCode(45)-Bulls and Cows

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

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

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

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

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

  5. 299. Bulls and Cows - LeetCode

    Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...

  6. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  7. [Leetcode] Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

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

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

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

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

随机推荐

  1. CF Gym100548 K Last Defence 解题报告

    先特判掉特殊情况: $a=b,Ans=2$ $ab=0,a+b>0,Ans=2$ $a=b=0,Ans=1$ 考虑剩下的非特殊情况.记$Solve(a,b)$为数列中除了$a,b$外的不同的数的 ...

  2. 在线最优化求解(Online Optimization)之二:截断梯度法(TG)

    在线最优化求解(Online Optimization)之二:截断梯度法(TG) 在预备篇中我们做了一些热身,并且介绍了L1正则化在Online模式下也不能产生较好的稀疏性,而稀疏性对于高维特征向量以 ...

  3. hdu 1754 线段树入门

    线段树点修改  区间最大值查询 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...

  4. 任务调度 QUARTZ

    任务调度在目前的JAVA应用程序中运用的十分普遍,故掌握QUARTZ是必备的技能 闲话少说,上官网:http://www.quartz-scheduler.org/ 下载最新1.80资源包 commo ...

  5. Zabbix 集成 OneAlert 实现全方位告警

    1. 前言 告警将重要信息发送给运维「或者其他相关人」,及时发现并且处理问题.在所有开源监控软件里面,Zabbix 的告警方式无疑是最棒的.告警的方式各式各样,从 Email 告警到飞信.139/18 ...

  6. 为什么android的R类要定义成16进制

    联想到c语言中的宏定义:我想是一个原因 如: #define SDL_INIT_TIMER 0x00000001 #define SDL_INIT_AUDIO 0x00000010 #define S ...

  7. 架构探险——从零开始写Java Web框架》第二章照作

    沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...

  8. [转]数据结构之Trie树

    1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...

  9. C++和pascal之间的通信

    // MyFirst.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "Winsock2.h"#pragm ...

  10. 【Linux高频命令专题(4)】sed

    简述 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法 sed命令行格式为: sed [-ne ...