翻译

给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适……

跟进:
你能否够不用不论什么循环或递归来完毕。

原文

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?

分析

题意我事实上不是满懂,比方说12究竟可不能够呢?还是说仅仅有:3、9、27、81这样的才行呢?先写个简单的递归试试看吧。

bool isPowerOfThree(int n) {
if (n == 1) return true;
else if (n == 0) return false;
else if (n % 3 == 0)
return isPowerOfThree(n / 3);
else return false;
}

提交成功了,那么自己用12作为參数来试试呢,发现返回false。那么就能够断定题意是上面我说的另外一种了。

是否还记得log函数呢。之前有一道题遇到过。所以这次一下就想到了……

logn3

假设以12来计算的话:

log123=2.26186

int(log123)=2

log123−int(log123)=0.26186

所以直接推断结果是否为0就好了……

bool isPowerOfThree(int n) {
double logAns= log(n) / log(3);
return (logAns- int(logAns) == 0) ? true : false;
}

然而这段代码提交后发现仍然是错误的,243在上面的代码中竟然是false。打断点看看应该是因为精度问题,所以继续改改。

logn3=logn10log310

所以代码就出来了……

代码

class Solution {
public:
bool isPowerOfThree(int n) {
double logAns = log10(n) / log10(3);
return (logAns - int(logAns) == 0) ? true : false;
}
};

LeetCode 326 Power of Three(3的幂)(递归、Log函数)的更多相关文章

  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

    Problem: Given an integer, write a function to determine if it is a power of three. 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. 326 Power of Three 3的幂

    给出一个整数,写一个函数来确定这个数是不是3的一个幂.后续挑战:你能不使用循环或者递归完成本题吗? 详见:https://leetcode.com/problems/power-of-three/de ...

  6. [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 ...

  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 (python)

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

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

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

随机推荐

  1. jqueryMobile 动态添加元素,展示刷新视图方法

    jqueryMobile动态添加元素jqueryMobile郏高阳 jQuery Mobile的是一个很好的移动开发框架,你可能已经知道,虽然它有很多难以解决的问题,但是我相信后续版本jquery会修 ...

  2. Git 经常使用命令合集

    ====== Git 经常使用命令合集 ====== === 1.Git 文档 ===     Git 中文文档观看地址:http://git.oschina.net/progit/      === ...

  3. 设置span在div中垂直居中

    转自:https://blog.csdn.net/weirenkuan/article/details/51177695 使用display:table-cell,span中内容无论多少,都可以垂直居 ...

  4. java.lang.IllegalStateException: Failed to load ApplicationContext selenium 异常 解决

    WARN <init>, HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF R ...

  5. 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣 创建线程数公式(MaxProcessMemory - JVMMemory – ReservedOsMemory)

    剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣 星期一早上到了公司,据称产品环境抛出了最可爱的异常—OutO ...

  6. (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 并不挶泥在这个点子上面,它反而往后站一步,看看他们已经做好的这个网站,可以再怎么包装成另一个完完全全不同的网站?所有的人所做的每件失败的事情中, 一定有碰到或含有成功的答案」在里面,只是他们不知道而已。 人不怕失败」,只怕宣布失败」

    (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 今天读到 一个非常励志人心的故事 ,就像现在「叶问」有「前传」,最近很火红的团集购网站Groupon 也出现了「Groupon前传」 ...

  7. java 取模运算% 实则取余 简述 例子 应用在数据库分库分表

    java 取模运算%  实则取余 简述 例子 应用在数据库分库分表 取模运算 求模运算与求余运算不同.“模”是“Mod”的音译,模运算多应用于程序编写中. Mod的含义为求余.模运算在数论和程序设计中 ...

  8. C++中四种类型转换方式(ynamic_cast,const_cast,static_cast,reinterpret_cast)

    Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而 ...

  9. git学习笔记(四)—— 分支管理

    一.创建与合并分支 git branch //查看分支 git branch <name> //创建分支 git checkout <name> //切换分支 git chec ...

  10. mongodb的serverstatus

    MongoDB shell version: 2.0.5 connecting to: test { "host" : "TENCENT64.site", -- ...