原题链接在这里:https://leetcode.com/problems/power-of-three/

题目:

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?

题解:

检查能否被3整除,然后整除,看能否一直除到1.

Time Complexity: O(logn).

Space: O(1).

AC Java:

 public class Solution {
public boolean isPowerOfThree(int n) {
if(n<=0){
return false;
}
while(n%3 == 0){
n /= 3;
}
return n==1;
}
}

Follow up 不用 loop. 整数范围内最大的3的幂数, 3^19 = 1162261467能否被n整除.

Time Complexity: O(1). Space: O(1).

AC Java:

 public class Solution {
public boolean isPowerOfThree(int n) {
return n>0 && 1162261467%n==0;
}
}

类似Power of Two.

最新文章

  1. 关于Intent的七大重要属性
  2. spring注解scheduled实现定时任务
  3. 在ubuntu下安装QQ
  4. HTTP权威指南阅读笔记三:HTTP报文
  5. linux diff具体解释
  6. CocoaPods的一些理解
  7. scala中的implict
  8. [RxJS] Using Observable.create for fine-grained control
  9. jQuery拖动调整表格列宽度-resizableColumns
  10. Java中实现Serializable接口为什么要声明serialVersionUID?
  11. 由一个简单需求到Linux环境下的syslog、unix domain socket
  12. 工程启动加载.properties/.xml配置文件
  13. gp工具的许可
  14. influxdb
  15. nginx配置文件注释说明
  16. 自定义控件详解(三):Canvas效果变换
  17. Linux之路,起步虽晚,迈步才会成功(2013.08.09)
  18. 解决CentOS7-python-pip安装失败
  19. /var/log/messages Logging not working on Centos 7
  20. 6.capacity scheduler

热门文章

  1. iOS之09-特有语法
  2. BZOJ 2427 &amp; 分块裸题
  3. BZOJ 1086 &amp; 类树的分块
  4. 一道常被人轻视的前端JS面试题(转)
  5. 20145304 第五周Java学习报告
  6. ACM: CodeForces 140A New Year Table-数学几何
  7. NOIP欢乐模拟赛 T1 解题报告
  8. 【BZOJ】1105: [POI2007]石头花园SKA
  9. 【POJ】3974 Palindrome
  10. Android --RatingBar的使用