LeetCode || 299. Bulls and Cows

bulls-and-cows 公牛和母牛 带有排行榜的 Bulls and Cows 游戏的 node.js 实现 分叉了优秀的: : 安装 下载并安装 node.js 和 npm 和 mongodb 跑步 转到您将克隆此 repo 的目录并运行: git clone https://github.com/gouthamve/bulls-and-cows.git cd bulls-and-cows npm install mongod & npm start 然后打开网络浏览器并转到 localhost:3001 就可以玩公牛和奶牛了 立即下载

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.

For example:

Secret number:  "1807"
Friend's guess: "7810"
Hint:  1  bull and  3  cows. (The bull is  8 , the cows are  0 ,  1  and  7 .)

Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".

Please note that both secret number and friend's guess may contain duplicate digits, for example:

Secret number:  "1123"
Friend's guess: "0111"
In this case, the 1st  1  in friend's guess is a bull, the 2nd or 3rd  1  is a cow, and your function should return  "1A1B" .

You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

题目分析:

本题即: 计算两个字符串的匹配度。

采用hash的方法计算即可。


耗时4ms.


class Solution {
public:
    string getHint(string secret, string guess) {
        int A[10] = {0};
        int bull = 0, cow = 0;
        
        //一定要先统计bull的数量
        for(int i = 0;i<secret.length();i++)
        {
            if(guess[i] == secret[i])
                bull++;
            else
                A[secret[i] - '0']++;
        }
        
        //再统计cow的数量
        for(int i = 0 ;i<guess.length();i++)
        {
            if(guess[i] != secret[i])
            {
                if(A[guess[i]-'0'] > 0)
                {
                    cow++;
                    A[guess[i]-'0']--;
                }
            }
        }
        
        char tmp[100];
        sprintf(tmp, "%dA%dB", bull, cow);//注意string的初始化方式
        string result(tmp);
        return result;
    }
};




RK3568平台(USB篇)USB基础知识 现象:把USB设备比如Android手机接到PC答1:是USB控制器,是USB控制器内嵌的root hub.答2. windows里已经有了USB的总线驱动程序,接入USB设备后,是"总线驱动程序"知道你是"android phone"提示你安装的是"设备驱动程序"。USB总线驱动程序负责:识别USB设备, 给USB设备找到对应的驱动程序。答3. PC的USB口内部,D-和D+接有15K的下拉电阻,未接USB设备时为低电平USB设备的USB口内部,D-或D+接有1.5K的上拉电阻; 阅读详情

相关推荐

Zabbix监控联想服务器XCC的SNMPv3配置实战

本文详细介绍了如何通过SNMPv3协议配置Zabbix监控联想服务器XCC的实战方法。文章从安全角度阐述了选择SNMPv3的必要性,并分步讲解了在XCC中启用SNMPv3、创建认证用户,以及在Zabbix中导入专用模板、创建主机并关联模板的完整流程,帮助运维人员实现对服务器硬件健康状态的自动化、安全监控。

weixin_29210727的博客 465

LeetCode第 299 题:猜数字游戏(C++)

299. 猜数字游戏 - 力扣(LeetCode) 就是计数就可以了,位置相同数字相同就公牛+1,数字相同但位置不同就母牛+1 class Solution { public: string getHint(string secret, string guess) { int num1 = 0, num2 = 0; unordered_map<int, int> m; for(int i = 0; i < secret.size()

zj 408

Qt快速读取Excel文件

网上很多读取方式都是如下形式,太慢了,打开文件+读取文件要6s以上,实在是受不了! QAxObject excel("Excel.Application"); excel.setProperty("Visible", false); QAxObject *work_books = excel.querySubObject("WorkBooks"); work_boo...

c+c++的博客 2万+

leetcode每日一题-299:猜数字游戏

leetcode每日一题-299:猜数字游戏 链接 猜数字游戏 题目 分析 虽然是一个中等题,但是其实十分的简单.我们只需要先按照最小长度遍历一遍两个字符串的前缀,然后统计一下同位置并且数字相同的结果,然后把这些点标记一下,然后将secret中没有匹配的加入哈希表,遍历guess来统计一下不同位置数字相同的答案即可. 代码 C++ class Solution { public: string getHint(string secret, string guess) {

qq_45826803的博客 5890

脑机接口(BCI)从原理到开发实战 09|SSVEP应用:通过视觉刺激控制设备

本文介绍了稳态视觉诱发电位(SSVEP)脑机接口技术。SSVEP是一种高效的非侵入式BCI范式,当用户注视特定频率闪烁的视觉刺激时,大脑枕叶会产生对应的节律性响应。文章详细阐述了SSVEP的神经机制、刺激范式设计、数据预处理方法,重点讲解了典型相关分析(CCA)这一核心识别算法。相比运动想象和P300范式,SSVEP具有信息传输率高(60-100 bits/min)、信号稳定、无需训练等优势,在高速拼写、设备控制等领域应用广泛。文章还提供了完整的电极选择、滤波、分段等预处理流程,以及CCA和滤波器组CCA的

小明的博客 1350

Anaconda+Pycharm安装

我提前创建好一个自己项目所需的虚拟环境。步骤是:打开终端(cmd)或者 搜索 ”Anaconda Prompt“ 并打开,输入 conda create -n pytorch python=3.9 -c。

Wang_sweet的博客 3231

leetcode刷题:299. 猜数字游戏

题目 你在和朋友一起玩 猜数字(Bulls and Cows)游戏,该游戏规则如下: 写出一个秘密数字,并请朋友猜这个数字是多少。朋友每猜测一次,你就会给他一个包含下述信息的提示: 猜测数字中有多少位属于数字和确切位置都猜对了(称为 “Bulls”, 公牛), 有多少位属于数字猜对了但是位置不对(称为 “Cows”, 奶牛)。也就是说,这次猜测中有多少位非公牛数字可以通过重新排列转换成公牛数字。 给你一个秘密数字 secret 和朋友猜测的数字 guess ,请你返回对朋友这次猜测的提示。 提示的格式为 “

weixin_43795179的博客 2904

F28335 DSP ePWM模块实战:从基础配置到电机控制

本文详细解析了TI TMS320F28335 DSP的ePWM模块,从基础概念、寄存器配置到实战应用。通过代码示例,手把手教你如何配置定时中断、生成PWM波形、实现死区保护与故障联防,并最终完成一个可调速、可换向的直流电机控制项目,是掌握F28335电机控制核心技术的实战指南。

g5h6i的博客 1127

【Leetcode】299. 猜数字游戏

每日一题

想要AC的sjh的博客空间 928

DeepSeek+Dify实战:零代码构建智能SQL生成器

本文详细介绍了如何利用DeepSeek和Dify零代码构建智能SQL生成器,帮助非技术人员通过自然语言查询数据库。文章涵盖从数据准备、知识库设计到Agent配置的全流程,并提供了优化技巧和实际应用案例,显著提升团队数据查询效率。

weixin_29311017的博客 418

1、深入解析OTA/gm - C滤波器架构与设计

本文深入探讨了OTA/gm-C滤波器的架构与设计方法,涵盖了从基本构建模块到高阶滤波器实现的多种技术。重点分析了双积分器环路结构(如Tow-Thomas和KHN滤波器)的工作原理、性能特点及OTA非理想特性对滤波器的影响,并介绍了降低寄生效应、提升动态范围的方法。文章还讨论了基于LC梯形仿真、多环反馈和电流模式的gm-C滤波器设计,以及自动调谐技术在频率稳定性中的应用。最后总结了不同类型gm-C滤波器的优缺点及其适用场景,为高频集成连续时间滤波器的设计提供了全面的技术参考。

fanta的博客 185

《Leetcode每日一题》299.猜数字游戏

关键: 1.to_string转换数字为字符 2.利用字符串以及后++/--,如果找到位置相同的直接A++,否则如果有位置不同但是数字相同的,判断这个时候的nums数组在guess的时候>0或者在secret的时候<0,通过后s++/g--来控制对应的数目。 3.注意数组初始化 class Solution { public: string getHint(string secret, string guess) { int nums[10]={0}; ...

shawnlycheeo的博客 2681

仿真学习系列(四十二):Sigrity仿真之走线阻抗和耦合检查

在高速电路设计中,走线的阻抗和信号线之间的耦合是影响信号完整性的重要因素。设计中可能会出现多种情况,导致阻抗变化或不匹配,例如线宽的变化、跨层分割等。传统的人工检查方法常常不够高效且易忽视细节。借助仿真软件,可以快速定位问题区域,并进行精确分析,从而有效避免或减少信号反射和串扰带来的影响。

案例文章,高速信号 425

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

judadeshu的博客 469

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode-299 Bulls and Cows

题目描述 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 provide a hi...

weixin_30826761的博客 238

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

Yano_nankai的博客 969

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode 299. Bulls and Cows(java)

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

katrina95的博客 352

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

henryzhihua 1474

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

lyh_xd 265

Java实现 LeetCode 299 猜数字游戏

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

南 墙 1万+

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 tha

JackZhangNJU的专栏 760

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

wjheha的博客 222

299. Bulls and Cows

题目: 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 provide a hin...

weixin_30633507的博客 83

[leetcode-299-Bulls and Cows]

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 provide a hint tha...

weixin_34061042的博客 88
上一篇: LeetCode ||326. Power of Three
下一篇: LeetCode || 198. House Robber
xuxumoi
博客等级 码龄16年 16粉丝 151原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值