LeetCode 231 Power of Two AND LeetCode 326 Power of Three
来源:互联网 发布:mac 浏览器 编辑:程序博客网 时间:2024/06/08 22:15
231 Power of Two
给一个数,要求在不用循环不用递归的前提下判断这个是时候是2的某次幂。
根据一个数学公式:
LOG a ( b ) = LOG c ( b ) / LOG c ( a )假如n为2的幂,则log2(n)应为整数,套用上公式,利用log10()函数,写出判断函数:
class Solution {public: bool isPowerOfTwo(int n) { return !fmod(log10(n)/log10(2), 1); }};
326 Power of Three
题意同上题,不过是判断是否为3的幂,同理,使用相同套路。
代码如下:
class Solution {public: bool isPowerOfThree(int n) { return !fmod(log10(n) / log10(3), 1); }};
阅读全文
0 0
- Python库之Requests(iMooc)---(1)
- 菜鸟心历之路(6)
- 【JavaScript】最简单的一个例子
- 《Visual Basic 程序设计》——>基本概念
- String类的常用方法(1)A
- LeetCode 231 Power of Two AND LeetCode 326 Power of Three
- bootstrap table中显示头行结构
- zookeeper和dubbo的关系
- Codeforces Round #427 (Div. 2)(A+B)
- unity 一个拼图demo(七巧板)和一个切割demo—2
- [Visual Studio]MFC游戏开发之回合制对战系统一_by书訢
- 电商项目实战——第一天(环境部署)3搭建vsftpd服务器
- JavaScript-函数之css()
- ZOJ 1134 Strategic Game(树形DP)