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. 题意:判断一个数是否是 ...
随机推荐
- 如何真正提高ASP.NET网站的性能
摘要:前言 怎么才能让asp.net网站飞得更快,有更好的性能?这是很多开发者常常思考的一个问题.我有时候会做大量的测试,或请求别人帮忙采集一些数据,希望能够验证网上一些专家的建议或证明 前言 怎么才 ...
- 使用CSS/JS代码修改博客模板plus
之前对CSS/JavaScript了解还不深,只是把模板的CSS胡乱修改了几个属性.最近正好也在做一个网站的前端,学习了不少东西,再来改一改~ 上次最后之所以铩羽而归,是因为从CSS里找不到那些#和. ...
- ccc animation
cc.Class({ extends: cc.Component, properties: { sheepAnim: { default: null, type: cc.Animation } }, ...
- BZOJ3189 : [Coci2011]Slika
通过离线将操作建树,即可得到最终存在的操作. 然后逆着操作的顺序,倒着进行染色,对于每行维护一个并查集即可. 时间复杂度$O(n(n+m))$. #include<cstdio> cons ...
- sdoi 2009 & 状态压缩
是不是平时在手机里玩吃豆豆游戏玩腻了呢?最近MOKIA手机上推出了一种新的围豆豆游戏,大家一起来试一试吧. 游戏的规则非常简单,在一个N×M的矩阵方格内分布着D颗豆子,每颗豆有不同的分值Vi.游戏者可 ...
- 关于NSNotificationCenter消息通信用法
NSNotificationCenter主要用于广播消息到多个监听着,其传统用法 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotification ...
- Linux远程传输命令之scp使用方法
首先用pwd命令确定文件全路径 1.获取远程服务器上的文件 cykdeMacBook-Pro:~ cyk$ scp cyk@10.211.55.5:/home/cyk/Desktop/hi.t ...
- 期望dp BZOJ3450+BZOJ4318
BZOJ3450 概率期望DP f[i]表示到i的期望得分,g[i]表示到i的期望长度. 分三种情况转移: ① s[i]=‘x’:f[i]=f[i-1],g[i]=0 ② s[i]=‘o’:f[i]= ...
- 三元表达式、逻辑表达式 与 &&、||的妙用
var a = "123", b = 123; console.log(a === b && "相等" || "不相等"); ...
- 用js读写cookie的简单办法
/* 功能:保存cookies函数 参数:name,cookie名字:value,值 */ function SetCookie(name,value){ var Days = 30*12; //co ...