326. Power of Three
来源:互联网 发布:淘宝返现在哪里查看 编辑:程序博客网 时间:2024/06/05 14:35
题目:
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?
思路:
本题和前面的博客power of two的解题思路一样,但是题目尝试让我们不要使用循环与递归,所以本题给出非循环,非递归的方法。
代码(递归):
class Solution {public: bool isPowerOfThree(int n) { if(n==1) return true; else if(n<1) return false; else { if ((n/3.0) == (n/3)) return isPowerOfThree(n/3); else return false; } }};代码(非):
class Solution {public: bool isPowerOfThree(int n) { // 1162261467 is 3^19, 3^20 is bigger than int return ( n>0 && 1162261467%n==0);}};
阅读全文
1 0
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- 326. Power of Three
- Windows下用C语言获取系统的CPU利用率、内存使用率、硬盘使用率等-转载
- (2)nodejs---使用nodejs搭建较为完整的服务器
- Android DexHelper帮你解决65536问题
- 类型转换
- linux初学者-磁盘阵列篇
- 326. Power of Three
- 深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const
- 聚类和分类
- 线程池---ThreadPoolExecutor中corePoolSize,maximumPoolSize,workQueue的关系
- 最大连续子序列和
- 人脸识别算法初次了解
- oracleday08(桥直连)
- HTTP详解(二):HTTP报文格式
- Class.newInstance()