原题链接在这里: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.

LeetCode Power of Three的更多相关文章

  1. [LeetCode] Power of Four 判断4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  2. [LeetCode] Power of Three 判断3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  3. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  4. LeetCode Power of Four

    原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...

  5. Leetcode Power of Two

    Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...

  6. Leetcode Power of two, three, four

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  7. leetcode power(x,n)

    class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...

  8. LeetCode——Power of Two

    Description: Given an integer, write a function to determine if it is a power of two. public class S ...

  9. [LeetCode]Power of N

    题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...

随机推荐

  1. Unity 逐步旋转

    npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, Quaternion.LookRotation(moveDir), ...

  2. Codeforces Round #247 (Div. 2) B - Shower Line

    模拟即可 #include <iostream> #include <vector> #include <algorithm> using namespace st ...

  3. webstorm常用快捷键(常用)

    ctrl+/ 注释 ctrl+shift+/ 注释一块的代码 ctrl+shift+z 返回撤撤销前的操作 ctrl+shift+up/down 代码向上/向下移动 ctrl+b或ctrl+鼠标左键单 ...

  4. SRM 615 DIV1 500

    TC 都615了...时间过的真快啊. 第一次做出500分,心情还是很激动的,虽然看了很久的题解,TC官网上的题解,很详细,但是英语的...我搜了搜,发现一份日语的...好吧,我还是看看英语的吧... ...

  5. FMS直播流发布时 Microphone Speex 编码设置注意事项

    1.为何要用 Speex?FP的默认音频编码是 NellyMoser,而FP10之后加入了 Speex.实际应用中,用默认的 NellyMoser 编码音频,会有个很大的问题,就是无法控制流码率浮动. ...

  6. Oralce中SQL删除重复数据只保留一条(转)

    用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 .查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select ...

  7. 20145330第九周《Java学习笔记》

    20145330第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JD ...

  8. XML参考 :XmlReader 详解、实例

    XML参考 :XmlReader 详解.实例-- 详解 转:http://www.cnblogs.com/Dlonghow/archive/2008/07/28/1252191.html XML参考 ...

  9. asp.net分页方法

    /// </summary> /// <param name="ds">DataSet实例</param> /// <param name ...

  10. 做为一名dba你应该知道这些数据恢复

    1.将备份数据   拉取到本地虚拟机上 进行恢复(千万不要把数据直接恢复到生产中,除非迫不得已!!)   2.在本地虚拟机上恢复之后,导出需要恢复的数据.   3.在本地虚拟机上恢复做恢复测试.   ...