Problem:

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

Could you do it without using any loop / recursion?

Summary:

用非循环/递归的方法判断数n是否为3的整数幂。

Analysis:

1. 循环:将n逐次除以3,判断是否为3的整数幂。

 class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= ) {
return false;
}
while (n > ) {
if (n % ) {
return false;
} n /= ;
} return true;
}
};

2. 递归:若n为3的整数幂,n/3必为3的整数幂,以这个思想构建递归。

 class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= ) {
return false;
} if (n == ) {
return true;
} return (n % == ) && isPowerOfThree(n / );
}
};

3. 非循环/递归:n若为3的整数幂,则 n = 3log3n = 3log2n / log 2

 class Solution {
public:
bool isPowerOfThree(int n) {
return (n > ) && (n == pow(, round(log(n) / log())));
}
};

LeetCode 326 Power of Three的更多相关文章

  1. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  2. 39. leetcode 326. Power of Three

    326. Power of Three Given an integer, write a function to determine if it is a power of three. Follo ...

  3. [LeetCode] 326. Power of Three 3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  4. Java [Leetcode 326]Power of Three

    题目描述: Given an integer, write a function to determine if it is a power of three. Follow up:Could you ...

  5. LeetCode 326 Power of Three(3的幂)(递归、Log函数)

    翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...

  6. leetcode 326 Power of Three (python)

    原题: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  7. Leetcode 326 Power of Three 数论

    判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) =  1162261467)的约数 这种方法是我 ...

  8. [LeetCode] 326. Power of Three + 342. Power of Four

    这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...

  9. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

随机推荐

  1. [译]angularjs directive design made easy

    原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...

  2. [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站

    原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...

  3. Ubuntu 14 安装 Chrome/Chromium flash播放器

    在命令终端中执行如下命令即可: sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-nonfree ...

  4. InnoDB为什么要使用auto_Increment

    在Mysql表设计中,通常会使用一个与业务无关的自增列做为主键.这是因为Mysql默认使用B-Tree索引,你可以简单理解为"排好序的快速查找结构".如下是一个B-Tree的结构图 ...

  5. ServiceManager: Permmission failure: android.permission.RECORD_AUDIO

    今天在Android6.0系统的手机上测试一款APP,出现如题错误: ServiceManager: Permmission failure: android.permission.RECORD_AU ...

  6. PL/SQL创建数据表空间

    创建数据表空间create tablespace stbss datafile 'E:\oracle\product\10.2.0\oradata\orcl\stbss_temp01.dbf' siz ...

  7. JQuery测手速小游戏-遁地龙卷风

    (-1)写在前面 我用的chrome49,jquery3.0,我得到过399分,信不信由你. (1)设计思路 两个p元素放在div里,每个p元素的高度和宽度都和div一样,当鼠标放在div上时,第一个 ...

  8. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  9. Caffe学习系列(16):caffe的整体流程

    在某社区看到的回答,觉得不错就转过来了:http://caffecn.cn/?/question/123 Caffe从四个层次来理解:Blob,Layer,Net,Solver. 1.Blob Caf ...

  10. mysql 将指定列的浮点数转化为整数

    mysql 将指定列的浮点数转化为整数: update A set B =  cast(B as decimal(10,0))  -- 或者 update A set B = round(B,0) 例 ...