当前位置:网站首页>【日常训练--腾讯精选50】231. 2 的幂
【日常训练--腾讯精选50】231. 2 的幂
2022-07-07 11:37:00 【Puppet__】
题目
给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false 。
如果存在一个整数 x 使得 n == 2x ,则认为 n 是 2 的幂次方。
示例 1:
输入:n = 1
输出:true
解释:20 = 1
示例 2:
输入:n = 16
输出:true
解释:24 = 16
示例 3:
输入:n = 3
输出:false
示例 4:
输入:n = 4
输出:true
示例 5:
输入:n = 5
输出:false
提示:
-231 <= n <= 231 - 1
代码
package tencent50;
public class leetcode231 {
// 符合规定的n最大取值为2^30,所以我们只要判断当前的n是不是2^30的约数即可
int bigNum = 1 << 30;
public boolean isPowerOfTwo(int n) {
return n > 0 && bigNum % n == 0;
}
// 或者是2的幂的话,则它二进制上只有一位为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));
}
}
边栏推荐
- 记一次 .NET 某新能源系统 线程疯涨 分析
- 1. Deep copy 2. Call apply bind 3. For of in differences
- Flink | 多流转换
- Vscade editor esp32 header file wavy line does not jump completely solved
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- [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
- [QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
- Ikvm of toolbox Net project new progress
- Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
- 迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
猜你喜欢

【堡垒机】云堡垒机和普通堡垒机的区别是什么?

Vscade editor esp32 header file wavy line does not jump completely solved

作战图鉴:12大场景详述容器安全建设要求

存储过程的介绍与基本使用

LeetCode简单题分享(20)

分布式事务解决方案

My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime

数字ic设计——SPI

Esp32 ① compilation environment

Getting started with cinnamon applet
随机推荐
得物客服热线的演进之路
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
《厌女:日本的女性嫌恶》摘录
记一次 .NET 某新能源系统 线程疯涨 分析
OSI seven layer model
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
Deep understanding of array related problems in C language
提升树莓派性能的方法
Realize the IP address home display function and number home query
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
Move base parameter analysis and experience summary
信号强度(RSSI)知识整理
error LNK2019: 无法解析的外部符号
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Thread pool reject policy best practices
LeetCode简单题分享(20)
Show the mathematical formula in El table
一文读懂数仓中的pg_stat
Xshell connection server changes key login to password login
【堡垒机】云堡垒机和普通堡垒机的区别是什么?