当前位置:网站首页>【日常训练--腾讯精选50】231. 2 的幂
【日常训练--腾讯精选50】231. 2 的幂
2022-07-07 11:37:00 【Puppet__】
题目
给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false 。
如果存在一个整数 x 使得 n == 2x ,则认为 n 是 2 的幂次方。
示例 1:
输入:n = 1
输出:true
解释:20 = 1
示例 2:
输入:n = 16
输出:true
解释:24 = 16
示例 3:
输入:n = 3
输出:false
示例 4:
输入:n = 4
输出:true
示例 5:
输入:n = 5
输出:false
提示:
-231 <= n <= 231 - 1
代码
package tencent50;
public class leetcode231 {
// 符合规定的n最大取值为2^30,所以我们只要判断当前的n是不是2^30的约数即可
int bigNum = 1 << 30;
public boolean isPowerOfTwo(int n) {
return n > 0 && bigNum % n == 0;
}
// 或者是2的幂的话,则它二进制上只有一位为1
public boolean isPowerOfTwo1(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
public static void main(String[] args) {
leetcode231 obj = new leetcode231();
System.out.println(obj.isPowerOfTwo(3));
}
}
边栏推荐
- LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
- Read PG in data warehouse in one article_ stat
- ROS机器人更换新雷达需要重新配置哪些参数
- C语言数组相关问题深度理解
- [Presto profile series] timeline use
- Introduce six open source protocols in detail (instructions for programmers)
- JNA learning notes 1: Concepts
- Mongodb command summary
- 得物客服热线的演进之路
- 交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
猜你喜欢
JS slow motion animation principle teaching (super detail)
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
Navicat run SQL file import data incomplete or import failed
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
Realize the IP address home display function and number home query
Xshell connection server changes key login to password login
QQ的药,腾讯的票
How to make join run faster?
单片机学习笔记之点亮led 灯
数字ic设计——SPI
随机推荐
MySQL error 28 and solution
线程池拒绝策略最佳实践
Distributed transaction solution
Pcap learning notes II: pcap4j source code Notes
RealBasicVSR测试图片、视频
实现IP地址归属地显示功能、号码归属地查询
信号强度(RSSI)知识整理
JS function returns multiple values
error LNK2019: 无法解析的外部符号
JNA learning notes 1: Concepts
[1] Basic knowledge of ros2 - summary version of operation commands
[learning notes] agc010
分布式事务解决方案
聊聊伪共享
648. 单词替换 : 字典树的经典运用
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Ogre入门尝鲜
得物客服热线的演进之路
Cinnamon 任务栏网速