原题链接在这里: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. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  2. Here's what C++ is

    Yes this article describes what c++ exactlyis http://www.skywind.me/blog/archives/1398#comment-3671 ...

  3. Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)

    数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...

  4. 瀑布流 &留言板

    实例:瀑布流 留言板(一)瀑布流瀑布流实现原理分析1.ajax文件内容function ajax(method, url, data, success) {    var xhr = null;   ...

  5. 在DataGridView控件中加入ComboBox下拉列表框的实现

    在DataGridView控件中加入ComboBox下拉列表框的实现 转自:http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.htm ...

  6. Picture effect of JavaScript

    每隔一定时间跟换图片Img = new Array("image/2.jpg","image/1.jpg","image/3.jpg",&q ...

  7. Java log4j详细教程

    Java log4j详细教程 http://www.jb51.net/article/74475.htm

  8. 通过jquery获取ul中第一个li的属性

    当加载列表时,默认希望选中第一条.top_menu 为ul的ID 通过 $("#top_menu li:first") 就可以获取到 ul下第一个li标签.然后就可以利用 例如 修 ...

  9. gerrit使用教程

      注:使用时把“user”替换为自己的账号,例如 ueapp: ssh://huang.fei@10.0.64.16:29418/jonet2_0_app_ueapp.git 新的环境下需要先注册g ...

  10. HTML中忽略的小问题

    1.padding和margin 例子 1 padding:10px 5px 15px 20px;(上,右,下,左) 上内边距是 10px 右内边距是 5px 下内边距是 15px 左内边距是 20p ...