leetcode 326. Power of Three(不用循环或递归)
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+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) 分析:这题本来很 ...
- [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 ...
- LeetCode 326 Power of Three(3的幂)(递归、Log函数)
翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode 326 Power of Three 数论
判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) = 1162261467)的约数 这种方法是我 ...
- [LeetCode] 326. Power of Three + 342. Power of Four
这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...
随机推荐
- [置顶] SPL讲解(6)--Condition篇
SmartPersistenceLayer 2.0 之 Condition篇 原理 强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...
- eclipse工具再学习
今天下午最后近1小时及晚上2个多小时,我都花费时间在工程环境配置上,自尊心被严重摧残,各种郁闷和抱怨.源头是我部分刷新代码后运行工程依赖的jar报错,后来找同事发现是因为我没更新pom.xml文件,重 ...
- python手记(44)
#!/usr/bin/env python # -*- coding: utf-8 -*- #http://blog.csdn.net/myhaspl #code:myhaspl@qq.com imp ...
- cenos 7 安装php7
1. CentOS/RHEL 7.x: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- Animate.css 教程
animate.css 是一个有趣,酷炫的,跨浏览器的动画库,你可以将它用于你的项目中.不管是主页,滑动切换,又或者是其它方面,你都可以通过它来制作出惊人的效果. 基本用法 引入CSS文件 这个对你来 ...
- Oracle SQL函数之聚组函数
AVG([distinct|all]x) [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或 ...
- centos上node.js的安装
CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,本文以v0.10.24为例: cd /usr/lo ...
- android——背景颜色渐变(梯度变化)
首先在drawable文件夹下面新建一个xml文件,起名为bgcolor.xml. 代码如下: <?xml version="1.0" encoding="utf- ...
- android 数据存储之SharePreference 的几种方式
1. 常见的 getSharedPreferences(String filename,mode) 指定sp文件的名称,生成的文件名为 filename.xml 2.getPreferences(mo ...