当前位置:网站首页>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));
}
};边栏推荐
- [transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
- 基于SVN分支开发模式流程浅析
- Tensor ellipsis (three points) slice
- Yolov5系列(一)——網絡可視化工具netron
- Byte practice surface longitude
- [probably the most complete in Chinese] pushgateway entry notes
- Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share
- 运维体系的构建
- Didi off the shelf! Data security is national security
- Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Kubernetes 进阶训练营 Pod基础

视觉上位系统设计开发(halcon-winform)-3.图像控件

redis缓存穿透,缓存击穿,缓存雪崩解决方案

Troubleshooting method of CPU surge
![[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer](/img/9b/6ca8375ef8689a80d437665909ae30.png)
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer

Byte practice surface longitude
![[probably the most complete in Chinese] pushgateway entry notes](/img/5a/6dcb75f5d713ff513ad6842ff53cc3.png)
[probably the most complete in Chinese] pushgateway entry notes

Jvm-02-class loading subsystem

5.4-5.5

4-29——4.32
随机推荐
【日常训练】395. 至少有 K 个重复字符的最长子串
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
Matplotlib drawing label cannot display Chinese problems
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
[probably the most complete in Chinese] pushgateway entry notes
Class part2
Basic SQL tutorial
Jvm-05-object, direct memory, string constant pool
Zero copy underlying analysis
Puppet automatic operation and maintenance troubleshooting cases
Summary of concurrent full knowledge points
The first character of leetcode sword offer that only appears once (12)
TPS61170QDRVRQ1
[set theory] inclusion exclusion principle (complex example)
Besides lying flat, what else can a 27 year old do in life?
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
Using multipleoutputs to output multiple files in MapReduce
redis单线程问题强制梳理门外汉扫盲
【pytorch学习笔记】Datasets and Dataloaders