LeetCode299. Bulls and Cows

LeetCode 299 Bulls and Cows(公牛和母牛)(HashMap) 翻译你在和朋友们玩一个叫做“公牛和母牛”的游戏:你写下一组数字,然后让你的朋友来猜它。每次你朋友做一个猜测,你根据他的猜测给一个提示:他在数字在值和位置上都猜对的数字,就叫做bulls(公牛),猜对了值但位置不对的数字叫做cows(母牛)。你的朋友将使用各种猜测和提示最终猜出来正确的数字。例如: 你给的秘密数字是:“1807” 朋友的猜测是:“7810”提示:1个公牛和3个母牛。(公牛是8,母牛 阅读详情
题目链接:

https://leetcode.com/problems/bulls-and-cows/

题目描述:

猜数字游戏,两个字符串,secret与 guess,将guess与secret比较,找出guess中,有多少是数字正确且位置正确的,有多少是数字正确位置错误的。

Secret number: “1807”
Friend’s guess: “7810”
return “1A3B”

Secret number: “1123”
Friend’s guess: “0111”
return “1A1B”

题目分析:

A代表数字正确且位置正确的个数
B代表数字正确但位置不正确的个数

两次遍历。

第一次找数字正确且位置正确的(剩下的要么是数字正确但位置不正确的,要么是数字不正确位置也不正确的),用一个数组记录guess中与secret不同的字符。

第二次遍历就在剩下两种情况中找数字正确但位置不正确的,记录数组如果表示存在secret中字符,那么就是数字正确但位置不正确。

代码:
class Solution {
public:
    string getHint(string secret, string guess){
        int A=0,B=0;
        int len=secret.size();
        int num[10]={0};
        for(int i=0;i<len;i++){
            if(secret[i]==guess[i]){
                A++;
            }
            else{
               num[guess[i]-'0']++;
            }
        }
        for(int i=0;i<len;i++){
           if(secret[i]!=guess[i] && num[secret[i]-'0']>0){
               num[secret[i]-'0']--;
               B++;
           }
        }
        char result[128] = {0};  
        sprintf(result, "%dA%dB", A, B);  
        return result;
    }
};
Halcon-AOI外观缺陷检测系列 阅读详情

相关推荐

Linux与Windows双系统,安装完linux后无法通过grub进入Windows的解决方法

先装Windows 10,Windows可正常启动,再装Kali,Kali提示装grub后照装。然而装完后重启发现无Windows选项。

sakurashockwave的博客 6454

LeetCode: 299. Bulls and Cows

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 ma...

杨领well的专栏 332

内网环境下 OpenSSL 与 OpenSSH 安全升级实战指南:从 9.9p2 离线部署到回滚方案

本文详细介绍了内网环境下OpenSSL与OpenSSH的安全升级实战指南,重点涵盖从9.9p2版本离线部署到回滚方案的全流程。通过备份策略、动态库处理、权限修复等关键步骤,确保升级过程安全可靠,特别适合金融、政务等敏感行业的内网服务器安全加固。

weixin_30566063的博客 511

Leetcode299 Bulls and Cows

Leetcode299 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 ma

weixin_43796689的博客 247

国民技术N32G45X低功耗问题之更改全局变量和栈在RAM中的地址

最近项目中用的MCU是国民技术的N32G45X,MCU的资源很丰富,RAM也很大,配套的库和例程也比较全,用起来还是不错的,但是在调试低功耗的时候还是费了不少劲。 首先由于项目低功耗需要,只能选择停机模式2(STOP2)。根据数据手册的描述,在此模式下外部低速时钟开启, RTC运行, R-SRAM保持,所有I/O状态保持,独立看门狗处于关闭状态,25℃的条件下电流为10个微安。 用的这款MCU的RAM是144KB,分为SRAM(128KB)和R-SRAM( Retention SRAM – 16KB),如果

狂想家 2485

citespace基本操作

聚类分析 时间线图 这些结果都是高被引文献的相关呈现结果,代表该领域的文献基础 下面可以看看该project的前沿领域 选择keywords 和term go! 调整显示方式 生成凸线图:表示 在某个研究领域或者时间段内 ,大家对某个领域或者某个词的关注度突然增高,凸显前沿方向的转变 以此来发现热点 ...

生信小博士的博客 1万+

【Leetcode】299-bulls-and-cows【Java实现】【hash table】

最近有空就刷一下leetcode,299题自己的方法居然要50ms+,都不好意思贴出来。在discussion中看到了一个hash的方法。学习了一下,很巧妙,加油吧! stem: You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and as

ALiTuTu的专栏 2135

【Bug已解决】Codex CLI 报 stream disconnected before completion 解决方案

方案适用场景推荐指数更新根证书库社区验证的最常见根因⭐⭐⭐⭐⭐检查系统时间虚拟机/容器等特殊运行环境⭐⭐⭐⭐核对 base_url 配置排除证书问题后的下一步排查⭐⭐⭐⭐更换网络环境测试怀疑本地网络设备存在干扰⭐⭐⭐升级到最新版本已知版本缺陷的场景⭐⭐⭐⭐报错虽然表现形式很像网络连接问题,但社区排查的多个真实案例表明,很大一部分情况的根因其实是本机系统的 TLS 证书信任链异常或系统时间不准确,而不是单纯的网络质量问题。优先检查并更新系统根证书库,这是社区验证过的高频根因。

一键难忘的博客 8016

Leetcode 299 | Bulls and cows | C++

class Solution { public: int inside(int a , std::vector<int> v) { for (int i = 0; i < v.size(); i++) { if (a == v[i]) return i; } return -1; } .

weixin_36629430的博客 324

智驾车载以太网(18)-switch之报文转发和vlan处理流程

本文详细解析了交换机中报文处理和转发流程:1)通过目标MAC查找转发端口,源MAC创建ARL条目;2)VLAN标签处理包括tagged/untagged帧判断、默认VID标记等5种处理方式;3)完整转发流程包含CRC校验、IQ封装、VLAN和ARL表查询、端口向量计算及优先级队列发送。文中还涉及TCAM修改VLAN、802.1q多种模式处理等细节,省略了流量控制等次要流程。该过程实现了基于MAC和VLAN的高效二层交换。

mitugaoyang1993的博客 42

药物不良反应预测实战指南:从FAERS数据挖掘到PyTorch深度学习模型(含可解释性分析)

【代码】药物不良反应预测实战指南:从FAERS数据挖掘到PyTorch深度学习模型(含可解释性分析)

理论都是虚的,代码才是王道。 721

bulls and cows java_【Leetcode】299-bulls-and-cows【Java实现】【hash table】

最近有空就刷一下leetcode,299题自己的方法居然要50ms+,都不好意思贴出来。在discussion中看到了一个hash的方法。学习了一下,很巧妙,加油吧!stem:You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask yo...

weixin_39782433的博客 187

OpenClaw实现多agent协作研究

本文介绍了如何构建多agent协作系统,通过飞书机器人实现任务分配与执行。系统包含三个agent:research_leader(总调度)、research_content(内容生成)和research_review(内容审核)。详细说明了agent配置方法(包括创建命令、权限设置)、飞书channel绑定方式,以及通过共享目录和会话机制实现协作。最后展示了应用示例,用户只需向leader机器人下达任务,系统即可自动完成内容生成和审核的全流程。

hhue2007的专栏 616

Leetcode 299. Bulls and Cows (Easy) (cpp)

Leetcode 299. Bulls and Cows (Easy) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 528

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

leetcode题集:299. Bulls and Cows

第三天:299. Bulls and Cows 题目大意就是两个字符串,一个秘密字符串,一个猜测字符串,然后比较这两个字符串。如果数字和位置都对应上,则是A类型;如果只是数字对上,位置对不上,则是B类型,问AB类型各有多少。如果重复的是一对多,则只算一个。 题目分析:A类型好求,就是一个循环完事儿了。B类型需要比较两个字符串中相同数字的个数,考虑到每一位都是数字,即数字类型是有限个数,即0...

wx_14678的博客 345

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

【LeetCode】299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/bulls-and-cows/description/ 题目描述: You are playing the follow...

负雪明烛 3061

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Leetcode_299_Bulls and Cows

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/50768550 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

皮斯特劳沃 2167

LeetCode || 299. Bulls and Cows

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 gu

xuxumoi 1070

[leetcode] 299. Bulls and Cows

Description 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 provid...

Learning from the mistakes 356

[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 t

coder 进阶的专栏 1468

LeetCode 299. Bulls and Cows

Description 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 provid...

dong的博客 272

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...

muche001的专栏 294

LeetCode 299 Bulls and Cows

Problem: You are playing the followingBulls and Cowsgame 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 pro...

dilian4850的博客 214

leetcode299. 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

大锅八十小锅四十 380

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...

Howie66的博客 373

LeetCode笔记:299. Bulls and Cows

又一个猜数字的游戏

Minecraft 1191
上一篇: PAT1077 Kuchiguse (20)
下一篇: LeetCode219. Contains Duplicate II
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值