当前位置:网站首页>[daily training -- Tencent select 50] 231 Power of 2
[daily training -- Tencent select 50] 231 Power of 2
2022-07-07 13:38:00 【Puppet__】
subject
Give you an integer n, Please judge whether the integer is 2 Power square . If it is , return true ; otherwise , return false .
If there is an integer x bring n == 2x , Think n yes 2 Power square .
Example 1:
Input :n = 1
Output :true
explain :20 = 1
Example 2:
Input :n = 16
Output :true
explain :24 = 16
Example 3:
Input :n = 3
Output :false
Example 4:
Input :n = 4
Output :true
Example 5:
Input :n = 5
Output :false
Tips :
-231 <= n <= 231 - 1
Code
package tencent50;
public class leetcode231 {
// In conformity with the regulations n The maximum value is 2^30, So we just need to judge the current n Is it right? 2^30 It's a divisor of
int bigNum = 1 << 30;
public boolean isPowerOfTwo(int n) {
return n > 0 && bigNum % n == 0;
}
// Or is it 2 If it's a power of , Then it has only one binary digit 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));
}
}
边栏推荐
猜你喜欢
Thread pool reject policy best practices
Introduction and basic use of stored procedures
Cinnamon taskbar speed
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
flask session伪造之hctf admin
LIS 最长上升子序列问题(动态规划、贪心+二分)
室内ROS机器人导航调试记录(膨胀半径的选取经验)
单片机学习笔记之点亮led 灯
LED light of single chip microcomputer learning notes
1. Deep copy 2. Call apply bind 3. For of in differences
随机推荐
Problems that cannot be accessed in MySQL LAN
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
1、深拷贝 2、call apply bind 3、for of for in 区别
flask session伪造之hctf admin
Solve the cache breakdown problem
RealBasicVSR测试图片、视频
记一次 .NET 某新能源系统 线程疯涨 分析
Mongodb meets spark (for integration)
高端了8年,雅迪如今怎么样?
Cinnamon Applet 入门
Data refresh of recyclerview
PHP - laravel cache
数字ic设计——SPI
【日常训练】648. 单词替换
Cinnamon 任务栏网速
Move base parameter analysis and experience summary
[Presto profile series] timeline use
What parameters need to be reconfigured to replace the new radar of ROS robot
Navicat运行sql文件导入数据不全或导入失败
MongoDB命令汇总