[leetcode]299. Bulls and Cows公牛和母牛
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公牛和母牛的更多相关文章
- LeetCode OJ:Bulls and Cows (公牛与母牛)
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- Leetcode 299 Bulls and Cows 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 299. Bulls and Cows - LeetCode
Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
随机推荐
- Spark分析之Dependency
在Spark中,每一个RDD是对于数据集在某一状态下的表现形式,比如说:map.filter.group by等都算一次操作,这个状态有可能是从前一状态转换而来的: 因此换句话说一个RDD可能与之前的 ...
- HTML5中常用的标签(及标签的属性和作用)
1.标签:<!DOCTYPE>作用:声明是文档中的第一成分,位于<html>标签之前. 2.标签:<html>作用:此元素可告知浏览器其自身是一个HTML文档.属性 ...
- redis 4,0 安装
安装redis : 1,yum install wget -y 2,cd /opt: 3,wget http://download.redis.io/releases/redis-4.0.10.tar ...
- 常用数据库4 mongodb
知识内容: 1.mongodb介绍与基本使用 2.mongodb操作 一.mongodb介绍与基本使用 1.mongodb介绍 Mongodb是一款强大,灵活,且易于扩展的通用型数据库.它能扩展出非常 ...
- jQuery ajax - serializeArray() 方法
定义和用法 serializeArray() 方法通过序列化表单值来创建对象数组(名称和值). 您可以选择一个或多个表单元素(比如 input 及/或 textarea),或者 form 元素本身. ...
- ASP.Net在web.config中设置上传文件的大小方法
修改Webcong文件:<system.web><httpRuntime maxRequestLength="40960" //即40MB,1KB=1024u ...
- 返回顶部 fixed oncheck(点击按钮)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring MVC 数据绑定流程
DataBinder 数据绑定 入参,校验,格式化, ConversionService: 进行数据类型转换和数据格式化 Validators 进行数据合法性的校验, 把结果放入BindingResu ...
- install命令
install 1.作用 install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户. 2.格式 (1)install [选项]... 来源 目的地 (2)install [选项].. ...
- sysbench相关
Sysbench工具是集系统测试和数据库测试一体的测试工具,但是传统的sysbench在数据库测试方面,没有遵循TPC-C测试模型,仅仅支持单个表的数据.而在实际的业务场景中,业务逻辑复杂的多.开源的 ...