[leetcode299] 299. Bulls and Cows
public String getHint(String secret, String guess) {
/*
判断bull 是通过比较两个字符串的每一位,每次相同就删除该字符出现的次数,因为后边的
要判断cow不能占用这些
判断cow只要检测出去被占用的还有没有该字符
*/
//key记录字符,value记录出现的次数
Map<Character,Integer> map = new HashMap<>();
String res = "";
int a = 0;
int b = 0;
boolean[] boo = new boolean[secret.length()];
for (int i = 0; i < secret.length(); i++) {
char cur = secret.charAt(i);
int num = map.getOrDefault(cur,0);
num++;
map.put(cur,num);
}
//判断bull和cow不能一起,要先判断bull,因为如果在判断cow的过程中会占用,会影响bull
for (int i = 0; i < guess.length(); i++) {
char cur = guess.charAt(i);
if (cur==secret.charAt(i))
{
a++;
boo[i] = true;
map.put(cur,map.get(cur)-1);
}
}
for (int i = 0; i < guess.length(); i++) {
char cur = guess.charAt(i);
if (!boo[i]&&map.containsKey(cur)&&map.get(cur)>0)
{
b++;
map.put(cur,map.get(cur)-1);
}
}
res = a+"A"+b+"B";
return res;
}
[leetcode299] 299. Bulls and Cows的更多相关文章
- 【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
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- [leetcode]299. Bulls and Cows公牛和母牛
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- [LC] 299. Bulls and Cows
Example 1: Input: secret = "1807", guess = "7810" Output: "1A3B" Expla ...
- Leetcode 299 Bulls and Cows 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
随机推荐
- 自定义orm字段
class MyCharField(models.Field): def __init__(self,max_length,*args,**kwargs): self.max_length = max ...
- windows中flask的环境搭建
之前在ctf中遇到了python模板注入的题,于是就打算学习一下flask框架,它是基于Python的Web轻量级应用框架,与其他框架相比,Flask可以自主选择应用组件,可扩展性强. 安装也简单 第 ...
- CSP-2020 退役记
CSP-2020 游记 第2次参加CSP-- Day -5~-7 每天笔试+机试 Day -8~-9 在家放松(写作业) Day 0 鸡鸭月考 Day 1 9:30以前 愉快的在别人月考的时候离开鸡鸭 ...
- JavaScript使用中的一些小技巧
任何一门技术在实际中都会有一些属于自己的小技巧.同样的,在使用JavaScript时也有一些自己的小技巧,只不过很多时候有可能容易被大家忽略.而在互联网上,时不时的有很多同行朋友会总结(或收集)一些这 ...
- 快速理解Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容的区别
<Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?>老猿介绍了二者的区别,为了快速理解,老猿在此使用另外一种方式补充说明一下: 1.使用%r是调用objec ...
- Python函数学习遇到的问题
Python函数的关键字参数 Python函数独立星号(*)分隔的命名关键字参数 Python函数中的位置参数 Python中对输入的可迭代对象元素排序的sorted函数 Python中函数的参数带星 ...
- PyQt学习随笔:使用QPropertyAnimation开发简单动画
QPropertyAnimation是PyQt5.QtCore模块提供的动画设计类,使用该类可以针对PyQt的界面对象进行动画播放,如果要针对一个指定对象进行动画播放,包括如下步骤: 一.创建动画对象 ...
- 简单且实用的关闭当前应用的auto.js 代码
function closeCurrentPackage() { // 可以稍加修改,关闭指定app let packageName = currentPackage(); app.openAppSe ...
- CSP-S 初赛最后的复习
2020CSP-S 模拟赛1 3.一个圆形水池中等概率随机分布着四只鸭子,那么存在一条直径,使得鸭子全在直径一侧的概率是(). A.\(\frac 1{16}\) B.\(\frac 1{8}\) C ...
- Vue开发中的移动端适配(px转换成vw)
1.项目根目录下,创建 .postcssrc.js 文件. 2.安装插件. -D (开发依赖) postcss-import postcss-url cssnano-preset-advanced - ...