leetcode简单题目两道(5)
Problem Given an integer (signed bits), write a function to check whether it is a power of . Example: Given num = , return true. Given num = , return false. Follow up: Could you solve it without loops/recursion? Code class Solution {
public:
bool isPowerOfFour(int num) {
return (num > ) && ((num & (num - )) == ) && ((num - ) % == );
}
};
说明 利用了2的指数与本身减1相与为0,以及4的指数减1,必定能整除3; class Solution {
public:
bool isPowerOfFour(int num) {
return (num > ) && ((num & (num - )) == ) && ((num & 0x55555555));
}
};
说明 利用了2的指数与本身减1相与为0,以及4的指数在16进制中的位置0x55555555,前者确定只有一个1,后者确定这个1肯定是4的指数的位置;
problem 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. Example: Secret number: "" 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: "" Friend's guess: "0111" In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 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. Code class Solution {
public:
string int2str(int int_temp)
{
stringstream stream;
stream << int_temp;
return stream.str();
}
string getHint(string secret, string guess) {
if(secret.size() == || guess.size() == ) {
return ;
}
int res1 = , res2 = , tmp;
map<char, int> map1, map2;
for(int i = ; i < secret.size(); i++) {
if (secret[i] == guess[i]) {
res1++;
} else {
map1[secret[i]]++;
map2[guess[i]]++;
}
}
map<char,int>::iterator it;
for(it=map1.begin();it!=map1.end();++it)
{
if (it->second < map2[it->first]) {
tmp = it->second;
} else {
tmp = map2[it->first];
}
res2 += tmp;
}
return int2str(res1) + "A" + int2str(res2) + "B";
}
};
一次遍历,不解释,哈哈。
leetcode简单题目两道(5)的更多相关文章
- leetcode简单题目两道(2)
Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...
- leetcode简单题目两道(4)
心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...
- leetcode简单题目两道(3)
本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...
- leetcode简单题目两道(1)
Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...
- CTF中关于XXE(XML外部实体注入)题目两道
题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...
- 两道面试题,带你解析Java类加载机制
文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...
- 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)
本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...
- 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester
这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
随机推荐
- CHARPTER 3--INDEX DMVs
1.查找最重要的缺失的索引 --======================================================= --查找最重要的缺失的索引 ) DB_NAME() AS ...
- Buffer Pool--SQL Server:Memory Manager 对象
--=================================================================SELECT * FROM sys.sysperfinfoWHER ...
- gitignore 使用
在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...
- LINQ to Entities 查询中的标准查询运算符
投影和筛选方法 投影指的是转换的结果集到所需的窗体中的元素. 例如,可以从结果集中的每个对象投影所需的属性子集,可以投影一个属性并对其执行数学计算,也可以从结果集投影整个对象. 投影方法有 Selec ...
- OpenSSH服务及其相关应用
远程登录工具: telnet,TCP/23:认证明文,数据传输明文,不够安全,所以出现了ssh ssh:Secure SHell,TCP/22,刚开始免费,后来商业化了,所以出现了Openssh,这个 ...
- sqlserver数据库存储汉字出现?
问题:有些相对复杂的汉字在数据库里会变成? 解决办法:原来数据类型是varchar,将数据类型修改为nvarchar
- 字体变色详解链接:https://www.cnblogs.com/daofaziran/p/9015284.html
格式:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m 字体色 | 背景色 | 颜色描述 ------------------------------------------- 30 ...
- 配置sql server 允许远程连接
如果要想远程连接数据库那么则需要在一个局域网中或一个路由器中才可以做到 接下来就是具体的操作检查sqlserver数据库是否允许远程连接 具体操作为 (1)打开数据库,用本地帐户登录,右击第一个选项, ...
- css细节复习笔记——基本视觉格式化
css包含如此开放.如此强大的一个模型,对于这样一个模型,可以有无数种方法结合应用各种属性,可以得到的效果数不胜数. 基本框 css假定每个元素都会生成一个或多个矩形框,这称为元素框.各元素中心有一个 ...
- 【OCP-12c】CUUG 071题库考试原题及答案解析(24)
24. choose the best answer In the EMPLOYEES table there are 1000 rows and employees are working in t ...