Skip to content

Latest commit

 

History

History
69 lines (51 loc) · 1.37 KB

0231-power-of-two.adoc

File metadata and controls

69 lines (51 loc) · 1.37 KB

231. Power of Two

{leetcode}/problems/power-of-two/[LeetCode - Power of Two^]

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

Example 1:

Input: 1
Output: true
Explanation: 20 = 1

Example 2:

Input: 16
Output: true
Explanation: 24 = 16

Example 3:

Input: 218
Output: false

思路分析

这道题可以利用 191. Number of 1 Bits 中的思路。

2 的幂次方数,它们的二进制数只有一个 1,那么 n & (n - 1) 必然为 0

看答案,也可以使用约数法。

一刷
link:{sourcedir}/_0231_PowerOfTwo.java[role=include]