当前位置: 首页 > news >正文

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.


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.


给一个字符串s,包含了从0-9的数字。输入一个字符串t,要求判断t[j]和s[j](j∈[0,N))的每一位是否相等,如果相等,就看做是Bulls;如果不等,那么就看t[j]是否等于s[i],其中i!=j,如果等,就看做是cows。


1. 本题可以用哈希表来做,第一遍遍历,将guess和secret的每一位比较,直接就能得出bulls的数量。同时统计s[i]的每一位出现的次数,存入哈希表hash中。
2. 如果bulls等于s的长度,说明secret和guess完全匹配,直接返回bulls即可。
3. 遍历guess的每一位,如果guess在hash中出现并且hash[guess[i]]大于0,则hash[guess[i]]--,并统计cows的数量。
4. 最后返回bulls和cows的数量即可。




实现代码:



public class Solution {
    public string GetHint(string secret, string guess) 
    {
        var bulls = 0;
    	var hash = new Dictionary<char,int>();
    	for(var i = 0;i < secret.Length; i++){
    		if(guess[i] == secret[i]){
    			bulls++;
    		}
    		if(!hash.ContainsKey(secret[i])){
    			hash.Add(secret[i], 1);
    		}
    		else{
    			hash[secret[i]]++;
    		}
    	}
    	
    	if(bulls == secret.Length){
    		return string.Format("{0}A0B",bulls);
    	}
    	
    	var guessed = 0;
    	for(var i = 0;i < guess.Length; i++){
    		if(hash.ContainsKey(guess[i]) && hash[guess[i]] > 0){
    			hash[guess[i]] --;
    			guessed ++;
    		}
    	}
    	
    	guessed -= bulls;
    	
    	return string.Format("{0}A{1}B", bulls , guessed);
    }
}


相关文章:

  • 如何修改Windows CE的平台类型
  • LeetCode -- Game of Life
  • 黄舒骏,改变1995
  • LeetCode -- H-Index II
  • vim插件 ctags 和 taglist 的安装和使用
  • LeetCode -- Longest Increasing Subsequence
  • LeetCode -- Serialize and Deserialize Binary Tree
  • Ubuntu中用apt安装和卸载软件
  • LeetCode -- Single Number III
  • Linux 常用C函数(内存及字符串操作篇2)
  • LeetCode -- Subsets II
  • WCDMA与CDMA2000网络结构比较
  • LeetCode -- Integer to English Words
  • WiMAX组网技术与解决方案
  • LeetCode -- Sum Root to Leaf Numbers
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • android图片蒙层
  • chrome扩展demo1-小时钟
  • JS学习笔记——闭包
  • JWT究竟是什么呢?
  • maven工程打包jar以及java jar命令的classpath使用
  • MySQL用户中的%到底包不包括localhost?
  • SwizzleMethod 黑魔法
  • Tornado学习笔记(1)
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 翻译--Thinking in React
  • 分享几个不错的工具
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 前端面试题总结
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • 正则与JS中的正则
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 交换综合实验一
  • $GOPATH/go.mod exists but should not goland
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (二开)Flink 修改源码拓展 SQL 语法
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (过滤器)Filter和(监听器)listener
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (强烈推荐)移动端音视频从零到上手(下)
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (一)SpringBoot3---尚硅谷总结
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)Sublime Text3配置Lua运行环境
  • (转)程序员疫苗:代码注入
  • .NET HttpWebRequest、WebClient、HttpClient
  • .NET Remoting学习笔记(三)信道
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • /etc/X11/xorg.conf 文件被误改后进不了图形化界面