网页学习体会

  • 首页
  • 个人博客
您的位置: 首页  >  IT文章  >  Java [Leetcode 231]Power of Two

Java [Leetcode 231]Power of Two

分类: IT文章 • 2024-06-14 23:32:12

题目描述:

Given an integer, write a function to determine if it is a power of two.

解题思路:

判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.

代码如下:

public class Solution {
    public boolean isPowerOfTwo(int n) {
        return (n > 0) && ( n & (n - 1)) == 0;
    }
}

  

相关推荐

  • LeetCode 231 Power of Two
  • 【LeetCode & 剑指offer刷题】发散思维题6:231. Power of Two(系列)
  • 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists
  • LeetCode 231. Power of Two
  • LeetCode Power of Two
  • 【LeetCode-口试算法经典-Java实现】【160-Intersection of Two Linked Lists(单链表的交集)】
  • 【LeetCode-口试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】
  • 【LeetCode-口试算法经典-Java实现】【004-Median of Two Sorted Arrays(两个排序数组的中位数)】
  • Java [leetcode 一] Two Sum
  • 【LeetCode-口试算法经典-Java实现】【029-Divide Two Integers(两个整数相除)】
  • vue
  • P1169棋盘制作
    网站免责声明 网站地图 最新文章 用户隐私 版权申明
本站所有数据收集于网络,如果侵犯到您的权益,请联系网站进行下架处理。   

Copyright © 2018-2021   Powered By 网页学习体会    备案号:   粤ICP备20002247号