翻译

给定一个整型数,写一个函数决定它是否是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. 将ASP.NET用户控件转化为自定义控件

    将ASP.NET用户控件转化为自定义控件 作者:Kevin Cheng (程建和) 最后修改时间:2006-03-14 概述:如何将ASP.NET用户控件移植为ASP.NET自定义控件 关键字:Asp ...

  2. 面向对象编程案例02--显示地调用父类的__init__()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #面向对象编程案例02--显示地调用父类的__init__() ''' 继承是面向对象的重要特征之一,继承是2 ...

  3. 理解 LDA 主题模型

    前言 gamma函数 0 整体把握LDA 1 gamma函数 beta分布 1 beta分布 2 Beta-Binomial 共轭 3 共轭先验分布 4 从beta分布推广到Dirichlet 分布 ...

  4. 简化版的SpringMVC框架的实现思路

    在SpringMVC基本统一Java web开发技术栈的环境下, 这是一个有点过时的话题了. SpringMVC的特点主要在于注解型的RequestMapping和参数机制非常灵活, 另外得益于Spr ...

  5. Newifi2(D1) 刷入pb-boot和breed的记录

    今天要给一个newifi d1刷系统时发现居然还是原厂的uboot, 使用uboot刷入rom时会进行校验拦截第三方的rom. 之前有刷过这个设备的, 但是已经完全记不清怎么处理的了. 查了一下, 这 ...

  6. samba config

    [global] netbios name = HARDY     #设置服务器的netbios名字 server string = my server #对samba服务器的描述 workgroup ...

  7. ubuntu 终端中文显示乱码问题!

    1 Alt+Ctrl+F1 进入第一个终端,发现中文乱码. 2 安装zhcon. sudo apt-get install zhcon 3 输入下面命令,启动zhcon,中文显示正常. zhcon - ...

  8. hibernate联合主键注解方式

    方法一:主键类用@Embeddable,pojo类仍然用@Entity但是引用主键类的对象用@Id 主键pojo类: @Embeddable public class composeIdPK impl ...

  9. iOS 中的静态库与动态库,区别、制作和使用

    如果我们有些功能要给别人用,但是又不想公开代码实现,比如高德地图.第三方登录分享等等,这时候我们就要打包成库了.库分静态库和动态库两种: 静态库:以.a 和 .framework为文件后缀名.动态库: ...

  10. win32 socket之select

    之前光看理论是不行滴,一定要实践,实践啊,不然永远都是门外汉!! 嗯嗯,把找到的一段源码贴上先,稍微修改了一下: #include <winsock.h> #include <std ...