当前位置:网站首页>【日常训练--腾讯精选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));
}
}
边栏推荐
- MATLAB中polarscatter函数使用
- Flink | 多流转换
- Esp32 series column
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- ESP32系列专栏
- JS缓动动画原理教学(超细节)
- Detr introduction
- MongoDB命令汇总
- Cinnamon 任务栏网速
- Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
猜你喜欢

Vscode编辑器ESP32头文件波浪线不跳转彻底解决

error LNK2019: 无法解析的外部符号

Getting started with MySQL

1. Deep copy 2. Call apply bind 3. For of in differences

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

Cmake learning and use notes (1)

【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...

Thread pool reject policy best practices

Esp32 construction engineering add components

OSI 七层模型
随机推荐
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
《厌女:日本的女性嫌恶》摘录
聊聊伪共享
Vscade editor esp32 header file wavy line does not jump completely solved
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
DETR介绍
move base参数解析及经验总结
Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
PHP - laravel cache
Error lnk2019: unresolved external symbol
QQ的药,腾讯的票
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
分屏bug 小记
Pcap learning notes II: pcap4j source code Notes
Use of polarscatter function in MATLAB
MongoDB 分片总结
JS function returns multiple values
Mongodb command summary