LeetCode----326. Power of Three(Java)

 1 package isPowerOfThree326;
 2 /*
 3  Given an integer, write a function to determine if it is a power of three.
 4  */
 5 public class Solution {
 6     /*
 7     //题目理解错误,理解成3次开方
 8     public static boolean isPowerOfThree(int n) {
 9         if (n==1)
10             return true;
11         else{
12             for(int i=2;i<=Math.sqrt(n);i++){
13                 if(n%i==0){
14                     n=n/i;
15                     if(n%i==0){
16                         n=n/i;
17                         if (n%i==0){
18                             n=n/i;
19                             return isPowerOfThree(n);
20                         }
21                         else
22                             return false;
23                     }
24                     return false;
25                 }
26             }
27             return false;
28         }
29     }
30     */
31     public static boolean isPowerOfThree(int n){
32         if (n==1)
33             return true;
34         else if (n==0)
35             return false;
36         else if (n%3==0){
37             n=n/3;
38             return isPowerOfThree(n);
39         }
40         else
41             return false;
42
43
44     }
45     public static void main(String[] args) {
46         // TODO Auto-generated method stub
47         System.out.println(isPowerOfThree(-3));
48     }
49
50 }
时间: 2024-10-07 03:54:56

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

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; }

leetcode 118 Pascal&#39;s Triangle ----- java

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 用的比价暴力的方法, 也是最快的. public class Solution { List list = new ArrayList<List<Integer>>(); public

LeetCode之数组处理题java

342. Power of Four Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy 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. public class

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

Java [Leetcode 231]Power of Two

题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0. 代码如下: public class Solution { public boolean isPowerOfTwo(int n) { return (n > 0) && ( n & (n - 1)) == 0; } }

Java for LeetCode 231 Power of Two

public boolean isPowerOfTwo(int n) { if(n<1) return false; while(n!=1){ if(n%2!=0) return false; n>>=1; } return true; }

LeetCode 342. Power of Four (4的次方)

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? 题目标签:Bit Manipulation 这道题目让我们判断一个数字是不是4的