当前位置:网站首页>【日常训练--腾讯精选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));
}
}
边栏推荐
- My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
- Ikvm of toolbox Net project new progress
- RealBasicVSR测试图片、视频
- MongoDB 分片总结
- Mongodb meets spark (for integration)
- JNA learning notes 1: Concepts
- 室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
- 1. Deep copy 2. Call apply bind 3. For of in differences
- MongoDB优化的几点原则
- Vscade editor esp32 header file wavy line does not jump completely solved
猜你喜欢
Error lnk2019: unresolved external symbol
Introduce six open source protocols in detail (instructions for programmers)
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
提升树莓派性能的方法
Cinnamon taskbar speed
Flink | 多流转换
MySQL error 28 and solution
Ways to improve the performance of raspberry pie
MongoDB内部的存储原理
Milkdown control icon
随机推荐
QQ的药,腾讯的票
Vscade editor esp32 header file wavy line does not jump completely solved
ESP32构解工程添加组件
RealBasicVSR测试图片、视频
Getting started with cinnamon applet
Server to server (S2S) event (adjust)
Esp32 ① compilation environment
Scripy tutorial classic practice [New Concept English]
Clion mingw64 Chinese garbled code
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
Flink | multi stream conversion
ESP32系列专栏
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Cinnamon 任务栏网速
Mongodb command summary
得物客服热线的演进之路
[Presto profile series] timeline use
shell 批量文件名(不含扩展名)小写改大写
PHP - laravel cache
error LNK2019: 无法解析的外部符号