当前位置:网站首页>[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));
}
}
边栏推荐
- LIS 最长上升子序列问题(动态规划、贪心+二分)
- SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
- 【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
- Getting started with cinnamon applet
- ESP32系列专栏
- Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
- 【日常训练--腾讯精选50】231. 2 的幂
- JS slow motion animation principle teaching (super detail)
- 【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
- 提升树莓派性能的方法
猜你喜欢
随机推荐
Distributed transaction solution
[learning notes] segment tree selection
[Presto profile series] timeline use
OSI seven layer model
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
MongoDB 分片总结
Some principles of mongodb optimization
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
Ikvm of toolbox Net project new progress
Error lnk2019: unresolved external symbol
得物客服热线的演进之路
Digital IC Design SPI
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
Deep understanding of array related problems in C language
Leecode3. Longest substring without repeated characters
Show the mathematical formula in El table
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
Read PG in data warehouse in one article_ stat
2022-7-7 Leetcode 844.比较含退格的字符串
1、深拷贝 2、call apply bind 3、for of for in 区别