326 Power of Three 3的幂

给出一个整数,写一个函数来确定这个数是不是3的一个幂。
后续挑战:
你能不使用循环或者递归完成本题吗?

详见:https://leetcode.com/problems/power-of-three/description/

C++:

方法一:

class Solution {
public:
    bool isPowerOfThree(int n) {
        while(n&&n%3==0)
        {
            n/=3;
        }
        return n==1;
    }
};

方法二:

class Solution {
public:
    bool isPowerOfThree(int n) {
        return (n>0&&1162261467%n==0);
    }
};

参考:https://www.cnblogs.com/grandyang/p/5138212.html

原文地址:https://www.cnblogs.com/xidian2014/p/8832583.html

时间: 2024-11-05 15:52:33

326 Power of Three 3的幂的相关文章

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

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为底的对数

Uva 11149 - Power of Matrix ( 矩阵快速幂 )

Uva 11149 -Power of Matrix ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 10 #define MAX_SIZE 40 struct Mat { int r, c; int mat[MAX_SIZE][MAX_SIZE]; Mat( int _r = 0 , int _c = 0 ) { CLR

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这种才行呢?先写

POJ 3233 Matrix Power Series(矩阵快速幂)

Default Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15553 Accepted: 6658 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one test

Matrix Power Series(矩阵快速幂)

C - Matrix Power Series Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3233 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. Input The input

POJ3233 Matrix Power Series【矩阵快速幂】

题目链接: http://poj.org/problem?id=3233 题目大意: 给定一个N*N的矩阵A和一个整数K,要求计算S = A + A^2 + A^3 + - + A^k. 思路: 分别用矩阵快速幂求出每一项的A^i,然后将每一项矩阵相加,考虑到k值很大,所有采用 二分求解. AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using

LeetCode Power of Two (2的幂)

题意:判断1个数n是否刚好是2的幂,幂大于0. 思路:注意会给负数,奇数.对于每个数判断31次即可. 1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 if(n<1||(n&1)==1&&n>1) return false; 5 unsigned int t=1; 6 while(t<n) //注意爆int 7 t<<=1; 8 if(t==n) return true; 9 ret

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