当前位置:网站首页>[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));
}
}
边栏推荐
- Learning breakout 2 - about effective learning methods
- 得物客服热线的演进之路
- 2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
- Simple and easy-to-use code specification
- High end for 8 years, how is Yadi now?
- php——laravel缓存cache
- Error lnk2019: unresolved external symbol
- Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
- MongoDB内部的存储原理
- User management summary of mongodb
猜你喜欢
2022-7-6 Leetcode 977.有序数组的平方
Thread pool reject policy best practices
单片机学习笔记之点亮led 灯
Introduction and basic use of stored procedures
Ogre introduction
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Final review notes of single chip microcomputer principle
[learning notes] zkw segment tree
Flink | multi stream conversion
靠卖概念上市,认养一头牛能走多远?
随机推荐
toRaw和markRaw
JS function 返回多个值
【日常训练】648. 单词替换
Mongodb slice summary
MongoDB优化的几点原则
靠卖概念上市,认养一头牛能走多远?
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Esp32 ① compilation environment
高端了8年,雅迪如今怎么样?
LIS 最长上升子序列问题(动态规划、贪心+二分)
High end for 8 years, how is Yadi now?
QQ medicine, Tencent ticket
Read PG in data warehouse in one article_ stat
C语言数组相关问题深度理解
Split screen bug notes
xshell连接服务器把密钥登陆改为密码登陆
1、深拷贝 2、call apply bind 3、for of for in 区别
MongoDB命令汇总
Talk about pseudo sharing
《厌女:日本的女性嫌恶》摘录