当前位置:网站首页>[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));
}
}
边栏推荐
- Talk about pseudo sharing
- 作战图鉴:12大场景详述容器安全建设要求
- 2022-7-7 Leetcode 844.比较含退格的字符串
- Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
- LIS 最长上升子序列问题(动态规划、贪心+二分)
- [1] Basic knowledge of ros2 - summary version of operation commands
- SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
- 1、深拷贝 2、call apply bind 3、for of for in 区别
- Vscode编辑器ESP32头文件波浪线不跳转彻底解决
- 交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
猜你喜欢

2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来

室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)

2022-7-6 Leetcode 977.有序数组的平方

Leecode3. Longest substring without repeated characters

2022-7-7 Leetcode 844.比较含退格的字符串

Custom thread pool rejection policy

Cmake learning and use notes (1)

Esp32 ① compilation environment

566. 重塑矩阵

2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
随机推荐
单片机原理期末复习笔记
MySQL error 28 and solution
MongoDB复制(副本集)总结
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
Realbasicvsr test pictures and videos
Shell batch file name (excluding extension) lowercase to uppercase
Drawerlayout suppress sideslip display
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
简单好用的代码规范
ESP32构解工程添加组件
JS缓动动画原理教学(超细节)
Some principles of mongodb optimization
Talk about pseudo sharing
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Cinnamon Applet 入门
10 pictures open the door of CPU cache consistency
作战图鉴:12大场景详述容器安全建设要求
LIS 最长上升子序列问题(动态规划、贪心+二分)
Write it down once Net a new energy system thread surge analysis
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours