[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?

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

前提学习:
Java Math的floor,round,ceil函数小结

想法:

3^x=n

log(3^x) = log(n)

x log(3) = log(n)

x = log(n) / log(3)

We need to

largest number power of 3, is 1162261467, we can use this to calculate the float precision. 0.0000000001 at least

Math.log(1162261468)/log(3)

private static final double epsilon = 10e-15;

    public boolean isPowerOfThree(int n) {
        if (n == 0)
            return false;
        double res = Math.log(n) / Math.log(3);
        return Math.abs(res - Math.round(res)) < epsilon;
    }

常规办法(AC过的):

public boolean isPowerOfThree(int n) {
        if (n == 0)
            return false;
        if (n == 1)
            return true;
        if (n % 3 == 0)
            return isPowerOfThree(n / 3);
        return false;
    }
时间: 2024-10-30 06:38:43

[LeetCode]--326. Power of Three的相关文章

[LeetCode]231.Power of Two

题目 Given an integer, write a function to determine if it is a power of two. 代码 /*--------------------------------------- * 日期:2015-08-02 * 作者:SJF0115 * 题目: 231.Power of Two * 网址:https://leetcode.com/problems/power-of-two/ * 结果:AC * 来源:LeetCode * 博客:

[LeetCode]--342. Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? Credits: Special thanks to @yukuairoy f

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode] Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 解题思路 如果一个数为4n,则它等于n个4相乘.乘以4可以化为左移2位的位运算

《Power Designer系统分析与建模实战》——3.2 建立业务处理模型

3.2 建立业务处理模型 创建业务处理模型主要有如下两种形式: 1)直接新建BPM. 2)从已有的BPM生成新的BPM. 本节主要讲解在Power Designer中直接新建BPM的方法. 3.2.1 创建BPM 选择"File"→"New"菜单项,从弹出的新建模型窗口中选择"Model types"→ "Business Process Model"→"Business Process Diagram",

[LeetCode] Number of 1 Bits &amp;amp; Reverse Integer - 整数问题系列

目录:1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历]3.Reverse Integer - 翻转整数 [int边界问题]4.Excel Sheet Column Number - Excel字符串转整数 [简单]5.Power of Two & Happy Number - 计算各个位数字 [%10 /10] 一.Number of 1 Bits  题目概述:Write a function t

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

Power to Give计划让手机获得等同于超级计算机的运算能力

摘要: 据说,在分布式计算飞速发展的当下改进超级计算机已经没有意义,比如HTC的Power to Give计划就能让你的手机获得等同于超级计算机的运算能力.即便如此,将超级计算机小型化的尝试还 据说,在分布式计算飞速发展的当下改进超级计算机已经没有意义,比如HTC的Power to Give计划就能让你的手机获得等同于超级计算机的运算能力.即便如此,将超级计算机小型化的尝试还是极具价值的,毕竟在很多场景下我们不能保证设备与云的连通. 据Engadget报道,昨天NVIDIA CEO黄仁勋在该公司

代码分析-leetcode:Scramble String问题

问题描述 leetcode:Scramble String问题 题目:Given a string s1 we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = ""great"": great / gr eat / / g r e at /