Leetcode 326. Power of Three
題目的意思是給定一個整數,判斷它是否是3的冪。能否不用遞迴或者迴圈來解決這個問題?
方法1:寫了個遞迴的版本。
class Solution {
public:
bool isPowerOfThree(int n) {
return n==1 || n>0 && n%3==0 && isPowerOfThree(n/3);
}
};
方法2:我們可以找到INT_MAX以內的最大的3的冪=3^(int)log3INT_MAX = 1162261467,如果一個數是3的冪,那麼它一定可以被1162261467整除。但是注意這種方法只是對素數有效。
class Solution {
public:
bool isPowerOfThree(int n) {
return (n>0 && (1162261467%n==0));
}
};
相關文章
- 326. Power of Three--LeetCode RecordLeetCode
- LeetCode 第 326 題 (Power of Three)LeetCode
- 每天一道LeetCode--326. Power of ThreeLeetCode
- The Power of Three - Eclipse, Tomcat, and StrutsEclipseTomcat
- Leetcode Power of TwoLeetCode
- Leetcode 231 Power of TwoLeetCode
- LeetCode 第 342 題(Power of Four)LeetCode
- LeetCode 259. Three Sum SmallerLeetCode
- Leetcode 231. Power of TwoLeetCode
- LeetCode 第 231 題 (Power of Two)LeetCode
- 每天一道LeetCode--342. Power of FourLeetCode
- 【LeetCode 231_整數_位運算】Power of TwoLeetCode
- oracle function powerOracleFunction
- 【Three.js】Three.js學習記錄JS
- 跟我一起刷leetCode演算法題11之 Maximum Product of Three NumbersLeetCode演算法
- three.jsJS
- UFS之Power Mode
- Power Profiles for AndroidAndroid
- E. Expected Power
- Shichikuji and Power Grid
- IBM Power5&Power6小型機 HMC預設IPIBM
- 38 Three.js高階材質THREE.ShaderMaterialJS
- spring_threeSpring
- three.js WebGLRenderTargetJSWeb
- 初識three.js,搭建three.js+vue.js專案Vue.js
- [LeetCode-231] Power of Two(判斷一個數是不是2的若干次冪)LeetCode
- DRIVER_POWER_STATE_FAILUREAI
- 精讀《Microsoft Power Fx》ROS
- Power BI入門教程
- Postpartum dragon power pills in londonGo
- ASCII in oracle, masking power onASCIIOracle
- CMD和power shell命令
- 填空題回答(Power Query)
- 155_模型_Power BI & Power Pivot 進銷存之安全庫存模型
- Three.js開發指南(4):使用Three.js的材質JS
- 41 Three.js高階幾何體THREE.ConvexGeometryJS
- 43 Three.js自定義二維圖形THREE.ShapeGeometryJS
- three.js 曲線JS