LeetCode Power of Three
原题链接在这里: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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- LeetCode Power of Four
原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...
- Leetcode Power of Two
Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...
- 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 ...
- 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 ...
- LeetCode——Power of Two
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
- [LeetCode]Power of N
题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...
随机推荐
- ccc animation
cc.Class({ extends: cc.Component, properties: { sheepAnim: { default: null, type: cc.Animation } }, ...
- XIII Open Cup named after E.V. Pankratiev. GP of SPb
A. Graph Coloring 答案为$1$很好判,为$2$只需要二分图染色,对于$3$,首先爆搜哪些边要染成第$3$种颜色,然后二分图染色判定即可. B. Decimal Fraction 枚举 ...
- HDU1518 Square(DFS,剪枝是关键呀)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- 【HDU】2138 How many prime numbers
http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...
- strace命令跟踪进程
在实际系统维护过程中,常常需要知道一个进程在做哪些动作,比如想判断一个进程是否hang,我们可以使用strace命令,此命令式用来跟踪一个进程在调用哪些系统函数和信号 通过跟踪xinetd进程演示st ...
- C#文字样式
[字体] 中文名称 英文名称宋体 SimSun黑体 SimHei微软雅黑 Microsoft YaHei微软正黑体 Microsoft JhengHei新宋体 NSimSun新细明体 PMin ...
- windows与linux之间传输文件
1.使用SSH Secure Shell Client 百度网盘 下载地址 http://pan.baidu.com/s/1kTmp00J 2.使用pscp 百度网盘地址:http://pan.bai ...
- 纪念逝去的岁月——C/C++快速排序
快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- JDBC的事务处理
JDBC的事务处理 事务,也是数据库事务,指的是作为单个逻辑工作单元执行的一系列操作.正常的情况下,操作应该顺利进行,与操作相关的所有数据库信息也成功地更新: 但是,如果在这一系列过程中任何一个环节出 ...
- KindEditor用法介绍
KindEditor是一套很方便的html编译器插件.在这里做一个简单的使用介绍. 首先在官网上下载最新的KindEditor文件(里面有jsp,asp等不同版本文件夹,可以删掉你不需要的版本), 把 ...