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的更多相关文章

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

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

  2. 299. Bulls and Cows - LeetCode

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

  3. LeetCode 299 Bulls and Cows

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

  4. 299. Bulls and Cows

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

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

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

  6. [leetcode]299. Bulls and Cows公牛和母牛

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

  7. 299 Bulls and Cows 猜数字游戏

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

  8. [LC] 299. Bulls and Cows

    Example 1: Input: secret = "1807", guess = "7810" Output: "1A3B" Expla ...

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

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

随机推荐

  1. Promise自定义,看我如何征服你

    自定义代码 这里是我自定义的Promise,如果想看原版,可以跳过最后有符合PromiseA+规范的源码 class 承诺 { constructor(处理器函数) { //1. 处理器函数是在_ne ...

  2. Python中迭代循环使用比较多的range函数的作用

    range函数用于生成一个不可变的数字序列可迭代对象,类型为range,该数字序列通常用于在 for 循环中循环指定的次数. 具体可参考:<Python中与迭代相关的函数>的详细介绍 老猿 ...

  3. 第8.32节 Python中重写__delattr__方法捕获属性删除

    一. 引言 上节介绍了__delattr__方法在Python清除实例属性时被捕获执行,本节结合例子介绍重写__delattr__方法,并说明__delattr__方法执行的触发逻辑. 二. 重写__ ...

  4. PyQt(Python+Qt)学习随笔:Model/View架构概述

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 模型-视图-控制器(Model-View-Controller,简称MVC)是一种源于Sm ...

  5. Azure Cosmos DB (五) .Net Core 控制台应用

    一,引言 之前在讲Azure CosmosDB Core(SQL)核心的时候,使用了EF Core 的方式,引用了 "Microsoft.EntityFrameworkCore.Cosmos ...

  6. windows cmd 链接远程mysql服务器

    我要链接的mysql 所在ip:192.168.0.110    用户名:root  密码:123 那么: 打开cmd 输入: mysql -h192.168.0.110 -uroot -p123 即 ...

  7. 软件测试相关术语(测试策略 && 测试方案 ....)

    软件测试有几种不同的定义方法: a.软件测试是为了发现程序中的错误而执行程序的过程. b.软件测试是根据软件开发各阶段的规格说明和程序的内部结构而精心设计的一批测试用例,并运用这些测试用例运行程序,以 ...

  8. 小心使用 Task.Run 续篇

    关于前两天发布的文章:为什么要小心使用 Task.Run,对文中演示的示例到底会不会导致内存泄露,给很多人带来了疑惑.这点我必须向大家道歉,是我对导致内存泄漏的原因没描述和解释清楚,也没用实际的示例证 ...

  9. 【转载】Django,学习笔记

    [转自]https://www.cnblogs.com/jinbchen/p/11133225.html Django知识笔记   基本应用 创建项目: django-admin startproje ...

  10. 标注工具labelimg和labelme

    矩形标注工具:labelimg 多边形标准工具:labelme 前者官网发布了可执行文件,后者只有python源码,如果需要编译windows exe,可以这样: pip install labelm ...