Leecode刷题之旅-C语言/python-326 3的幂
/*
* @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的幂的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
随机推荐
- 绘制虚线的UIView
绘制虚线的UIView CAShapeLayer配合贝塞尔曲线可以绘制曲线,笔者继承了一个UIView的子类,并将该子类的backedLayer替换为CAShapeLayer,以此来实现绘制虚线的效果 ...
- C#:安装Windows服务,动态指定服务名及描述(转载)
来源:http://www.cnblogs.com/Fooo/p/3476675.html Installer.cs public Installer() { InitializeComponent( ...
- 有用的JS函数
1. QueryString function queryString(key) { var re = new RegExp("[?&]" + key + "=( ...
- 深入浅出SharePoint——Caml快速开发
适用于Visual Studio 2010的Caml智能感知工具 http://visualstudiogallery.msdn.microsoft.com/15055544-fda0-42db-a6 ...
- 深入浅出SharePoint2012——安装Report Service
安装顺序 Microsoft .NET Framework 3.5 SP1 report service installation,pls SQLServer2008R2SP1-KB2528583-x ...
- [BZOJ 1124][POI 2008] 枪战 Maf
1124: [POI2008]枪战Maf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 659 Solved: 259[Submit][Status ...
- Python2.7 - IMOOC - 2
第三章 Python变量和数据类型 3-1.数据类型 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,表示方法和数学上的写法一模一样,十 ...
- CRITICAL **: Couldn't acquire global lock, snapshots will not be consistent: Access denied
报错如下:** (mydumper:56288): CRITICAL **: Couldn't acquire global lock, snapshots will not be consisten ...
- Gym 100633G Nano alarm-clocks
题目,给定n个时钟,要求把他们调成一样的时间.求最小的步数 思路:肯定是有一个时钟作为标准的啦,要找到这个时钟,怎么找呢?没其他方便的方法,暴力枚举.那么枚举后,怎么能快速地算到其他时钟转到这个时钟的 ...
- [19/04/14-星期日] 网络编程_java.net包(InetAddress类、InetSocketAddress类、URL类)
一.概念 Java为了可移植性,不允许直接调用操作系统,而是由java.net包来提供网络功能.Java虚拟机负责提供与操作系统的实际连接. InetAddress 作用:封装计算机的IP地址和 ...