当前位置:网站首页>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));
}
};边栏推荐
- Troubleshooting method of CPU surge
- socket.io搭建分布式Web推送服务器
- Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
- Tencent internship interview sorting
- Leetcode the smallest number of the rotation array of the offer of the sword (11)
- Yolov5系列(一)——網絡可視化工具netron
- Global and Chinese market of air cargo logistics 2022-2028: Research Report on technology, participants, trends, market size and share
- Tensor ellipsis (three points) slice
- What is machine reading comprehension? What are the applications? Finally someone made it clear
猜你喜欢

百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式

Matplotlib drawing label cannot display Chinese problems

Idea does not specify an output path for the module

Reentrantlock usage and source code analysis

mysql innodb 存储引擎的特性—行锁剖析

Chapter 04_ Logical architecture

Can‘t connect to MySQL server on ‘localhost‘

Byte practice plane longitude 2

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

Kubernetes帶你從頭到尾捋一遍
随机推荐
Kubernetes vous emmène du début à la fin
Incluxdb2 buckets create database
Halcon与Winform学习第一节
使用JMeter对WebService进行压力测试
Yolov5 advanced nine target tracking example 1
4-29——4.32
Dataframe returns the whole row according to the value
Jvm-03-runtime data area PC, stack, local method stack
Enable multi-threaded download of chrome and edge browsers
视觉上位系统设计开发(halcon-winform)-3.图像控件
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
[daily training] 395 Longest substring with at least k repeated characters
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
The state does not change after the assignment of El switch
Explanation of time complexity and space complexity
Chapter 04_ Logical architecture
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
[pytorch learning notes] transforms
Jvm-02-class loading subsystem
【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention