Java实现 LeetCode 230 2的幂
231. 2的幂
给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
示例 1:
输入: 1
输出: true
解释: 20 = 1
示例 2:
输入: 16
输出: true
解释: 24 = 16
示例 3:
输入: 218
输出: false
PS:
2的次幂和他的上一位数&的结果为0
8的二进制就是1000
7的二进制就是0111
结果========0000
class Solution {
public boolean isPowerOfTwo(int n) {
if(n <= 0) return false;
return (n&(n-1)) == 0;
}
}
Java实现 LeetCode 230 2的幂的更多相关文章
- Java for LeetCode 230 Kth Smallest Element in a BST
解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...
- Java实现 LeetCode 342 4的幂
342. 4的幂 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶: 你 ...
- Java实现 LeetCode 326 3的幂
326. 3的幂 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: tru ...
- Java实现 LeetCode 230 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- 设计模式GOF23之单例模式
单例模式的五种方式 主要:懒汉式,饿汉式 其他:双重检测锁(Double Checking模式),静态内部类,枚举模式 选取时机 延时加载,占用内部资源大:静态内部类好于懒汉 不延时加载,占用内部资源 ...
- [hdu1028]整数拆分,生成函数
题意:给一个正整数n,求n的拆分方法数(不考虑顺序) 思路:不妨考虑用1~n来构成n.用多项式表示单个数所有能构成的数,用多项式表示,就相当于卷积运算了. 1 2 3 4 5 6 7 8 9 10 1 ...
- 【SMB源码解析系列】——002.RESET中断
跟随代码结尾处的中断向量,我们可以看到RESET中断所在地址为Start标签处. 这部分代码比较简单,从字面便可基本理解. 1.(682~683)状态寄存器设置,sei指令用于禁用IRQ中断,SMB中 ...
- CQengine高性能内存数据缓存查找框架
CQengine可实现高性能内存数据缓存查找 CQEngine 需要设置字段对应的属性以方便访问与查询 主要有属性链接 SimpleAttribute(不能为空) SimpleNullableAttr ...
- windows下node配置npm全局路径(踩坑)
事情的起因是:Koa要求v7.6.0以上的nodejs. 但是window环境下升级node不容易,试过npm install -g n 和n stable等命令无效,而网上推荐的nvm并不支持win ...
- React中父子组件数据传递
Vue.js中父子组件数据传递:Props Down , Events Up Angular中父子组件数据传递:Props Down, Events Up React中父子组件数据传递:Prop ...
- 3.9 Go Slice切片
3.9 Go Slice切片 Go语言切片(Slice) 切片是可动态变化的序列,是对数组的引用,引用类型,遵循引用传递的机制 slice类型写作[ ]T,T是slice元素类型,var s1 []i ...
- 模板渲染jnja2模块
模板渲染jnja2模块 模板的引入: 在返回动态页面时,上述我们在08版web框架返回每次访问的时间,利用自己写的占位符进行字符串替换进行动态响应: 在实际应用中,完全可以从数据库中读取数据,然后替换 ...
- CDH6 高版本hbase+solr实现二级索引
之前的环境是单独下载的CDH组件包搭建的集群,但是因为hadoop版本过低导致漏洞无法修复,重新搭建高版本集群环境. 新集群环境: 主要组件:hadoop,hbase,zookeeper,Key-Va ...
- module.exports = $; $ is not defined
https://blog.csdn.net/weixin_43945983/article/details/88294052 解决方案:安装依赖包 1.执行安装jquery依赖包命令 cnpm ins ...