当前位置:网站首页>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));
}
};边栏推荐
- socket. IO build distributed web push server
- SQL server installation location cannot be changed
- Class part2
- 百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
- MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
- Troubleshooting method of CPU surge
- Global and Chinese market of air cargo logistics 2022-2028: Research Report on technology, participants, trends, market size and share
- Search in the two-dimensional array of leetcode sword offer (10)
- The method of parameter estimation of user-defined function in MATLAB
- SQL server安装位置改不了
猜你喜欢

解决pushgateway数据多次推送会覆盖的问题

视觉上位系统设计开发(halcon-winform)-5.相机

Halcon与Winform学习第一节

Characteristics of MySQL InnoDB storage engine -- Analysis of row lock

Pytoch deep learning and target detection practice notes

Functional modules and application scenarios covered by the productization of user portraits

运维体系的构建

视觉上位系统设计开发(halcon-winform)

Tencent internship interview sorting

什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
随机推荐
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Kubernetes advanced training camp pod Foundation
App全局异常捕获
Analysis of development mode process based on SVN branch
Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
[pytorch learning notes] transforms
视觉上位系统设计开发(halcon-winform)-5.相机
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share
Functional modules and application scenarios covered by the productization of user portraits
What is label encoding? How to distinguish and use one hot encoding and label encoding?
Yolov5系列(一)——網絡可視化工具netron
Halcon与Winform学习第二节
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
5.4-5.5
Didi off the shelf! Data security is national security
Detailed comments on MapReduce instance code on the official website
Using multipleoutputs to output multiple files in MapReduce
Introduction, use and principle of synchronized