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. 渗透入门rop

    原文链接:https://blog.csdn.net/guiguzi1110/article/details/77663430?locationNum=1&fps=1 基本ROP 随着NX保护 ...

  2. Django 的JsonResponse 与json

    json

  3. jdk从1.8降到jdk1.7失败

    1.将JAVA_HOME:的路径更改为1.7的相关路径,例如我的:C:\Java\jdk1.7.0_80 2.此时查看版本:Java -version,如果是1.8的版本,就把path路径下的%JAV ...

  4. Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成

    一.新建项目 新建项目,只放置接口,用于暴露 Dubbo 服务接口 public interface GreetingService { String greeting(); } 二.provider ...

  5. 孪生网络入门(上) Siamese Net及其损失函数

    最近在多个关键词(小数据集,无监督半监督,图像分割,SOTA模型)的范畴内,都看到了这样的一个概念,孪生网络,所以今天有空大概翻看了一下相关的经典论文和博文,之后做了一个简单的案例来强化理解.如果需要 ...

  6. Python中的"缝合器"zip函数:将多个可迭代对象组合成一个迭代器

    zip函数将参数中多个可迭代对象中相同序号的元素取出组合成一个元组作为输出列表的一个同样序号的元素,即输出列表的每个元素是一个元组,该元组的元素来源于参数中每个迭代对象的对应序号的元素. 具体可参考: ...

  7. 转:HTTP协议简介与在python中的使用详解

    1. 使用谷歌/火狐浏览器分析 在Web应用中,服务器把网页传给浏览器,实际上就是把网页的HTML代码发送给浏览器,让浏览器显示出来.而浏览器和服务器之间的传输协议是HTTP,所以: HTML是一种用 ...

  8. 第十二章 Python标准库内置模块和包简介

    在<第十章 Python的模块和包>老猿详细介绍了Python模块和包的相关概念,模块和包是Python功能扩展的重要手段,也是Python开放的重要特征.为了提供强大的能力,Python ...

  9. Springboot集成xxl-Job

    一.前言 xxl-job一个轻量级的分布的调度中间件,详情介绍自己到官网看:https://www.xuxueli.com/xxl-job/ 二.客户端(执行器) 引入依赖compile group: ...

  10. Python-Wechaty: 面向所有IM软件的聊天机器人框架

    Author: wj-Mcat Code: python-wechaty 个人开发项目,且行且不易,有感兴趣的朋友可以去给一波关注,你们的支持就是我最大的动力,谢谢大家. Python-wechaty ...