leetcode 326. Power of Three(不用循环或递归)

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?

题意是判断一个数是否是3的幂,最简单的也最容易想到的办法就是递归判断,或者循环除。

有另一种方法就是,求log以3为底n的对数。类似 如果n=9,则结果为2,如果是10,则结果肯定不是个整数。所以第一次提交如下:

public class Solution {

    public boolean isPowerOfThree(int n) {
if(0 == n)
return false;
double res = Math.log(n)/Math.log(3);
return Math.floor(res) == Math.ceil(res); }
}

但是并没有ac,原因是243的时候出错,后来分析发现是因为java 浮点数的原因。参考别人的修改了下,如下:

public class Solution {
private static final double epsilon = 10e-15; public boolean isPowerOfThree(int n) {
if(0 == n)
return false;
double res = Math.log(n)/Math.log(3);
//return Math.floor(res) == Math.ceil(res);
return Math.abs(res - Math.round(res)) < epsilon;
}
}

ac

leetcode 326. Power of Three(不用循环或递归)的更多相关文章

  1. 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法

    来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...

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

  3. LeetCode 326 Power of Three(3的幂)(递归、Log函数)

    翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...

  4. 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 ...

  5. 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 ...

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

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

  8. Leetcode 326 Power of Three 数论

    判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) =  1162261467)的约数 这种方法是我 ...

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

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

随机推荐

  1. 关于ViewPager被嵌套在ScrollView中不显示的问题

    关于ViewPager被嵌套在ScrollView中不显示的问题 进入全屏 ScrollView 嵌套ViewPager,要不是业务需求这样,估计没人愿意这么干!因为这种方式,会问题多多,简单百度一下 ...

  2. [置顶] SPL讲解(6)--Condition篇

    SmartPersistenceLayer 2.0 之 Condition篇 原理        强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...

  3. 如何导出sqlserver中的表数据,sqlserver2008

    sqlserver数据库中的表数据,我们通常想使用一下,可是怎样获取这些数据呢? 1.选择任务->生成脚本 2.选择数据库 3.设置配置,让编写数据的脚本为true 4.保存为sql 5.完成 ...

  4. 2014元旦第1周三新的尝试&爬山丢失望远镜

    2014元旦在早起中开始,起来后看了<逃出你的肖申克>系列文章,没有精度只是选择了里面的关键语句和方法论,没有多少意外的收获.然后看了些java系列文章,关于jvm参数配置.运行监控及性能 ...

  5. LeeCode(Database)-Employees Earning More Than Their Managers

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

  6. Zookeeper 2、Zookeeper的安装和配置(集群模式)

    1.下载与解压 Zookeeper下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下载完成以后解压到一个特定目录 同步时间所有节点的时间,并关 ...

  7. 让你的javascript函数拥有记忆功能,降低全局变量的使用

    考虑例如以下场景:假如我们须要在界面上画一个圆,初始的时候界面是空白的.当鼠标移动的时候,圆须要尾随鼠标移动.鼠标的当前位置就是圆心.我们的实现方案是:假设界面上还没有画圆,那么就新创建一个:假设已经 ...

  8. Android launcher3 开发初始篇

    版本号:1.0 日期:2014.8.26 2014.8.27 2014.11.10 版权:© 2014 kince 转载注明出处         好久没有写博客,也是由于工作比較忙的关系.当然这不是理 ...

  9. &lt;源代码&gt;FTPclient追加方式上传自己定义信息

    实现功能:向FTPserver以追加方式上传自己定义信息(例程中为:2014-10-08 13:47:15 test.) 源代码下载(免积分):http://download.csdn.net/det ...

  10. App Store不能下载一直等待中的两种解决办法

    1,重启手机,之后确认是否得到改善 2,重启不行,更改WiFi的dns为114.114.114.114或者223.5.5.5 或 223.6.6.6,再重启手机 ps:我是第二种方法