Leet Code OJ 326. Power of Three [Difficulty: Easy]
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 326. Power of Three [Difficulty: Easy]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
翻譯:
給定一個整數,寫一個函數去判斷它是否是3的冪。
更進一步:你能不使用循環或者遞歸來完成嗎?
代碼:
public class Solution {public boolean isPowerOfThree(int n) {if(n<=0){return false;}while(n!=1){if(n%3!=0){return false;}else{n/=3;}}return true;} }總結
以上是生活随笔為你收集整理的Leet Code OJ 326. Power of Three [Difficulty: Easy]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leet Code OJ 219. Co
- 下一篇: Leet Code OJ 26. Rem