LeetCode Bulls and Cows (简单题)
题意:
给出两个数字,输出(1)有多少位是相同的(2)有多少位不在正确的位置上。
思路:
扫一遍,统计相同的,并且将两串中不同的数的出现次数分别统计起来,取小者之和就是第2个答案了。
class Solution {
public:
string getHint(string secret, string guess) {
const int N=;
int *cnt1=new int[N];memset(cnt1,,sizeof(int)*N);
int *cnt2=new int[N];memset(cnt2,,sizeof(int)*N);
int right=, wrong=;
for(int i=; i<secret.size(); i++)
{
if(secret[i]==guess[i]) right++;
else
{
cnt1[secret[i]-'']++;
cnt2[guess[i]-'']++;
}
}
for(int i=; i<N; i++)
wrong+=min(cnt1[i],cnt2[i]);
return to_string(right)+"A"+to_string(wrong)+"B";
}
};
AC代码
LeetCode Bulls and Cows (简单题)的更多相关文章
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- LeetCode Majority Element(简单题)
题意: 给一个数组,其中有一个元素的出现次数已经超过数组的一半大小,请找出这个元素? 思路: 可以扫一遍数组,将这个出现次数过多的元素抵消其他的元素,最后必定留下1个以上的元素,就是它自己了. pyt ...
- LeetCode Ugly Number (简单题)
题意: 判断是一个数的质因子仅含有2,3,5这3个. 思路: 因子2比较容易解决,num/=num-(num&num-1)就可以了.3和5的需要通过循环来另判. C++ class Solut ...
- LeetCode Missing Number (简单题)
题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. int missing ...
- LeetCode Valid Anagram (简单题)
题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序 ...
- LeetCode Move Zeroes (简单题)
题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- leetcode笔记:Bulls and Cows
一. 题目描写叙述 You are playing the following Bulls and Cows game with your friend: You write down a numbe ...
随机推荐
- JSP生成条形码
下载barcode4j-2.0-bin.zip 目前最新版本是2.0, 解压barcode4j-2.0-bin.zip, 将其中的\lib\avalon-amework-4.2.0.jar和\bui ...
- netty4 连通步骤
转载:http://xw-z1985.iteye.com/blog/1973205 服务端依次发生的步骤 建立服务端监听套接字ServerSocketChannel,以及对应的管道pipeline: ...
- 亿级Web系统搭建——单机到分布式集群[转]
当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...
- intel vt-x处于禁用状态下如何处理
1.首先看你的bios选项里面有没有该选项,如果没有就更新,更新之后还没有,则不支持 2.找到intel Virtualization Technology 将状态改为Enabled 同时找到int ...
- mysql创建PATH快捷
1.使其临时生效 PATH=$PATH:/usr/local/mysql/bin 2.永久生效 编辑/etc/profile 添加第79列 然后source /etc/profile 3.输入命令m ...
- 我们无法找到服务器加载工作簿的数据模型"的 SharePoint 网站,当您刷新 Excel 2013 工作簿中的数据透视表时出错
假定您使用 Analysis Services 源在 Microsoft Excel 2013 中创建数据透视表.将 Excel 工作簿上载到 Microsoft SharePoint 网站中.当您尝 ...
- node.js 对接公众平台
http://www.tfan.org/wp-content/uploads/使用-Nodejs-和-MongoDB-开发高性能微信公众平台应用.pdf
- 李明杰视频 SVN
李明杰视频 SVN 就10-12使用技术SVN 源代码会引发哪些问题? 无法后悔:做错一个操作 版本备份:费控件,费时间 版本混乱:因版本备份太多造成混乱 代码冲突:多人操作同一文件 强烈建议 使用源 ...
- bzoj 1818: [Cqoi2010]内部白点
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struc ...
- (转载)重新对APK文件签名
1.将证书(debug.keystore)复制到与需要重新签名的apk文件相同的目录下(如:复制到D:\Sign) 2.在cmd中切换到需要重新签名的apk文件的目录下 3.使用WinRAR打开要重新 ...