翻译

给定一个整型数,写一个函数决定它是否是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. Xcode SVN配置

    Xcode SVN配置 编辑 ~/.subversion/config 文件  注意:假设".subversion"文件夹不存在.请执行"svn status" ...

  2. iOS中TableView小技巧

    摘要: TableView是ios开发中经经常使用到的控件,这里统一记录一下开发中遇到的经常使用小技巧,不断探索更新.也希望大家能够告诉我很多其它经常使用的小技巧啦~一起进步 1.去除多余的列表线条 ...

  3. 【Linux】awk详细介绍

    awk简介 awk是一种使用方便且表现力很强的编程语言,它可以应用在多种不同的计算与数据处理任务中.由于awk天生提供对文件中文本分列进行处理,所以如果一个文件中的每行都被特定的分隔符(常见的是空格) ...

  4. Eclipse和MyEclipse使用技巧--MyEclipse下创建的项目导入到Eclipse中详细的图文配置方法

    一.情景再现. 有些人比较喜欢用Myeclipse开发,有些人却比较喜欢用eclipse开发.但是其中有一个问题,Myeclipse里面的项目导入的时候出现了一个小小的问题. 如下: 二.说明问题 导 ...

  5. loadrunner error 27796 Failed to connect to server

    (2012-10-23 01:23:17) 转载▼   Action.c(58): Error -27796: Failed to connect to server "www.baidu. ...

  6. 转:ogre的编译及安装

    ogre在Windows环境下的编译及安装过程: 1.从下面网址下载OGRE 1.8.1 Source For Windows.Dependencies source repository with ...

  7. Pinpoint - 应用性能管理(APM)平台实践之部署篇

    0.0 前言 国内的APM行业这两年刚刚起步,但是在国外却比较成熟了,并且由于这两年人力成本的快速提高,国内外涌现了几家非常不错的APM企业,例如APPdynamic,Dynamic,NewRelic ...

  8. Maven2和ivy比较

    Maven 2和Ivy常被放在一起对比,但实际上两者是不同类型的工具.Ivy仅提供依赖管理功能,但是Maven 2是一个软件项目管理综合工具,能够管理构建.报告.文档,以及根据中心化的信息来管理依赖. ...

  9. VS2010 C++环境下DLL和LIB文件的生成与调试

    利用VS2010工具,调试DLL文件的方法现总结如下: 在一个解决方案中生成两个工程,假设MYDLL和MYDLG两个工程,前者是DLL工程,后者DLG调用前边的DLL工程.设置如下: 目录如下:图,本 ...

  10. 指尖下的js ——多触式web前端开发之二:处理简单手势(转)

    这篇文章将描述多触式网页开发中对手势(Gesture)事件的处理.     水果设备中的Gesture,广义的说包括手指点击(click),轻拂(flick),双击(double-click),两只手 ...