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. (zxing.net)二维码PDF417的简介、实现与解码

    一.简介 二维码PDF417是一种堆叠式二维条码.PDF417条码是由美国SYMBOL公司发明的,PDF(Portable Data File)意思是“便携数据文件”.组成条码的每一个条码字符由4个条 ...

  2. “全栈2019”Java多线程第十四章:线程与堆栈详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  3. python 将列表嵌套字典的unicode字符串转换为str格式的字符串的方法

    今天在进行django开发的过程中遇到了一个非常棘手的问题, 因为需求原因, 需要将一份数据存为json格式到数据库中, 如下面这种格式: list_1 = [{"name":&q ...

  4. npm 查看express版本

    npm list 名称    eg: npm list express

  5. HDU-6125-Friend-Graph-2017CCPC网络赛(图论,拉姆齐定理-组合数学)

    Friend-Graph Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. sort函数详解(史上最完整QAQ)

    1.sort 使用:#include <algorithm>   using namespace std; 作用:排序 时间复杂度:n*lg(n) 实现原理:sort并不是简单的快速排序, ...

  7. SUSE Linux Enterprise Server设置IP地址、网关、DNS

    说明: ip:202.118.83.247 子网掩码:255.255.255.0 网关:202.118.83.2 dns:8.8.8.8 / 8.8.4.4 1.设置ip $ vi /etc/sysc ...

  8. 语音转文字小工具开发Python

    # -*- coding: utf- -*- import requests import re import os import time from aip import AipSpeech fro ...

  9. golang (4) golang 操作mongdb

    1. 数据按照时间聚合操作 1.1 正常的数据结构 { "_id" : ObjectId("5cac8d7b1202708adf5d4b64"), " ...

  10. android:activity知识点

    一.活动流程 1.创建活动 public class firstActivity extends Activity{} 2.创建布局 新建first_layout.xml文件 3.注册活动 在andr ...