当前位置:网站首页>【日常训练--腾讯精选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));
}
}
边栏推荐
- Some principles of mongodb optimization
- Use of polarscatter function in MATLAB
- Vscade editor esp32 header file wavy line does not jump completely solved
- MongoDB优化的几点原则
- Getting started with cinnamon applet
- 华为镜像地址
- JNA learning notes 1: Concepts
- 648. 单词替换 : 字典树的经典运用
- Storage principle inside mongodb
- Esp32 series column
猜你喜欢
Esp32 ① compilation environment
Xshell connection server changes key login to password login
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
OSI 七层模型
如何让join跑得更快?
Milkdown 控件图标
Introduce six open source protocols in detail (instructions for programmers)
Ways to improve the performance of raspberry pie
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
靠卖概念上市,认养一头牛能走多远?
随机推荐
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
ESP32系列专栏
线程池拒绝策略最佳实践
QQ的药,腾讯的票
分布式事务解决方案
[1] ROS2基础知识-操作命令总结版
Thread pool reject policy best practices
实现IP地址归属地显示功能、号码归属地查询
Introduction and basic use of stored procedures
Final review notes of single chip microcomputer principle
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
聊聊伪共享
Write it down once Net a new energy system thread surge analysis
Ways to improve the performance of raspberry pie
存储过程的介绍与基本使用
Milkdown control icon
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)