326. Power of Three

Easy

Given an integer, write a function to determine if it is a power of three.

Example 1:

Input: 27
Output: true

Example 2:

Input: 0
Output: false

Example 3:

Input: 9
Output: true

Example 4:

Input: 45
Output: false

Follow up:
Could you do it without using any loop / recursion?

package leetcode.easy;

public class PowerOfThree {
public static void main(String[] args) {
PowerOfThree sol = new PowerOfThree();
int iterations = 1000000; // See table header for this value
long startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree1(i);
}
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间 startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree2(i);
}
endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间 startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree3(i);
}
endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间 startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree4(i);
}
endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
} public boolean isPowerOfThree1(int n) {
if (n < 1) {
return false;
} while (n % 3 == 0) {
n /= 3;
} return n == 1;
} public boolean isPowerOfThree2(int n) {
return Integer.toString(n, 3).matches("^10*$");
} public boolean isPowerOfThree3(int n) {
return (Math.log10(n) / Math.log10(3)) % 1 == 0;
} public boolean isPowerOfThree4(int n) {
return n > 0 && 1162261467 % n == 0;
} @org.junit.Test
public void test() {
System.out.println(isPowerOfThree1(27));
System.out.println(isPowerOfThree1(0));
System.out.println(isPowerOfThree1(9));
System.out.println(isPowerOfThree1(45));
System.out.println(isPowerOfThree2(27));
System.out.println(isPowerOfThree2(0));
System.out.println(isPowerOfThree2(9));
System.out.println(isPowerOfThree2(45));
System.out.println(isPowerOfThree3(27));
System.out.println(isPowerOfThree3(0));
System.out.println(isPowerOfThree3(9));
System.out.println(isPowerOfThree3(45));
System.out.println(isPowerOfThree4(27));
System.out.println(isPowerOfThree4(0));
System.out.println(isPowerOfThree4(9));
System.out.println(isPowerOfThree4(45));
}
}

最新文章

  1. Java多线程同步 synchronized 关键字的使用
  2. Bash漏洞批量检测工具与修复方案
  3. java定时任务实现
  4. Chrome RenderText分析(2)
  5. hdu3966 树链剖分+成段更新
  6. linux自定义脚本添加到rc.local脚本无法正常运行的问题
  7. MINIX3 系统任务分析
  8. 安装rlwrap错误的问题解决方法
  9. C# 总结
  10. Android Studio Gradle 缓存目录设置
  11. html 块状元素 行内元素 内联元素
  12. js调用父框架函数
  13. linux下PHP 环境搭建
  14. [LeetCode] Couples Holding Hands 两两握手
  15. ORA-00471: DBWR process terminated with error案例
  16. 登录获取token,token参数关联至所有请求的请求体内
  17. python设计模式第六天【原型模式】
  18. python---memcache使用操作
  19. shiro中记住我功能
  20. setTimeout设置为0的意义

热门文章

  1. 08 node.js 的使用
  2. 语法速学,返回数组 repeated修饰符
  3. 2019-2020-1 20199302《Linux内核原理与分析》第一周作业
  4. C# 遍历 enum 枚举
  5. 洛谷P5506 封锁
  6. BFC、IFC、FFC、GFC
  7. VMware虚拟机找不到USB设备该怎么办?
  8. elasticsearch routing
  9. 分布式缓存Redis集群搭建
  10. 2018-2019-2 《网络对抗技术》Exp9 WebGoat 20165326