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 ...
随机推荐
- cf475B Strongly Connected City
B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 剑指offer-面试题17.合并两个排序的链表
题目:输入两个递增的排序的链表,合并这两个链表并使新链表中的节点仍然是 按照递增排序的.例如链表1链表2合并为链表3. List1:->->-> List2:->->-& ...
- Unity扩展 四种Menu的区别
[MenuItem("Tools\AddColor")] : 在Unity菜单中添加一种快捷,执行public static方式 [AddComponentMenu(" ...
- IOS 计算密码强度
+ (BOOL) judgeRange:(NSArray*)conditionArr Password:(NSString*)password { NSRange range; BOOL result ...
- Spring(二)——IoC
IoC(Inversion of Control)称之为控制反转,指的是在Spring框架的配置文件中声明对象,由框架负责创建对象,这叫做控制反转.实现方式有两种:DI(Dependency Inje ...
- 玩转iOS开发 - JSON 和 Xml 数据解析
前言 Json 和xml是网络开发中经常使用的数据格式,JSON轻量级.xml相对较复杂.所以如今用JSON的比例很大.基本上从server获取的返回数据都是JSON格式的,作为iOS开发人员,解析J ...
- Unity 3D 连接Mysql数据库
要想使用Unity直接连接数据库需要以下几个动态库
- stagefright框架(七)-Audio和Video的同步
讲完了audio和video的处理流程,接下来要看的是audio和video同步化(synchronization)的问题.OpenCORE的做法是设置一个主clock,而audio和video就分别 ...
- 11g导入大量包含子分区的数据时表空间不足
问题描述: ORACLE11g使用impdp数据泵导入时遭遇: ORA-01691: Lob 段 ISCS.SYS_LOB0000100750C00045$$ 无法通过 128 (在表空间 RT_DA ...
- UITableView 隐藏多余的分割线
//隐藏多余的分割线 - (void)setExtraCellLineHidden: (UITableView *)tableView { UIView *view =[ [UIView alloc] ...