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)的更多相关文章

  1. leetcode简单题目两道(2)

    Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...

  2. leetcode简单题目两道(4)

    心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...

  3. leetcode简单题目两道(3)

    本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...

  4. leetcode简单题目两道(1)

    Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...

  5. CTF中关于XXE(XML外部实体注入)题目两道

    题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...

  6. 两道面试题,带你解析Java类加载机制

    文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...

  7. 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)

    本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...

  8. 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester

    这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...

  9. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

随机推荐

  1. MVC v5.1 Preview 包含 web api 2.1 web pages 3.1

    Includes ASP.NET MVC 5.1, Web API 2.1, and Web Pages 3.1 preview release. This was released marked a ...

  2. CentOS6.3安装MySQL5.5

    1.查看系统是否安装了MySQL 使用命令: #rpm -qa | grep mysql 2.卸载已安装的MySQL 卸载mysql命令如下: #rpm -e --nodeps  mysql-libs ...

  3. 【node错误】/usr/bin/env: node: No such file or directory

    背景 安装了node后,执行npm run xxx的命令的时候,报错,提示如下: /usr/bin/env: node: No such file or directory 步骤 1. 什么玩意,执行 ...

  4. how to remote debug in vs 2013

    first download the debugger tools "rtools_setup_x64" start C:\Program Files\Microsoft Visu ...

  5. Java50道经典习题-程序10 自由落体

    题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? import java.util.Scanner; public cl ...

  6. GO语言官方中文教程!

    官方中文教程网址:https://tour.go-zh.org/basics/1 推荐理由:简洁,一句废话没有,对于初学者可以让大家快速掌握GO语言! 注意问题:如果不能访问,你懂的! 教程截图:

  7. BZOJ 1801--中国象棋(DP)

    1801: [Ahoi2009]chess 中国象棋 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1995  Solved: 1160[Submit] ...

  8. python中mysql的存储

    1. 连接mysql import pymysql db = pymysql.connect(host=', port=3306) cursor = db.cursor() cursor.execut ...

  9. 部署LVS-NAT群集

    案例环境 LVS调度器作为Web服务器池的网关,LVS两块网卡,分别连接内外网,外网地址172.16.16.172.24,同时也作为整个群集的VIP,内网地址为192.168.7.21-24/24,是 ...

  10. 读取Properties文件的六种方法

    1.使用java.util.Properties类的load()方法 示例: InputStream in = new BufferedInputStream(new FileInputStream( ...