博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode简单题目两道(5)
阅读量:5136 次
发布时间:2019-06-13

本文共 2810 字,大约阅读时间需要 9 分钟。

ProblemGiven an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without loops/recursion?Codeclass Solution {public:    bool isPowerOfFour(int num) {        return (num > 0) && ((num & (num - 1)) == 0)  && ((num - 1) % 3 == 0);    }};说明利用了2的指数与本身减1相与为0,以及4的指数减1,必定能整除3;class Solution {public:    bool isPowerOfFour(int num) {        return (num > 0) && ((num & (num - 1)) == 0)  && ((num & 0x55555555));    }};说明利用了2的指数与本身减1相与为0,以及4的指数在16进制中的位置0x55555555,前者确定只有一个1,后者确定这个1肯定是4的指数的位置;
problemYou 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: "1807" 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: "1123" Friend's guess: "0111" In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 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.Codeclass Solution {public:    string int2str(int int_temp)    {        stringstream stream;        stream << int_temp;        return stream.str();    }    string getHint(string secret, string guess) {        if(secret.size() == 0 || guess.size() == 0) {            return 0;        }        int res1 = 0, res2 = 0, tmp;        map
map1, map2; for(int i = 0; i < secret.size(); i++) { if (secret[i] == guess[i]) { res1++; } else { map1[secret[i]]++; map2[guess[i]]++; } } map
::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"; }};一次遍历,不解释,哈哈。

 

转载于:https://www.cnblogs.com/hawk-whu/p/6754803.html

你可能感兴趣的文章
构造者模式
查看>>
http和https的区别
查看>>
Hbuild在线云ios打包失败,提示BuildConfigure Failed 31013 App Store 图标 未找到 解决方法...
查看>>
找到树中指定id的所有父节点
查看>>
今天新开通了博客
查看>>
AS3优化性能笔记二
查看>>
4----COM:a Generative Model for group recommendation(组推荐的一种生成模型)
查看>>
UVA 11137 - Ingenuous Cubrency
查看>>
js阻止事件冒泡的两种方法
查看>>
Java异常抛出
查看>>
74HC164应用
查看>>
变量声明和定义的关系
查看>>
Wpf 之Canvas介绍
查看>>
linux history
查看>>
jQuery on(),live(),trigger()
查看>>
Python2.7 urlparse
查看>>
sencha touch在华为emotion ui 2.0自带浏览器中圆角溢出的bug
查看>>
【架构】Linux的架构(architecture)
查看>>
ASM 图解
查看>>
Date Picker控件:
查看>>