原题链接在这里: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. 创建第一个Hiberntae工程

    一.前言 很久之前已经对Hibernate有所了解,在项目中进行过简单的应用,基本了解hibernate的简单应用,没有深入的了解,来Shine公司快三个月了,公司的ORM框架就是用Hiberante ...

  2. win7 删除Windows服务的方法

    http://www.jb51.net/os/windows/25090.html 一.什么是Windows服务 Windows服务也称为Windows Service,它是Windows操作系统和W ...

  3. AFNetworking 2.0 出现Use of undeclared identifier AFURLSessionManager错误

    当向下面使用时会出现错误 #import "AFNetworking.h" #import "AFURLSessionManager.h" AFURLSessi ...

  4. [Leetcode] Merge Intevals

    Question: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3], ...

  5. ubuntu13.04下安装jdk7

    参考http://www.neversaydie.cc/ubuntu-install-jdk-in-detailed/ 而来 1.手工从Oralce官网下载jdk-7u25-linux-x64.gz  ...

  6. 【BZOJ】1535: [POI2005]Sza-Template

    题意 给一个串\(s(1 \le |s| \le 500000)\),求一个最长的串,使得这个串能覆盖整个串(可以重叠). 分析 首先这个串肯定是前缀也肯定是后缀. 题解 对串kmp后,建立\(fai ...

  7. iOS 三种收起键盘的方法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  8. win7 64位DCOM配置(关于导出excel 配置计算机组件服务)(转)

    http://blog.sina.com.cn/s/blog_9323b3a50101qrxm.html [解决方案1] 1:在服务器上安装office的Excel软件. 2:在"开始&qu ...

  9. ios修改产品名

    在创建项目的时候,会设置一个项目名,以后生成的APP名字也就是这个了,但由于某种原因,我想修改APP名字,也就是屏幕程序图标下面显示的那个,这该怎么办呢? 下面有三种方法都可以: 修改Product ...

  10. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...