翻译

  1. 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适……
  2. 跟进:
  3. 你能否够不用不论什么循环或递归来完毕。

原文

  1. Given an integer, write a function to determine if it is a power of three.
  2. Follow up:
  3. Could you do it without using any loop / recursion?

分析

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

  1. bool isPowerOfThree(int n) {
  2. if (n == 1) return true;
  3. else if (n == 0) return false;
  4. else if (n % 3 == 0)
  5. return isPowerOfThree(n / 3);
  6. else return false;
  7. }

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

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

logn3

假设以12来计算的话:

log123=2.26186

int(log123)=2

log123−int(log123)=0.26186

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

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

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

logn3=logn10log310

所以代码就出来了……

代码

  1. class Solution {
  2. public:
  3. bool isPowerOfThree(int n) {
  4. double logAns = log10(n) / log10(3);
  5. return (logAns - int(logAns) == 0) ?
  6. true : false;
  7. }
  8. };

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. django之创建第4-3个项目-访问list数据

    1.index <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. python之函数用法islower()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att ...

  3. Adobe After Effects CS6 操作记录

    安装 After Effects CS6 在Mac OS 10.12.5 上无法直接安装, 需要浏览到安装的执行文件后才能进行 https://helpx.adobe.com/creative-clo ...

  4. android事务队列处理的实现

    代码地址如下:http://www.demodashi.com/demo/14748.html 前言 在android开发中,我们经常会遇到一种情况,随时接收并处理服务端发过来的消息,当服务端发过来的 ...

  5. adjustResize和adjustPan的比较

    在下面的描述中,编辑框的maxLine都设定为10. 在信息列表界面中,编辑框在RelativeLayout中定义.编辑框上边(above)的列表组件的高度不会缩小为0,导致显示出现一点问题. 当信息 ...

  6. 首次启动Kafka报Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c0000000, 1073741824, 0) failed; error='Cannot allocate memory' (errno=12)

    首次启动Kafka报错如下: 原因:内存不足,查看启动配置 调小一些:

  7. 零行代码为 App 添加异常加载占位图

    前文提要 近期准备重构项目,需要重写一些通用模块,正巧需要设置App异常加载占位图的问题,心血来潮设想是否可以零行代码解决此问题,特在此分享实现思路. 思路分享 对于App占位图,通常需要考虑的控件有 ...

  8. Java正则应用

    private List<String> find(String reg, String str) { Matcher matcher = Pattern.compile(reg).mat ...

  9. Rplidar学习(二)—— SDK库文件学习

    SDK头文件介绍 1.头文件简介: rplidar.h //一般情况下开发的项目中仅需要引入该头文件即可使用 RPLIDAR SDK 的所有功能. rplidar_driver.h //定义了 SDK ...

  10. SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍

    今天给大家介绍一下SpringBoot中Redis的set.map.list.value等基本操作的具体使用方法 上一节中给大家介绍了如何在SpringBoot中搭建Redis缓存数据库,这一节就针对 ...