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 ...
随机推荐
- Div CSS absolute与relative的区别小结
absolute:绝对定位,CSS 写法“ position: absolute; ”,它的定位分两种情况,如下: 1. 没有设定 Top.Right.Bottom.Left 的情况,默认依据父级的“ ...
- ios基础篇(十)——UINavgationController的使用(一)UIBarButtonItem的添加
UINavigationController又被成为导航控制器,继承自UIViewController,以栈的方式管理所控制的视图控制器,下面就详细说一下UINavigationController的 ...
- replace(),indexOf(),substring(),split(),join(),——各种小知识点
1.replace ———— 实现去除指定字符串功能,可以用空字符串代替,也可以去新字符代替已有的字符. var str="123_z.jpg"; str=str.replace( ...
- BZOJ3238 [Ahoi2013]差异
首先把后缀数组和height数组都搞出来... 然后用两个单调栈维护$[l, r]$表示对于一个点$x$,满足$height[x] \le height[l..x] \ \&\&\ ...
- extjs DateField 的值用getValue()方法获取后是一大堆字符串,类似Tue Dec 07 2010 00:00:00 GMT 0800,这玩意存入数据库实在不好办。。。
extjs DateField 的值用getValue()方法获取后是一大堆字符串,类似Tue Dec 07 2010 00:00:00 GMT 0800,这玩意存入数据库实在不好办...所以要把它格 ...
- hduacm 5104
http://acm.hdu.edu.cn/show #include <cstdio> #include <cstring> #include <algorithm&g ...
- [Js]封装好的通过className来获取元素的函数
<div id="box"> <div class="star"></div> <div class="st ...
- C语言中文件的读取和写入
在C语言中写文件 //获取文件指针 FILE *pFile = fopen("1.txt", //打开文件的名称 "w"); // 文件打开方式 如果原来有内容 ...
- VS2010连接SQLite数据库
Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:This is the only setu ...
- Java对象的序列化和反序列化实践
2013-12-20 14:58 对象序列化的目标是将对象保存在磁盘中,或者允许在网络中直接传输对象.对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久的保存 ...