前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >299. 猜数字游戏

299. 猜数字游戏

作者头像
张伦聪zhangluncong
发布于 2022-10-26 10:06:39
发布于 2022-10-26 10:06:39
43500
代码可运行
举报
运行总次数:0
代码可运行

你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜。每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位数字猜对了但是位置不对(称为“Cows”, 奶牛)。你的朋友将会根据提示继续猜,直到猜出秘密数字。

请写出一个根据秘密数字和朋友的猜测数返回提示的函数,用 A 表示公牛,用 B 表示奶牛。

请注意秘密数字和朋友的猜测数都可能含有重复数字。

示例 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
输入: secret = "1807", guess = "7810"

输出: "1A3B"

解释: 1 公牛和 3 奶牛。公牛是 8,奶牛是 0, 17

示例 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
输入: secret = "1123", guess = "0111"

输出: "1A1B"

解释: 朋友猜测数中的第一个 1 是公牛,第二个或第三个 1 可被视为奶牛。

解:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
     public String getHint(String secret, String guess) {
        //secret = "1807", guess = "7810"
        if (secret == null || guess == null || secret.length() != guess.length()) {
            return "";
        }
        int countA = 0;
        int countB = 0;
        int[] count = new int[10];
        for (int i = 0; i < secret.length(); i++) {
            if (secret.charAt(i) == guess.charAt(i)) {
                countA++;
            } else {
                //或者这种也可以Character.getNumericValue(secret.charAt(i));
                int tmp1 = secret.charAt(i) - '0';
                int tmp2 = guess.charAt(i) - '0';
                //出题者加1
                count[tmp1]++;
                //出题者碰到<=0说明猜题者猜对了这个字母一次或者多次
                if (count[tmp1] <= 0) {
                    countB++;
                }
                //猜题-1
                count[tmp2]--;
                //-1后还是>=0说明,出题者出了这个字母一次或多次
                if (count[tmp2] >= 0) {
                    countB++;
                }
            }
        }
        return String.valueOf(countA) + "A" + String.valueOf(countB) + "B";
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【算法题解】 Day14 哈希表
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。
sidiot
2023/08/31
1640
打卡群刷题总结0610——猜数字游戏
链接:https://leetcode-cn.com/problems/bulls-and-cows
木又AI帮
2020/06/17
3990
ACMSGURU 486 - Bulls and Cows
You probably know the game “bulls and cows”. Just in case, we explain the rules. The first player picks a four-digit number with all digits distinct (leading zero is allowed) and keeps it secret. The second player tries to guess the secret number. For each guess, the first player issues a response in the form “n bulls, m cows”. A “bull” is a digit that is present in both the secret and the guess and occurs in the same position in both. A “cow” is a digit that is present in both numbers, but occurs in different positions.
Reck Zhang
2021/08/11
3600
Leetcode 题目解析之 Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
ruochen
2022/01/10
1.3K0
299. 猜数字游戏
你在和朋友一起玩 猜数字(Bulls and Cows)游戏,该游戏规则如下: 你写出一个秘密数字,并请朋友猜这个数字是多少。 朋友每猜测一次,你就会给他一个提示,告诉他的猜测数字中有多少位属于数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位属于数字猜对了但是位置不对(称为“Cows”, 奶牛)。 朋友根据提示继续猜,直到猜出秘密数字。 请写出一个根据秘密数字和朋友的猜测数返回提示的函数,返回字符串的格式为 xAyB ,x 和 y 都是数字,A 表示公牛,用 B 表示奶牛。 xA
编程张无忌
2021/06/01
6840
【LeetCode】超简单!猜数字游戏!
今天分享一个LeetCode题,题号是299,标题是猜数字游戏,题目标签是哈希表,题目难度是简单。
五分钟学算法
2020/03/10
1.2K0
【LeetCode】超简单!猜数字游戏!
LeetCode 299. 猜数字游戏
你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜。 每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位数字猜对了但是位置不对(称为“Cows”, 奶牛)。 你的朋友将会根据提示继续猜,直到猜出秘密数字。
Michael阿明
2020/07/13
3540
LeetCode 299. 猜数字游戏
【Day16】Java算法刷题 [299. 猜数字游戏 ] [1.两数之和] [面试题 01.09. 字符串轮转 ]
解题思路: 题目中给到我们两串数字,一串是secret 另外一串是 guess,分别是游戏的答案 与 参与者猜测的答案。 简单总结一下游戏规则,当我们猜的一串数字中,撞到了答案中某个出现的数字时,有两种情况:
.29.
2022/11/15
3360
【Day16】Java算法刷题 [299. 猜数字游戏 ] [1.两数之和] [面试题 01.09. 字符串轮转 ]
LeetCode —— 299. 猜数字游戏
计算A的数目通过比较guess和secret两个字符串中位置和字符相等的个数得到。
Regan Yue
2023/07/10
3520
漫画:猜数字说公牛母牛的高频面试题
第299题:猜数字(Bulls and Cows)游戏,你写下一个数字让你的朋友猜。每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位数字猜对了但是位置不对(称为“Cows”, 奶牛)。你的朋友将会根据提示继续猜,直到猜出秘密数字。
帅地
2020/03/06
9660
漫画:猜数字说公牛母牛的高频面试题
LeetCode笔记:299. Bulls and Cows
第一步找到bull,也就是数字和位置都正确的个数,这个直接循环比对两个字符串同等位置的数字是否一样就好,为了方便我们先全部转换成数组去比较,比较完了记录下个数,还要记录下有哪些位置的数字是bull,这样第二步找cow的时候就不要再判断了。
Cloudox
2021/11/23
4460
漫画:猜数字说公牛母牛的高频面试题
第299题:猜数字(Bulls and Cows)游戏,你写下一个数字让你的朋友猜。每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位数字猜对了但是位置不对(称为“Cows”, 奶牛)。你的朋友将会根据提示继续猜,直到猜出秘密数字。
程序员小跃
2020/03/12
9360
哈希表问题-LeetCode 146、290、299、300(哈希表,双向链表,最小上升序列)
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作:获取数据 get 和 写入数据 put 。
算法工程师之路
2019/11/26
6020
LeetCode 299 Bulls and Cows
刚开始我的想法是依次获取公牛和奶牛的数量, 但奶牛的判断需要 O(n^2) 的时间复杂度, 后面想到, 用所有匹配的数量 - 公牛的数量就是奶牛的数量, 只需要 O(n) 的时间复杂度和 O(1) 的空间复杂度.
一份执着✘
2018/12/26
7260
Leetcode 299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your s
triplebee
2018/01/12
4130
Array - 299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
ppxai
2020/09/23
3460
❤️创意网页:猜数字游戏
你是否喜欢挑战和推理?那么,猜数字游戏是一个能够让你忙碌的游戏选择。这个简单而又令人兴奋的游戏要求你在规定的次数内猜出一个随机生成的数字。让我们一起来探索这个有趣的游戏,并看看你的直觉和运气能否战胜随机数生成器。
命运之光
2024/03/20
2730
❤️创意网页:猜数字游戏
C语言——猜数字游戏
今天我分享一个小游戏给大家,相信大家都玩过这样一款游戏,给你一个1-100的随机数字,假定给的数字为36,我猜60,就提示猜大了,又接着猜,我猜50,显示猜大了,我猜30显示猜小了.....就这样不断的猜下去。
用户11369558
2024/11/20
1760
LeetCode 0299 - Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.
Reck Zhang
2021/08/11
2640
【C语言】手把手带你用实现猜数字游戏,猜不对直接关机!(搞怪室友版)
    按常规套路,玩游戏前都会有一个简易菜单,让用户选择是否开始游戏,由于现在讲到的知识有限,在这里我们利用函数printf直接做一个菜单     思路:我们需要用户输入一个值来确定是否开始游戏,在这里我们就定为:输入1开始游戏,输入0退出游戏,输入其他值就显示输入错误,请重新选择,后面我们会讲到具体实现     现在我们有了规定就开始动手吧,为了美观,我们将其包装为一个函数menu(),菜单代码如下:
TANGLONG
2024/10/15
2430
【C语言】手把手带你用实现猜数字游戏,猜不对直接关机!(搞怪室友版)
推荐阅读
相关推荐
【算法题解】 Day14 哈希表
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验