当前位置:网站首页>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));
}
};
边栏推荐
- 视觉上位系统设计开发(halcon-winform)-5.相机
- Besides lying flat, what else can a 27 year old do in life?
- el-switch 赋值后状态不变化
- Kubernetes帶你從頭到尾捋一遍
- App global exception capture
- Halcon与Winform学习第二节
- What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
- Chapter 14 class part 1
- 【可能是全中文网最全】pushgateway入门笔记
- Kubernetes will show you from beginning to end
猜你喜欢
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
Byte practice plane longitude 2
Halcon与Winform学习第二节
Didi off the shelf! Data security is national security
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
Kubernetes帶你從頭到尾捋一遍
What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
Dataframe returns the whole row according to the value
Kubernetes 进阶训练营 Pod基础
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
随机推荐
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
The first character of leetcode sword offer that only appears once (12)
Redis主从、哨兵、集群模式介绍
[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
Introduction, use and principle of synchronized
Qt常用语句备忘
Troubleshooting method of CPU surge
视觉上位系统设计开发(halcon-winform)
406. Reconstruct the queue according to height
Explanation of time complexity and space complexity
Global and Chinese market of optical fiber connectors 2022-2028: Research Report on technology, participants, trends, market size and share
Redis single thread problem forced sorting layman literacy
Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
Search in the two-dimensional array of leetcode sword offer (10)
Yolov5 series (I) -- network visualization tool netron
Byte practice plane longitude 2
Yolov5 advanced 8 format conversion between high and low versions
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
5-1 blocking / non blocking, synchronous / asynchronous