本文主要是介绍LeetCode 231 Power of Two (位运算),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Given an integer, write a function to determine if it is a power of two.
题目链接:https://leetcode.com/problems/power-of-two/
题目分析:大于0且二进制中只有一位为1
public class Solution {public boolean isPowerOfTwo(int n) {return n > 0 && (n & (n - 1)) == 0;}
}
这篇关于LeetCode 231 Power of Two (位运算)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!