当前位置:网站首页>[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 longest ascending subsequence problem (dynamic programming, greed + dichotomy)
- Storage principle inside mongodb
- Getting started with MySQL
- C语言数组相关问题深度理解
- 为租客提供帮助
- Navicat run SQL file import data incomplete or import failed
- Realbasicvsr test pictures and videos
- How to make join run faster?
- 单片机学习笔记之点亮led 灯
- MongoDB的用户管理总结
猜你喜欢
Ways to improve the performance of raspberry pie
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
DID登陆-MetaMask
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
Scripy tutorial classic practice [New Concept English]
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Write it down once Net a new energy system thread surge analysis
PAcP learning note 1: programming with pcap
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
Leecode3. Longest substring without repeated characters
随机推荐
Final review notes of single chip microcomputer principle
提升树莓派性能的方法
How did Guotai Junan Securities open an account? Is it safe to open an account?
mysql 局域网内访问不到的问题
Use of polarscatter function in MATLAB
得物客服热线的演进之路
648. 单词替换 : 字典树的经典运用
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Cmake learning and use notes (1)
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
JS function 返回多个值
clion mingw64中文乱码
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
PAcP learning note 3: pcap method description
2022-7-7 Leetcode 844.比较含退格的字符串
【日常训练】648. 单词替换
xshell连接服务器把密钥登陆改为密码登陆
Summary of import, export, backup and recovery of mongodb
How far can it go to adopt a cow by selling the concept to the market?
Getting started with MySQL