/*
* @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. 图书管理系统 基于form组件

    models: from django.db import models # Create your models here. class Book(models.Model): name = mod ...

  2. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)

    前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详细介绍 Nginx + PHP 安装.配置.使用的资料之一,为推动 Nginx ...

  3. Mybatis学习---基础知识考核

    MyBatis 2.什么是MyBatis的接口绑定,有什么好处 接口映射就是在IBatis中任意定义接口,然后把接口里面的方法和SQL语句绑定,    我们直接调用接口方法就可以,这样比起原来了Sql ...

  4. c# 内存泄漏检查心得

    系统环境 windows 7 x64 检查工具:ANTS Memory Profiler 7 或者 .NET Memory Profiler 4.0 开发的软件为winform / windows s ...

  5. January 09 2017 Week 2nd Monday

    Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实是甜蜜的. Some patience may be just fruitless, o ...

  6. Kafka生产者producer简要总结

    Kafka producer在设计上要比consumer简单,不涉及复杂的组管理操作,每个producer都是独立进行工作的,与其他producer实例之间没有关联.Producer的主要功能就是向某 ...

  7. HDFS Namenode&Datanode

    HDFS Namenode&Datanode HDFS 机制粗略示意图 客户端写入文件流程: NN && DN Namenode(NN)工作机制 NN是整个文件系统的管理节点. ...

  8. 【4】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(色彩与绘图)】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  9. mysql安装linux_二进制包安装

    1.下载(本地下载www.mysql.com ----->DOWNlOADS------>Archives----->MySQL Community Server---->Li ...

  10. 【node】安装

    参考链接: http://www.runoob.com/nodejs/nodejs-install-setup.html http://blog.csdn.net/u010255310/article ...