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. 热更新(一) 之Lua语法的学习

    热更新 如热更新果需要更换UI显示,或者修改游戏的逻辑,这个时候,如果不使用热更新,就需要重新打包,然后让玩家重新下载(浪费流量和时间,体验不好).热更新可以在不重新下载客户端的情况下,更新游戏的内容 ...

  2. cesium随笔 — 简单实现获取三维范围(包括相机高度)

    代码 // 获取当前三维范围 function getCurrentExtent() { // 范围对象 var extent = {}; // 得到当前三维场景 var scene = viewer ...

  3. DotNet三层架构

    [项目结构]DAL 数据访问层.BLL 业务逻辑.UI 表现层 Model 数据.Common 常用工具[引用关系]DAL --> Model CommonCommon --> 谁都不引用 ...

  4. qi zi

    #include<stdio.h> ]; ][]; int N; typedef struct node{ int x; }node; node dui[]; int se(int a) ...

  5. 在线编辑器Ckeditor (2) - php (31)

    接上一篇 3 in-page(页内)配置,在使用Ckeditor的界面里进行直接配置 页内配置 效果 特点:配置项完全属于某个特定的Ckeditor实例,不可重用 三种配置方式比较 定制方式 特点 说 ...

  6. 洛谷P1742 最小圆覆盖(计算几何)

    题面 传送门 题解 之前只是在抄题解--这篇才算是真正自己想的吧-- 首先我们把输入序列给\(random\)一下防止出题人好心送你一个毒瘤序列 我们设\(r\)为当前最大半径,\(o\)为此时对应圆 ...

  7. linuxea:ELK5.5-elasticsearch-x-pack破解

    本站采用知识共享署名-非商业性使用-相同方式共享国际许可协议4.0 进行许可 本文作者:www.linuxea.com for Mark 文章链接:https://www.linuxea.com/17 ...

  8. Elasticsearch(9):使用Logstash-input-jdbc同步数据库中的数

    1.数据同步方式 全量同步与增量同步 全量同步是指全部将数据同步到es,通常是刚建立es,第一次同步时使用.增量同步是指将后续的更新.插入记录同步到es. 2.常用的一些ES同步方法 1). elas ...

  9. 敏感词过滤的算法原理之DFA算法

    参考文档 http://blog.csdn.net/chenssy/article/details/26961957 敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有 ...

  10. 实例的初始化由JVM装载类的时候进行,保证了线程的安全性

    在23种设计模式中,单例是最简单的设计模式,但是也是很常用的设计模式.从单例的五种实现方式中我们可以看到程序员对性能的不懈追求.下面我将分析单例的五种实现方式的优缺点,并对其在多线程环境下的性能进行测 ...