给出一个整数,写一个函数来确定这个数是不是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

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

  1. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  2. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  3. 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 ...

  4. <LeetCode OJ> 326. Power of Three

    326. Power of Three Question Total Accepted: 1159 Total Submissions: 3275 Difficulty: Easy 推断给定整数是否是 ...

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

    翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...

  6. 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 ...

  7. 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 ...

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

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...

  9. 【LeetCode】326. Power of Three 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 递归 取对数 判断是不是最大3的倍数的因子 日 ...

随机推荐

  1. array copy rotate in Pointwise

    goal: # # Copyright 2010 (c) Pointwise, Inc. # All rights reserved. # # This sample Pointwise script ...

  2. BZOJ 4385 洛谷3594 POI2015 WIL-Wilcze doły

    [题解] 手残写错调了好久QAQ...... 洛谷的数据似乎比较水.. n个正整数!!这很重要 这道题是个类似two pointer的思想,外加一个单调队列维护当前区间内长度为d的子序列中元素之和的最 ...

  3. PAT 1122 Hamiltonian Cycle

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  4. 邮票(codevs 2033)

    题目描述 Description 已知一个 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 例如,假设有 ...

  5. 稍微成型点的用WEBSOCKET实现的实时日志LOG输出

    难的是还是就地用JS显示出来相关的发布进度. 还好,花了一下午实现了. 可以移植到项目中去罗... websocket.py: import tornado.ioloop import tornado ...

  6. N - Is It A Tree? 并查集

    A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one ...

  7. Mutual Training for Wannafly Union #5

    A(UVA12336) 题意:给一个n*m(n,m<=1e5)的棋盘,棋盘上有一些障碍点不能放棋子,现在要在棋盘上放4个棋子,满足A->B->C->D->A,其中走的规则 ...

  8. 友盟 个推 微信sdk spring boot

    友盟 个推 微信sdk spring boot ngix mongodb memory cache

  9. jquery-mobile 学习笔记之二(表单创建)

    绪上 一.注意事项 1. <form> 元素必须设置 method 和 action 属性 2. 每一个表单元素必须设置唯一的 "id" 属性. 该 id 在网站的页面 ...

  10. 改善java程序的151个建议--数组和集合

    60.性能考虑,数组是首选,在基本类型处理方面.数组还是占优势的,并且集合类的底层也都是通过数组实现.建议在性能要求较高的场景中使用数组替代集合. 61.假设有必要.使用变长数组:我们能够通过对数组扩 ...