326. Power of Three
来源:互联网 发布:各大电商平台双11数据 编辑:程序博客网 时间:2024/06/03 22:05
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
这里可以用log的换底公式,logab = logb/loga。代码如下:
public class Solution { public boolean isPowerOfThree(int n) { double num = Math.log10(n) / Math.log10(3); if (num % 1 == 0) { return true; } else { return false; } }}这里如果用Math.log的话,测试集243结果是4.99999999,这跟计算机组成原理有关。
也可以用下面这个思路,
public class Solution { public boolean isPowerOfThree(int n) { // 1162261467 is 3^19, 3^20 is bigger than int return ( n>0 && 1162261467%n==0); }}
0 0
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- teamviewer 客户端没有侧边栏 没发选择机器怎么办
- 旋转的参数
- 杀掉mysql进程之后,我通宵了一整个晚上
- 接口回调Demo
- 基础12
- 326. Power of Three
- 导android-support-v4.jar.properties源码
- KiCad设计PCB-1-元器件库的制作(1)
- 分治递归之奶牛过河问题
- WCF学习笔记1
- asp.net面试题
- net面试宝典
- 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法
- PHP面试经典