[leetcode]299. Bulls and Cows公牛和母牛

摘要:
Guest=“0111”输出:bulls++使用int[]map=newint[256]构建HashMap的简化版本。这个想法与验证程序类似。当前字符在地图中标记为++p。当前字符标记为--then,=guess。length())returnfalse;5int[]映射=newint[256];8for(inti=0;10charg=guest.charAt(i);

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.

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. 

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.

题意:

猜字游戏。

我负责猜,

你负责给提示:数字和位置都对,bulls++。数字对位置不对,cows++。

[leetcode]299. Bulls and Cows公牛和母牛第1张

思路:

挨个扫字符串s,

挨个扫字符串g

若s当前字符等于g的字符,bulls++

用int[] map = new int[256]建一个简化版的HashMap

思路类似valid anagram

s当前字符在map里标记为++

p当前字符在map里标记为--

那么,若s当前字符已经被标记为--,说明p的字符来标记过,即它们字符相同但位置不同,cow++。

同理,若p当前字符已经被标记为++, 说明s的字符来标记过, 即它们字符相同但位置不同,cow++。

代码:

 1 class Solution {
 2     public String getHint(String secret, String guess) {
 3         // corner 
 4         if(secret.length() != guess.length()) return false;  
 5         int[] map = new int[256];
 6         int bull = 0;
 7         int cow = 0;
 8         for(int i = 0; i < secret.length();i++){
 9             char s = secret.charAt(i);
10             char g = guess.charAt(i);    
11             if(s == g){
12                 bull++;
13             }else{
14                 if(map[s]<0) cow++;
15                 if(map[g]>0) cow++;
16                 map[s]++;
17                 map[g]--;
18             }    
19         } 
20         return bull +"A" + cow + "B";    
21     }
22 }

免责声明:文章转载自《[leetcode]299. Bulls and Cows公牛和母牛》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MongoDB简单的配置及应用纠错《COM技术内幕》之ProgID下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C# 处理base64 以及base64的原理分析

u  博客园 首页 新随笔 联系 订阅 管理 随笔 - 2009  文章 - 1  评论 - 74 C# 处理base64 以及base64的原理分析  base64的原理, http://www.cnblogs.com/diligenceday/p/6002382.html http://www.cnblogs.com/chengxi...

vim编辑器-删除命令

dd:删除游标所在的一整行(常用)   ndd:n为数字。删除光标所在的向下n行,例如20dd则是删除光标所在的向下20行   d1G:删除光标所在到第一行的所有数据   dG:删除光标所在到最后一行的所有数据   d$:删除光标所在处,到该行的最后一个字符   d0:那个是数字0,删除光标所在到该行的最前面的一个字符   x,X:x向后删除一个字符(相...

IOS UIImage类方法总结

IOS中对图片的处理 UIImage 相信做项目时肯定会有用到 UIImage 这个类,那我们就来看一下这个类中都有什么内容。 其实这篇文章就是在看文档的时候想记录一下文档中得方法。 UIImage 继承于NSObject 下面介绍一下UIImage中的方法 首先是我们最常用的 通过图片的文件名来获取这个图片 + (UIImage *)imageNa...

SQL Server:SQL Like 通配符特殊用法:Escape

 SQL Server:SQL Like 通配符特殊用法:Escape   %:匹配零个及多个任意字符; _:与任意单字符匹配; []:匹配一个范围; [^]:排除一个范围 Symbol Meaning like '5[%]' 5% like '[_]n' _n like '[a-cdf]' a, b, c, d, or f like...

(PHP)redis String(字符串)操作

/** * * String操作 * 字符串操作 * */ //设置键值:成功返回true,否则返回false,键值不存在则新建,否则覆盖 $redis->set('string', 'hello world!'); //从左往右第五个字符开始替换为另一指定字符串,成功返回替换后新字符串的长度。 $redis->setRan...

13、ubuntu终端快捷键(参考 dy9776)

1、终端的快捷键 Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+l 相当于clear,即清屏 Ctrl+Z 把当前任务放到后台运行(相当于运行命令时后面加&) Ctrl+d 关闭终端 Ctrl+c 终止进程/命令 Ctrl+r 查找历史命令 Shift+Ctrl+n...