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.

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.

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.

题意:

猜字游戏。

我负责猜,

你负责给提示:数字和位置都对,bulls++。数字对位置不对,cows++。

思路:

挨个扫字符串s,

挨个扫字符串g

若s当前字符等于g的字符,bulls++

用int[] map = new int[256]建一个简化版的HashMap

思路类似valid anagram

s当前字符在map里标记为++

p当前字符在map里标记为--

那么,若s当前字符已经被标记为--,说明p的字符来标记过,即它们字符相同但位置不同,cow++。

同理,若p当前字符已经被标记为++, 说明s的字符来标记过, 即它们字符相同但位置不同,cow++。

代码:

 class Solution {
public String getHint(String secret, String guess) {
// corner
if(secret.length() != guess.length()) return false;
int[] map = new int[256];
int bull = 0;
int cow = 0;
for(int i = 0; i < secret.length();i++){
char s = secret.charAt(i);
char g = guess.charAt(i);
if(s == g){
bull++;
}else{
if(map[s]<0) cow++;
if(map[g]>0) cow++;
map[s]++;
map[g]--;
}
}
return bull +"A" + cow + "B";
}
}

[leetcode]299. Bulls and Cows公牛和母牛的更多相关文章

  1. LeetCode OJ:Bulls and Cows (公牛与母牛)

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

  2. LeetCode 299 Bulls and Cows

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

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

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

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

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

  5. 299. Bulls and Cows - LeetCode

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

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

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

  7. 299. Bulls and Cows

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

  8. 299 Bulls and Cows 猜数字游戏

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

  9. Java [Leetcode 229]Bulls and Cows

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

随机推荐

  1. pandas的merge函数

    pandas.merge(left,right,how='inner',on=None,left_on=None,right_on=None,left_index=False,right_index= ...

  2. IIS7根据PID查找对应的站点

    runas /user:administrator cmd cd \Windows\System32\inetsrv appcmd.exe list wp

  3. 中国标准时间改为formatTime格式

    1.toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串): var time = new Date(); var formatTime = time.toLoc ...

  4. web.py模版系统

    介绍: 调用的web.py模版语言Templetor旨在将python的强大功能带入模版.它不是为模板创建新语法,而是重用python语法. Templetor故意限制模版中的变量访问.用户可以访问传 ...

  5. python拓展2 collections模块与string模块

    知识内容 1.collections模块介绍 2.collections模块使用 3.string模块介绍及使用 一.collections模块介绍 collections模块中提供了很多python ...

  6. Bogart gGrid.vb

    Namespace BogartMis.Cls Public Class gGrid '設定表格控的列標題的別名 '說明:strItem字符串的格式為"01,02,03,04,05" ...

  7. IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理

    来源:http://www.guchengnet.com/1499.html IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理 发表于2016年12月14日 有2.3个月没有用本地的i ...

  8. stdio.h头文件中申明的基本函数

    调用scanf函数时,需传入变量的地址作为参数,scanf函数会等待标准输入设备(键盘等)输入数据,并且将输入的数据赋值给地址对应的变量. #include<stdio.h> #inclu ...

  9. 13.mysql基本查询

    1. 给表起个别名:但是,前面的也是需要进行修改的,否则会报错的: select * from s.name from students as s; 2. 为字段起别名 select s,name a ...

  10. VBA 使用QueryTables 中文乱码的处理

    一般情况: cnn = "OLEDB;Provider=IBMDA400;Data Source=TFB4001;User ID=;Password=;" Sql = " ...