299. Bulls and Cows - LeetCode
Question
Solution
题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一个cow,注:数字对与位置对优先,一个号码不能重复判断.
思路:构造map结构,遍历实现
Java实现:实现的不漂亮,好歹能通过
public String getHint(String secret, String guess) {
Map<Character, Index> map = new HashMap<>();
for (int i=0; i<secret.length(); i++) {
Index idx = map.get(secret.charAt(i));
if (idx == null) {
idx = new Index();
map.put(secret.charAt(i), idx);
}
idx.add(i);
}
int bulls = 0;
int cows = 0;
List<Character> cowsList = new ArrayList<>(); // for count cows
// count bulls
for (int i=0; i<guess.length(); i++) {
Index idx = map.get(guess.charAt(i));
if (idx != null) { // check digits
if (idx.isBull(i)) {
bulls++;
} else {
cowsList.add(guess.charAt(i));
}
}
}
// count cows
for (char c : cowsList) {
Index idx = map.get(c);
if (idx.isCow()) {
cows++;
}
}
return bulls + "A" + cows + "B";
}
class Index {
List<Integer> idxList;
int count;
// constructor
public Index() {
idxList = new ArrayList<>();
count = 0;
}
void add(int x) {
idxList.add(x);
count++;
}
boolean isCow() {
return count-- > 0;
}
boolean isBull(int x) {
for (int tmp : idxList) {
if (x == tmp) {
count--;
return true;
}
}
return false;
}
}
Ref
https://leetcode.com/problems/bulls-and-cows/discuss/74621/One-pass-Java-solution
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
int s = Character.getNumericValue(secret.charAt(i));
int g = Character.getNumericValue(guess.charAt(i));
if (s == g) bulls++;
else {
if (numbers[s] < 0) cows++;
if (numbers[g] > 0) cows++;
numbers[s] ++;
numbers[g] --;
}
}
return bulls + "A" + cows + "B";
}
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) bulls++;
else {
if (numbers[secret.charAt(i)-'0']++ < 0) cows++;
if (numbers[guess.charAt(i)-'0']-- > 0) cows++;
}
}
return bulls + "A" + cows + "B";
}
299. Bulls and Cows - LeetCode的更多相关文章
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [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
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- Bulls and Cows leetcode
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- [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 ...
随机推荐
- 无单位数字和行高 —— 别说你懂CSS相对单位
前段时间试译了Keith J.Grant的CSS好书<CSS in Depth>,其中的第二章<Working with relative units>,书中对relative ...
- javascript当中嵌套函数
3)嵌套函数例 3.3.1<head> <meta http-equiv="content-type" content="text/html; c ...
- Centos搭建 Git 服务器教程
搭建 GIT 服务器教程 下载安装 git Git 是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 此实验以 CentOS 7.2 x64 的系统为环境,搭建 git 服 ...
- 微信小程序常用表单校验方法(手机号校验、身份证号(严格和非严格校验、验证码六位数字校验))
util.js function isPhone(value) { if (!/^1(3|4|5|7|8)\d{9}$/.test(value)) { return false } else { re ...
- Vue入坑日记: day - 01
前言 最近做了一些小项目,小组里写前端的确实有点拉胯,于是自己动手写前端,因为大一学过web前端基础,所以对HTML,CSS还有印象,就直接对JS下手了,学了两天把JS大致搞明白了,顺便对JQuery ...
- Spring Boot-@EnableWebMvc注解
作用:当配置类中添加了该注解了之后,就表示某个模块的自动配置就都失效了,全部都要自己配置 例如下面这个MVC模块的配置类 /** * @author:抱着鱼睡觉的喵喵 * @date:2020/12/ ...
- 数组-LeetCode-笔试
目录 数组理论基础 二分查找 二分法第一种写法 二分法第二种写法 ACM 移除元素 暴力解法 双指针法(快慢指针) ACM 有序数组的平方 暴力排序 双指针法 长度最小的子数组 暴力解法 滑动窗口 相 ...
- 微信小程序实战,用vue3实现每日浪漫情话推荐~
之前做了个恋爱话术微信小程序,实现高情商的恋爱聊天. 但最近突然发现,每天早上给女朋友发一段优美情话可以让她开心一整天,但无奈自己的语言水平确实有限,不能随手拈来,着实让人有点不爽. 不过办法总比困难 ...
- 面试官:请分析一条SQL的执行
最近一直在写<手撕MySQL系列>文章,我发现自己的切入点有一些问题,虽尝试深入探究MySQL中的一些关键特性,但对于MySQL的知识掌握不太能够形成较好的体系化的知识网络.我感到在对全局 ...
- 都2022年了,HDFS为何还如此能战!
摘要:HDFS也许不是最好的大数据存储技术,但依然是最重要的大数据存储技术. 本文分享自华为云社区<HDFS为何在大数据领域经久不衰?>,作者: JavaEdge. 1.概述 1.1 简介 ...