翻译

给定一个整型数,写一个函数决定它是否是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. Linux常见命令缩写

    缩写,是简介高效地表达 unix   unix-like   Linux 00.命令简写 ls:list(列出目录内容)cd:Change Directory(改变目录)su:switch user ...

  2. CSS3 flex的使用方法

    display:flex; align-items://水平对齐方式 justify-content://垂直对齐方式 flex://盒子所占大小(如果你盒子里面只有两个div,那么你设置其中之一个f ...

  3. 十问Android NFC手机上的卡模拟(转)

    1,  问:能否在AndroidNFC手机上实现卡模拟? 答:在技术上可行,但是,对一般开发人员来讲,目前看来仅仅是技术上可行:( 2,  问:具体如何实现呢? 答:有两种方式:一种是基于硬件的,被称 ...

  4. 利用hadoop来解决“共同好友”的问题

    假设A有好友B C D:B有好友A C D E:C有好友A B D E:D有好友A B C E;E有好友B C D. A -> B C D B -> A C D E C -> A B ...

  5. 【Struts2】Struts2与Spring整合后,如何指定Action为多例模式

    Strust2默认是多例的,但是Spring默认是单例的,在进行Spring+Strust2整合的时候,就需要把Spring管理的action指定为多例模式,只需要在action上面加上@Scope( ...

  6. 【java】详解java多线程

    目录结构: contents structure [+] 线程的创建与启动 继承Thread类创建线程类 实现Runnable接口创建线程类 使用Callable和Future创建线程 线程的生命周期 ...

  7. Windows10内置Linux子系统

      WSL 前言 前段时间,机子上的win10又偷偷摸摸升级到了一周年正式版,比较无奈.不过之前听闻这个版本已经支持内置的linux子系统,于是就怀着好奇心试玩了一把.虽然期间遇到了很多问题,但总体来 ...

  8. iOS-获取当前View所在的控制器

    用一个分类,具体: .h #import <UIKit/UIKit.h> @interface UIView (CurrentController) /** 获取当前View的控制器对象 ...

  9. tp数据库表大写命名的一些问题

    在使用thinkphp时,如果数据库表命名有大写,会被转换成小写加下划线(可以使用$model->_sql())来查看实际执行的sql是什么 这个问题,看了一下源代码,在 Thinkphp/Co ...

  10. app-framework学习--官网地址及demo下载地址

    一起学习共同进步,加油..! 官网地址:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/detail/ha ...