[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 ...
随机推荐
- RBAC相关的配置
一.什么是RBAC 基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注. 在RBAC中,权限与角色相关联,用户通 ...
- iframe引入百度地图显示企业位置
步骤一:打开下面这个地址:http://api.map.baidu.com/lbsapi/creatmap/index.html 步骤二:定位中心点 在打开的页面左侧,输入企业的详细地 ...
- ossim 4.1 安装
在这说明一下,本人也是第一次安装这东东很多我自己也不懂,只能把自己的安装步骤通过贴图呈现出来了!!!! 我用root用户登录的,顺便查看ip 在浏览器打开网址https://192.168.86.15 ...
- MONGODB用户、角色和权限管理
最近遇到很多问MONGODB用户认证授权方面的问题,现在特记录下来,与大家共享. 一.概念理解 1.用户概念 Mongodb的用户是由 用户名+所属库名组成 例如: 登录mongo testdb ...
- 很详细的curl命令使用大全
可以看作命令行浏览器 1.开启gzip请求 curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间 cu ...
- hive 下篇
由于spark on hive 问题,导致无法插入数据,暂时使用spark进行hive操作 向分区表插入数据 hive> show partitions customers;OKpartitio ...
- 【独家】完美解决appium安装app时,需要手动确认安装的问题
appium初始化driver时,如果未安装该app会先进行安装,安装时,很多安卓手机都会弹框,需要手动确认安装. 如小米的机器, 这是个头疼的问题,之前在网上找遍了,只有通过adb去点相对坐标成功了 ...
- JAVA JDBC 各大数据库的连接字符串和连接类
oracle: driverClass:oracle.jdbc.OracleDriver url:jdbc:oracle:thin:@127.0.0.1:1521:dbname mys ...
- DB2 to mysql
1.安装DB2 create database testdb connect to testdb 2. 将DB2 ixf格式into DB2 IMPORT FROM /tabxxx.IXF OF IX ...
- Activity服务类-8 RuntimeService服务类
一共89个接口1.启动流程实例(20个方法)//使用给定的键在流程定义的最新版本中启动一个新的流程实例.ProcessInstance startProcessInstanceByKey(String ...