当前位置:网站首页>leetcode_ Power of Four
leetcode_ Power of Four
2022-07-03 15:16:00 【alw_ one hundred and twenty-three】
Problem description
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?
Credits:
Special thanks to @yukuairoy for adding this problem and creating all test cases.
Ideas
2 The power of contains 4 The power of , in other words 4 The power of must be 2 Power of Dan 2 The power of is not necessarily 4 The power of . Then you might as well calculate whether the input is 2 The power of , when 2 And then judge whether it is 4 The power of .
To judge whether the input is 2 The power of is simple , Just judge whether there is only 1 individual 1 that will do . That is, assume that the input is n, If n&(n-1)==0, be n Namely 2 The power of .
So how to judge whether it is 4 Power of ? Take a chestnut .2 and 8 The binary code of is (0010) and (1000) These two numbers are 2 Power of, but not 4 The power of . and 4 and 16 The binary code of is (0100) and (10000). You can find a rule ,4 In the binary code of the power of 1 Will appear on odd digits ( From right to left ), Instead of 4 In the binary code of the power of 1 Will appear in even digits . That is to say if 1 When it appears on odd digits, it is 4 The power of . To judge , A mask can be introduced (1010101010101010101010101010101) That is to say 0x55555555. then n And this mask bitwise AND , If the result is not 0, Namely 4 The power of .
Realization
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<=0)
return false;
return ((num&(num-1))==0 && (num&0x55555555));
}
};
边栏推荐
- Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share
- Chapter 14 class part 1
- Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
- 视觉上位系统设计开发(halcon-winform)-5.相机
- Mmdetection learning rate and batch_ Size relationship
- 百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
- Unity hierarchical bounding box AABB tree
- Kubernetes - yaml file interpretation
- 使用JMeter对WebService进行压力测试
- Global and Chinese market of postal automation systems 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Kubernetes带你从头到尾捋一遍
Zero copy underlying analysis
百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
Introduction to redis master-slave, sentinel and cluster mode
Finally, someone explained the financial risk management clearly
第04章_逻辑架构
Basic SQL tutorial
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
随机推荐
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Halcon与Winform学习第一节
Kubernetes - yaml file interpretation
5-1 blocking / non blocking, synchronous / asynchronous
The method of parameter estimation of user-defined function in MATLAB
Functional modules and application scenarios covered by the productization of user portraits
Detailed comments on MapReduce instance code on the official website
Jvm-04-runtime data area heap, method area
Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
SQL server installation location cannot be changed
视觉上位系统设计开发(halcon-winform)-6.节点与宫格
Yolov5系列(一)——網絡可視化工具netron
北京共有产权房出租新规实施的租赁案例
Explanation of time complexity and space complexity
Idea does not specify an output path for the module
What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Halcon与Winform学习第二节
Jvm-03-runtime data area PC, stack, local method stack