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 ...
随机推荐
- OpenCV在ARM上的移植
OpenCV在ARM上的移植 与X86 Linux类似,请参考:Linux 下编译安装OpenCV 本文在此基础上进行进一步操作. 网络上很多移植编译的方法比较老,多数针对OpenCV 1.0,而且方 ...
- Isomorphic Strings 解答
Question Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- initial pointer [expert c]
initial differece between pointer and array Both arrays and pointers can be initialized with a liter ...
- Python高阶函数
在Python中,函数名也是一个变量,可以进行赋值 高阶函数是至少满足下列一个条件的函数: 接受一个或多个函数作为输入 输出一个函数 函数名也可以作为函数参数,还可以作为函数返回值 def f(n) ...
- Akka边学边写(2)-- Echo Server
EchoServer 上篇文章里,我们用Akka写了一个简单的HelloWorld样例,对Akka(以及Actor模式)有了初步的认识.本文将用Akka写一个EchoServer,看看在Actor的世 ...
- 完美实现同时分享图片和文字(Intent.ACTION_SEND)
private void share(String content, Uri uri){ Intent shareIntent = new Intent(Intent.ACTION_SEND); if ...
- 转:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...
- PHP学习笔记十【数组】
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/h ...
- HTTP状态码搜集
一.1xx消息 这一类型的状态码,代表请求已经接受,需要继续处理. 这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束. 由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...
- asp生命周期
笔记:如果不对,欢迎指出来. 当一个用户访问一个页面,实际是发送http请求到服务器,而http请求其实是socket发送特定规范(协议)的文字(报文). 然后服务器的http.sys内核模块接受 请 ...