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的幂的更多相关文章

  1. Java for LeetCode 230 Kth Smallest Element in a BST

    解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...

  2. Java实现 LeetCode 342 4的幂

    342. 4的幂 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶: 你 ...

  3. Java实现 LeetCode 326 3的幂

    326. 3的幂 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: tru ...

  4. Java实现 LeetCode 230 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...

  5. 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 ...

  6. 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. ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 风扇转速通过FPGA采样

    1.风扇最大转速16000RPM,那么每一转需要时间60S/16000=0.00375S=375*10^4ns=T=T1+T2+T3+T4: 2.采样0.6S内的风扇detect信号的上升沿个数:0. ...

  2. 小心了!Kubernetes自动化操作工具将让你失去工作

    运行Kubernetes的人已经花费太多时间在操作上,企业正在考虑为Kubernetes编写自动化工具. 尽管IT部门的大部分职位都会增加,但职业顾问说,计算机操作员预计会减少.这个角色涉及运行She ...

  3. Mysql 常用函数(8)- concat 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html concat 的作用 连接多个字符串 concat ...

  4. 2018-06-20 js字符串函数

    str.length -> 字符串长度; str.indexOf() -> 从左边查找字符串中某字符的位置: str.lastIndexOf -> 从右边查找字符串中某字符的位置: ...

  5. 基于MySQL 的 SQL 优化总结

    文章首发于我的个人博客,欢迎访问.https://blog.itzhouq.cn/mysql1 基于MySQL 的 SQL 优化总结 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 ...

  6. 【雕爷学编程】Arduino动手做(43)---单路继电器模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践(动手试试)出真知的理念,以学习和交流为目的,这里准备 ...

  7. React:redux+router4搭建应用骨架

    可能是短期内关于react的对后一篇笔记.假设读者对redux和router4有基本了解. 缘由: 现在网上很多关于react+redux的文章都是沿用传统的文件组织形式,即: |--componen ...

  8. VMware 11安装Mac OS X 10.10 (转载)

    VM11安装Mac OS X 10.10 工具/原料 1.VMware Workstation 112.unlocker 203(for OS X 插件补丁)3.Mac OS X 10.10镜像方法/ ...

  9. JS如何判断是否已经引入某个css或是js?

    http://bbs.csdn.net/topics/390541081 function isInclude(name){     var js= /js$/i.test(name);     va ...

  10. C# 使用RestClient 调用接口

    最近做项目使用RestClient 向第三方接口推送数据.不废话直接贴代码 /// <summary> /// 获取Token /// </summary> /// <r ...