/*
* @lc app=leetcode.cn id=326 lang=c
*
* [326] 3的幂
*
* https://leetcode-cn.com/problems/power-of-three/description/
*
* algorithms
* Easy (42.85%)
* Total Accepted: 14.2K
* Total Submissions: 33.1K
* Testcase Example: '27'
*
* 给定一个整数,写一个函数来判断它是否是 3 的幂次方。
*
* 示例 1:
*
* 输入: 27
* 输出: true
*
*
* 示例 2:
*
* 输入: 0
* 输出: false
*
* 示例 3:
*
* 输入: 9
* 输出: true
*
* 示例 4:
*
* 输入: 45
* 输出: false
*
* 进阶:
* 你能不使用循环或者递归来完成本题吗?
*
*/
bool isPowerOfThree(int n) {
if(n<=){
return false;
}
while(n>){
if(n%!=) return false;
n /=;
}
return true;
}

和2的幂思路一样的算法。

但是题目有个进阶要求 不使用循环或者递归

这里计算最大的3的幂次方的数 然后判断n能否被这个数整除即可。

/*
* @lc app=leetcode.cn id=326 lang=c
*
* [326] 3的幂
*
* https://leetcode-cn.com/problems/power-of-three/description/
*
* algorithms
* Easy (42.85%)
* Total Accepted: 14.2K
* Total Submissions: 33.1K
* Testcase Example: '27'
*
* 给定一个整数,写一个函数来判断它是否是 3 的幂次方。
*
* 示例 1:
*
* 输入: 27
* 输出: true
*
*
* 示例 2:
*
* 输入: 0
* 输出: false
*
* 示例 3:
*
* 输入: 9
* 输出: true
*
* 示例 4:
*
* 输入: 45
* 输出: false
*
* 进阶:
* 你能不使用循环或者递归来完成本题吗?
*
*/
bool isPowerOfThree(int n) {
if(n<=)
{
return false;
}
int max3Power=(int)pow(,(int)(log(0x7fffffff)/log()));
if (max3Power%n==)
{
return true;
}
else
{
return false;
}
}

--------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=326 lang=python3
#
# [326] 3的幂
#
# https://leetcode-cn.com/problems/power-of-three/description/
#
# algorithms
# Easy (42.85%)
# Total Accepted: 14.2K
# Total Submissions: 33.1K
# Testcase Example: '27'
#
# 给定一个整数,写一个函数来判断它是否是 3 的幂次方。
#
# 示例 1:
#
# 输入: 27
# 输出: true
#
#
# 示例 2:
#
# 输入: 0
# 输出: false
#
# 示例 3:
#
# 输入: 9
# 输出: true
#
# 示例 4:
#
# 输入: 45
# 输出: false
#
# 进阶:
# 你能不使用循环或者递归来完成本题吗?
#
#
class Solution:
def isPowerOfThree(self, n: int) -> bool:
return n > 0 and 3**19 % n == 0

Leecode刷题之旅-C语言/python-326 3的幂的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  7. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  8. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  9. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

随机推荐

  1. ASP.NET Page执行顺序如:OnPreInit()、OnInit()……

    using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...

  2. asp.net 服务器控件的 ID,ClientID,UniqueID 的区别

    1.简述 ID是设计的时候自己所指定的ID,是我们分配给服务器控件的编程标识符,我们常常使用this.controlid来寻找控件,那么这个controlid就是这里所说的ID. ClientID是由 ...

  3. DAO,SERVICE

  4. centos yum升级php

    centos yum升级php5.3.3到最5.6.3 不要轻易升级,否则后果很严重! 注意事项: 1 升级后之前的php扩展不会丢失 自动会安装对应最新php的扩展2 升级后需重启下apache 才 ...

  5. 一点二次插值、二点二次插值 ,matlab

    syms f x a b c; f(x)=3*x^4-4*x^3-12*x^2; q(x)=a*x^2+b*x+c; %二点二次插值 x=[-1.2 -0.8]; ff=diff(diff(f)); ...

  6. CopyrightHelper—开源VS插件辅助插入版权注释

    前言 有很多时候,我们在写代码的时候需要在代码文件头加上描述和版权信息等,如果使用代码项目模板又得为每种文件定模板,而已不方便,如果从某个地方复制过来,又嫌麻烦... 为了能解决这种懒人的需求,我开始 ...

  7. August 09th 2017 Week 32nd Wednesday

    Find hope from despair, life will become brilliant. 从绝望中寻找希望,人生终将辉煌. Have you ever seen the movie Ba ...

  8. iPhone的设备名转换

    def convertDeviceName(self, deviceName): """ 转换deviceName(如iPhone 6,2)为用户习惯形式(如iPhone ...

  9. js url传值中文乱码完美解决(JAVA)

    js url传值中文乱码完美解决(JAVA) 首先在你的jsp页面这样更改: var url="你要传入的Action的位置&ipid="+ipid+"& ...

  10. 简单说说Spring Security 使用(附加验证码登录,自定义认证)

    先看官方文档:http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/ spring security4已 ...