當(dāng)前位置:
首頁 >
LeetCode - 231. Power of Two
發(fā)布時(shí)間:2025/3/19
32
豆豆
生活随笔
收集整理的這篇文章主要介紹了
LeetCode - 231. Power of Two
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Given an integer, write a function to determine if it is a power of two.
Example 1:
Input: 16 Output: trueExample 2:
Input: 5 Output: false判斷一個(gè)數(shù)是否為2的次冪,偶然發(fā)現(xiàn)3年前做這道題就是很簡單的循環(huán)對2取余整除。順手用位運(yùn)算寫了一行代碼又提交了一次。效率提升了200多倍。 public boolean isPowerOfTwo(int n) {return n > 0 && (n & (n - 1)) == 0; }
原理參考這篇博客的技巧部分。
?
總結(jié)
以上是生活随笔為你收集整理的LeetCode - 231. Power of Two的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 44、生鲜电商平台-Java后端生成To
- 下一篇: 针对SSL/TLS的拒绝服务攻击以及使用