[LeetCode]231. Power of Two&326. Power of Three&342. Power of Four
来源:互联网 发布:iphone硬件检测软件 编辑:程序博客网 时间:2024/06/08 02:15
231 . Power of Two
Easy
Given an integer, write a function to determine if it is a power of two.
2ms:
public boolean isPowerOfTwo(int n) { if(n==0) return false; while(n%2==0){ n /= 2; } return n==1; }
2ms:
public boolean isPowerOfTwo(int n) { return n>0 &&(0==(n&(n-1))); }
326 . Power of Three
Easy
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
23ms:
public boolean isPowerOfThree(int n) { return ( n>0 && 1162261467%n==0); }
31ms:
public boolean isPowerOfThree(int n) { if(n==0) return false; while(n%3==0) n /= 3; return n==1; }
342 . Power of Four
Easy
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
2ms:
public boolean isPowerOfFour(int num) { return num > 0 && (0==(num & (num - 1))) && (num & 0x55555555) == num; }
2ms:
public boolean isPowerOfFour(int num) { int n=0; while(num>0){ int tmp = num&3; if(tmp==3||tmp==2) return false; n+=tmp; num=num>>2; } return n==1; }
0 0
- Leetcode 231. Power of Two & 326. Power of Three & 342. Power of Four
- 【leetcode】326. Power of Three 231. Power of Two 342. Power of Four
- [LeetCode]231. Power of Two&326. Power of Three&342. Power of Four
- leetcode 231. Power of Two & 326. Power of Three & 342. Power of Four
- Power of two,power of three,power of four
- Leetcode Power of Two/Three/Four
- LeetCode 231. Power of Two & 326. Power of Three
- 231./ 326. /342. Power of Four/ Three /Two
- [LeetCode] Power of Three | Power of Two
- [LeetCode]Power of Four/Power of Three/Power of Two(Java)
- 326. Power of Three / 342. Power of Four
- 326. Power of Three ,231. Power of Two ,
- LeetCode 231,326,342 --Power of Two & Three & Four
- [LeetCode]Power of Four
- Power of Four-Leetcode
- LeetCode Power of Four
- LeetCode - Power of Four
- LeetCode:Power of Four
- UITextField 知识整理
- 简单插入排序与希尔排序
- Linux软连接与硬连接
- 零java基础搞定微信Server_4_写一个servlet的DEMO
- 机器视觉开源代码集合
- [LeetCode]231. Power of Two&326. Power of Three&342. Power of Four
- JAVA进阶2.4——finally
- 70. Climbing Stairs
- 使用java语言将数组中的数据写入txt文件,然后读取
- mptcp应用——在nexus5安装mptcp
- Android在代码中控制UI界面
- JSP指令元素
- PHP文件(图像)上传
- Java注释