[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 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.
代码:
class Solution {
public:
string getHint(string secret, string guess) {
int aCount = ;
int bCount = ;
vector<int> secretVec(,);
vector<int> guessVec(,);
if(secret.empty())
return "0A0B";
for(int i = ; i < secret.size(); ++i)
{
char c1 = secret[i];
char c2 = guess[i];
if(c1 == c2)
{
++aCount;
}
else
{
++secretVec[c1-''];
++guessVec[c2-''];
}
}
for(int i = ; i < secretVec.size(); ++i)
{
bCount += min(secretVec[i],guessVec[i]);
}
return to_string(aCount) + "A" + to_string(bCount) + "B";
}
};
[LeetCode299]Bulls and Cows的更多相关文章
- [Swift]LeetCode299. 猜数字游戏 | Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
- Bulls and Cows leetcode
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
随机推荐
- Android比较字符串是空的(isEmpty)
通常情况下,我们需要去推断一个字符串变量是否为空,今天,我特意做了一个小测试 StringUtils.java: package com.yx.equipment_collection.utils; ...
- poj3295 Tautology , 计算表达式的值
给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...
- 2014ACM/ICPC亚洲区西安站 F题 color (组合数学,容斥原理)
题目链接:传送门 题意: n个格子排成一行.我们有m种颜色.能够给这些格子涂色,保证相邻的格子的颜色不同 问,最后恰好使用了k种颜色的方案数. 分析: 看完题目描写叙述之后立刻想到了一个公式 :C(m ...
- 所有javax包
所有jar包 > javax javax 下载 javax jar 包 本站下载镜像: javax-jmi-model.jar.zip javax-jmi-reflect.jar.zip jav ...
- 怎样使用docker不加sudo
有时候发现docker有的指令必须加sudo才干运行,通过下面三步设置便可不加sudo直接运行docker指令: 1. 假设还没有docker group就加入一个: sudo groupadd do ...
- sort功能使用
头: #include <algorithm> using namespace std; 1.默认的sort函数是按升序排序. sort(a,a+n); / ...
- 用XAML做网页!!—导航栏
原文:用XAML做网页!!-导航栏 这次要完成的是导航栏,这是页面中比较复杂的区域. 先在 Microsoft Expression Design 中绘制导航栏的背景图案: 导出为barback.xa ...
- •当对自己的技术足够自信的时候,拼的就是RP和面试技巧了(转)
前言 三月真是一个忙碌的季节,刚刚开学就需要准备各种面试和笔试(鄙视).幸运的是,在长达一个月的面试内推季之后,终于同时拿到了阿里和腾讯的offer,还是挺开心的.突而想起久未更新的博客,就冒昧学一学 ...
- SVN常见问题及解决方案
. 隐藏文件.svn目录删除了怎么办 Checkout后,工作空间下.svn目录下有大量隐藏文件,占用比较大的空间,他们是工作空间的管理文件,不能删除,如果不小心删除了也不要抓狂,不会影响服务器端的, ...
- 安装pygame
pygame的安装 我们首先要去到:http://www.pygame.org/download.shtml 下载我们所需要的软件包: 我选择的是:pygame-1.9.2a0.win32-py3.2 ...