当前位置:网站首页>【日常训练--腾讯精选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));
}
}
边栏推荐
猜你喜欢

最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航

DETR介绍

How far can it go to adopt a cow by selling the concept to the market?

ESP32 ① 编译环境

Ways to improve the performance of raspberry pie

Introduce six open source protocols in detail (instructions for programmers)
![[Presto profile series] timeline use](/img/c6/83c4fdc5f001dab34ecf18c022d710.png)
[Presto profile series] timeline use

My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime

迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件

LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
随机推荐
Esp32 construction engineering add components
信号强度(RSSI)知识整理
[1] Basic knowledge of ros2 - summary version of operation commands
Problems that cannot be accessed in MySQL LAN
干货|总结那些漏洞工具的联动使用
Write it down once Net a new energy system thread surge analysis
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
DID登陆-MetaMask
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
MongoDB内部的存储原理
Esp32 series column
Mongodb slice summary
Cinnamon 任务栏网速
MongoDB命令汇总
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
ESP32构解工程添加组件
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
C语言数组相关问题深度理解
高端了8年,雅迪如今怎么样?
ROS机器人更换新雷达需要重新配置哪些参数