[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 ...
随机推荐
- 2、Spring Cloud和dubbo简介
1.Spring Cloud简介 (1).Spring Cloud简介 SpringCloud,基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负 ...
- Kubernetes Ingress-nginx使用
目录 简介 1. 部署Ingress-Controller 2. 使用Ingress规则 2.1 Ingress地址重写 2.2 配置HTTPS 2.3 黑白名单配置 2.4 匹配请求头 2.5 速率 ...
- 【2020.11.30提高组模拟】删边(delete)
删边(delete) 题目 题目描述 给你一棵n个结点的树,每个结点有一个权值,删除一条边的费用为该边连接的两个子树中结点权值最大值之和.现要删除树中的所有边,删除边的顺序可以任意设定,请计算出所有方 ...
- vue绑定用户页面
1.vue微博回调空页面 微博回调空页面为: http://127.0.0.1:8888/oauth/callback/ 1.1 页面路径 components\oauth.vue <templ ...
- PyQt学习随笔:Qt Designer的Edit Buddies功能
在Qt Designer的Edit菜单下有个Edit Buddies(编辑伙伴关系)子菜单,该菜单的作用是将一个Label与其他控件关联起来,当Label控件设置了快捷键时,通过Label的快捷键就可 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中部件的mouseTracking和tabletTracking属性
在Qt Designer中的部件属性设置中,有mouseTracking和tabletTracking两个属性,这两个属性是跟踪鼠标或平板触控笔的移动轨迹的. 一.mouseTracking mous ...
- centos7最小安装后——网络配置、常见命令安装,远程连接、yum源安装软件包
安装环境 #软件:vmware 14 #centos版本:CentOS-7-x86_64-DVD-1810 下载地址: #网络配置:NAT模式 配置 网络配置 #动态获取ip: centos7最小安装 ...
- linux 复制文件前n行到另外一个文件
head -n 100 train.txt > 123.txt head -100 train.txt > 123.txt
- Oracle函数:trunc、round、ceil和floor
1.trunc函数 1).trunc(date) 格式:trunc(date,fmt) trunc用于截取时间,即便你指定不同的格式类型,返回的类型始终都是时间类型. 示例: with dates a ...
- Java 线程安全问题的本质
原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 目录: 线程安全问题的本质 理解CPU JVM虚拟机类比于操作系统 重排序 汇总 一些解释 ...