LeetCode--326--3的幂
问题描述:
给定一个整数,写一个函数来判断它是否是 3 的幂次方。
示例 1:
输入: 27
输出: true
示例 2:
输入: 0
输出: false
示例 3:
输入: 9
输出: true
示例 4:
输入: 45
输出: false
方法:取243时,会出错。log(243,3) == 4.9999... 用round 四舍五入。(时间太长)
import math
class Solution: def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n > 0:
return pow(3,round(math.log(n,3))) == n
else:
return False
官方:3^19=1162261467是小于2^31最大的3的倍数
class Solution:
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
maxThreeInt = 3**19 return n > 0 and maxThreeInt % n == 0
循环:
import math
class Solution: def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n > 0:
if n == 1:
return True
else:
k = 0
while k == 0 :
n = n / 3.0
k = n % 3
if n == 1:
return True
else:
return False
else:
return False
2018-09-25 21:03:03
LeetCode--326--3的幂的更多相关文章
- Java实现 LeetCode 326 3的幂
326. 3的幂 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: tru ...
- Leetcode 326.3的幂 By Python
给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输 ...
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- LeetCode 231.2的幂
LeetCode 231.2的幂 题目: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 算法: 若一个数是2的幂次的话定会有n & (n - 1) == 0这个关系成立 所以直接用 ...
- 不使用循环或递归判断一个数是否为3的幂(leetcode 326)
326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...
- leetcode刷题笔记326 3的幂
题目描述: 给出一个整数,写一个函数来确定这个数是不是3的一个幂. 后续挑战:你能不使用循环或者递归完成本题吗? 题目分析: 既然不使用循环或者递归,那我可要抖机灵了 如果某个数n为3的幂 ,则k=l ...
- LeetCode 326 Power of Three(3的幂)(递归、Log函数)
翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...
- 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 ...
- Leetcode 326 Power of Three 数论
判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) = 1162261467)的约数 这种方法是我 ...
- 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 ...
随机推荐
- Python3基础 list list()生成空列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Restful framework【第十一篇】url路由控制
基本使用 -url控制 -传统的url配置 url(r'^books/$', views.BookView.as_view()), url(r'^books/(?P<pk>\d+)$', ...
- Android O HIDL的实现对接【转】
本文转载自:https://blog.csdn.net/gh201030460222/article/details/80551897 Android O HIDL的实现对接1. HIDL的定义1.1 ...
- tp框架中的一些疑点知识-1
tp默认的编码是utf-8 Runtime中的Cache和Logs都是分模块的,因为在应用app下可以有多个模块,但是 公共模块和Runtime模块只有一个, 所以, Runtime要包含各个模块的内 ...
- SpringBoot 使用validation数据校验
后端对数据进行验证 添加包 hibernate-validator <!-- https://mvnrepository.com/artifact/org.hibernate.validator ...
- OAuth2.0原理与实现
弄懂了原理流程,才可以搭建出来.更重要的是,可以根据原理流程自定义搭建,甚至可以完全自己实现一套,最后运行效果和原理和这个对得上就成功了,不要总期待标准答案! 首先参考两篇博客: 阮一峰的博客以及张开 ...
- C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!
https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new Ope ...
- Cisco 2960交换机配置
一. 基本操作 Switch(config)#hostname test01(交换机名称) //全局模式下修改交换机名称 Switch(config)#enable secret 123456 //全 ...
- [idea] - 项目启动报错Process finished with exit code 1
今天运行项目发现一个bug, "C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -XX:TieredStopAtLevel=1 - ...
- Cas单点登录配置数据查询用户
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...