LeetCode(65)-Power of Four
题目:
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?
思路:
- 题意是判断一个32位的符号整数是不是4的次方
- 对于2的次方的判断是n&(n-1)== 0
10 => 2
100 => 4
1000 => 8
10000 => 16
100000 => 32
1000000 => 64
10000000 => 128
100000000 => 256
1000000000 => 512
10000000000 => 1024
100000000000 => 2048
1000000000000 => 4096
10000000000000 => 8192
100000000000000 => 16384
由图中观察可以看出来,4的次方,1都在从右往左数的奇数位,1,3,5等
所有从2的次方移除4的次方,与上01010101010101010101010101010101,十六进制是0x555555555
代码:
public class Solution {
public boolean isPowerOfFour(int num) {
return num > 0 && (num&(num -1)) == 0 && (num & 0x55555555) != 0;
}
}
LeetCode(65)-Power of Four的更多相关文章
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- C#版 - Leetcode 65. 有效数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- [LeetCode] 342. Power of Four 4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- leetcode:Power of Two
Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- LeetCode 342. Power of Four (4的次方)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- [LeetCode] Reordered Power of 2 重新排序为2的倍数
Starting with a positive integer N, we reorder the digits in any order (including the original order ...
- [LeetCode] 326. 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 ...
随机推荐
- Eclipse中设置VM参数
eclipse.ini -Xms256m //设置堆最小值 -Xmx1024m //设置堆最大值 Eclipse 做JVM 的分析时,需要动态设置JVM的参数来进行各种测试, 可以在下图地方进行设置 ...
- RocketMQ,JStorm与Tair使用笔记
关于RocketMQ 启动mq nohup sh mqnamesrv -n 10.150.0.94:9876 & nohup sh mqbroker -n 10.150.0.94:9876 ...
- Android打包遇到的那些坑
说说今天打包遇到的坑,由于线上有个支付的bug需要紧急修复,而我们的项目又没有使用热修复,所以只能通过编译打包等传统流程,还好android上线比较快. 说说我进早上打包遇到的几个问题吧,首先我使用b ...
- LInux last命令详解
命令简介: 该命令用来列出目前与过去登录系统的用户相关信息.指令英文原义:show listing of last logged in users 执行权限 :有些需要特殊权限 指令所在路径: ...
- android之View绘制
Android系统的视图结构的设计也采用了组合模式,即View作为所有图形的基类,Viewgroup对View继承扩展为视图容器类,由此就得到了视图部分的基本结构--树形结构 View定义了绘图的基本 ...
- Tomcat 5.5 JNDI Resource 配置 (tomcat数据源配置)
转自:http://blog.csdn.net/fenglibing/article/details/4528512 Tomcat 5.5 JNDI Resource 配置 Author Blog:h ...
- 【安卓网络请求开源框架Volley源码解析系列】初识Volley及其基本用法
在安卓中当涉及到网络请求时,我们通常使用的是HttpUrlConnection与HttpClient这两个类,网络请求一般是比较耗时,因此我们通常会在一个线程中来使用,但是在线程中使用这两个类时就要考 ...
- 【Unity Shaders】Vertex Magic —— 访问顶点颜色
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- Swift基础之闭包Closure学习
首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...
- Android的事件处理-android学习之旅(四十四)
androd事件处理简介 控制飞机移动的简单实例 package peng.liu.test; import android.app.ActionBar; import android.app.Act ...