LeetCode-326. Power of Three

Description:

Given an integer, write a function to determine if it is a power of three.

判断一个整数是否是3的幂。

基于公式log3^n可以得出y=lg(n)/log(3)如果y正好是个整数的话,说明n是3的幂。也可以使用循环除3的方法,也不是很浪费时间循环不了多少次。

public class Solution {
    public boolean isPowerOfThree(int n) {

        double ans = Math.log(n) / Math.log(3);
        return Math.abs(ans - Math.round(ans)) < 0.000000000001;
    }
}

时间: 2024-12-28 21:00:06

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

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. Follow up: Could you do it without using any loop / recursion? 思路:看这个数取以3为底的对数结果是否为整数,C++中只有自然对数函数log()和以10为底的对数函数log10(),所以要借助换底公式.此处用自然对数会有精度问题,用以10为底的对数

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

翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你是否可以不用任何循环或递归来完成. 原文 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? 分析 题意我其实不是满懂,比如说12到底可不可以呢?还是说只有:3.9.27.81这种才行呢?先写

LeetCode 231 Power of Two(2的幂)

翻译 给定一个整型数,写一个函数来决定它是否是2的幂. 原文 Given an integer, write a function to determine if it is a power of two. 分析 详情请看这篇文章:LeetCode 326 Power of Three(3的幂)(递归.Log函数) 看题号,326是本题的加强版,326是要求不能用循环或递归的--大家可以去看看上面那篇文章. 本题就直接贴我的代码了-- 代码 class Solution { public: bo

326. Power of Three

/* * 326. Power of Three * 2016-7-8 by Mingyang * 中规中矩,不过注意,n不能为0,不然while一直走,也不能为负 * 时间logn,空间到是没有要求 */ public boolean isPowerOfThree(int n) { if (n < 1) { return false; } while (n % 3 == 0) { n /= 3; } return n == 1; }

326. Power of Three(LeetCode)

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? 1 class Solution { 2 public: 3 bool isPowerOfThree(int n) { 4 if (n == 0) 5 return false; 6 while (n != 1) 7 { 8

[LeetCode][JavaScript]Power of Three

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? https://leetcode.com/problems/power-of-three/ 要求判断一个数是不是3的幂次方. 最直观的做法是不停除以3,看余数是否为零. 题目要求不能用递归和循环,

【Leetcode】Power of Four

题目链接:https://leetcode.com/problems/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 lo

[LeetCode][JavaScript]Power of Two

Power of Two Given an integer, write a function to determine if it is a power of two. https://leetcode.com/problems/power-of-two/ 二分. 2的整数次幂,要么开方后是整数(这个数也是2的整数次幂),要么除以2之后再开方后是整数.继续递归判断开方后的结果直到碰到1或者2. 比如1024(2^10),是由两个32(2^5)相乘:2048(2^11),是由两个32(2^5)相

[LeetCode] Reordered Power of 2 重新排序为2的倍数

Starting with a positive integer?N, we reorder the digits in any order (including the original order) such that the leading digit is not zero. Return?true?if and only if we can do this in a way such that the resulting number is a power of 2. Example

leetcode 231. Power of Two

Given an integer, write a function to determine if it is a power of two. class Solution { public: bool isPowerOfTwo(int n) { int num = 0; while (n > 0) { if (n &1) num++; n/=2; } if (num == 1) return true; return false; } };