[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 ...
随机推荐
- UnityEditorWindow做一个TimeLine的滑动块
UnityEditorWindow做一个TimeLine的滑动块 最近在做一个基于TimeLine的动画编辑器,在制作TineLine滑动条时遇到问题,网上查了好久,试了好多GUI组件都不满意.最后在 ...
- 通过sql 语句将多行数据拼接到一行中
- Cys_Control(一) 项目搭建
一.基础工程搭建 Cys_Controls Cys_Resource(注:一般类库默认不能引入资源文件,故直接创建Custom Control Library) Cys_Demo 删除默认文件夹及类, ...
- 不是程序员,代码也不能太丑!python官方书写规范:任何人都该了解的 pep8
不是程序员,代码也不能太丑!python官方书写规范:任何人都该了解的 pep8 简介:为什么要强调 书写规范 ?这其实并不关乎"美丑",而是为了 更高的效率(代码阅读.开发.维护 ...
- Python中__new__方法为什么有人说是构造方法?有什么作用?
__new__方法是Python新式类引入的,通常用于控制生成一个新实例的过程.它是类级别的静态方法,是在创建实例对象前执行,如果自定义类中没有重写该方法,则Python自动调用父类的方法,当所有父类 ...
- 第十七章、Model/View开发:QListView的功能及属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 QListView是从QAbstractItemView 派生的类,实现了QAbstrac ...
- PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象的dockOptions属性
dockOptions属性代表主窗口对浮动部件停靠的反应,其类型为枚举类型QMainWindow.DockOptions.相关取值及含义如下: 这些枚举值可以组合使用,仅控制如何在QMainWindo ...
- Oracle命令管理账户和权限
方式一.登陆数据库SQL PLUS: 步骤:Oracle - OraDb10g_home1 =>应用程序开发=>SQL PLUS 用户名:system 密码:tiger/admin 退出数 ...
- Google浏览器PostMan插件版安装步骤
PostMan插件版安装步骤: 第一步:把下载后的.crx扩展名的离线Chrome插件的文件扩展名改成.zip或者.rar 第二步:右键点击该文件,并使用压缩软件(如winrar.360压缩等)对该压 ...
- pyinstaller---将py文件打包成exe
pyinstaller可将Python脚本打包成可执行程序,使在没有Python环境的机器上运行. 1.pyinstaller在windows下的安装 直接在命令行用pip安装 pyinstaller ...