【一天一道LeetCode】#326. Power of Three
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
(二)解题
题目大意:判断一个是不是3的n次方。
解题思路:很容易就想到将该数每次都除以3,如果整除则继续除,直到等于1就表示是3的n次方,否则就不是。
class Solution {
public:
bool isPowerOfThree(int n) {
int num=n;
while(num>0 && num%3==0) //整除
num/=3;
return num==1;
}
};
但是题目中提到,能不能不用循环或者递归来解题。可是想了半天都没有想到,无奈只能求助百度了。
在int范围内的3的n次方最大为3的19次方1162261467,如果num时3的n次方数的话,一定能被1162261467整除。
于是有下面的代码:
class Solution {
public:
bool isPowerOfThree(int n) {
return n>0?(1162261467%n==0?true:false):false;
}
};
还有的方法就是列举法,也就19个数,判断跟这19个数中任意一个相不相等即可。
【一天一道LeetCode】#326. Power of Three的更多相关文章
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- 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 ...
- [LeetCode] 326. Power of Three 3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- 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 ...
- 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 ...
- LeetCode 326 Power of Three(3的幂)(递归、Log函数)
翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...
- leetcode 326 Power of Three (python)
原题: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...
- Leetcode 326 Power of Three 数论
判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) = 1162261467)的约数 这种方法是我 ...
- [LeetCode] 326. Power of Three + 342. Power of Four
这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
随机推荐
- Django中ORM操作
ORM操作: class UserInfo(models.Model): username = models.CharField(max_length=32) password = models.Ch ...
- python中读取文件数据时要注意文件路径
我们在用python进行数据处理时往往需要将文件中的数据取出来做一些处理,这时我们应该注意数据文件的路径.文件路径不对,回报如下错误: FileNotFoundError: File b'..Adve ...
- Postgresql 创建SEQUENCE,Springboot中使用KeyHolder
项目中使用到JdbcTemplate中的KeyHolder,代码如下: String sql = "insert into web_users(username, password, pho ...
- 特殊权限 SUID、SGID、Sticky
摘录之----------QuintinX 一. 前提 本篇主要讲解SUID, SGID, Sticky三个权限的基本原理和应用. 为什么要使用特殊权限? 比如系统中假如有超过四类人然而每一类人都需要 ...
- [Java] 设计模式:代码形状 - lambda表达式的一个应用
[Java] 设计模式:代码形状 - lambda表达式的一个应用 Code Shape 模式 这里介绍一个模式:Code Shape.没听过,不要紧,我刚刚才起的名字. 作用 在应用程序的开发中,我 ...
- idea-JSP out.println报错问题
<%! out.println("xxxx");%> 上面是错误的,<%!%>是声明变量是使用,而不是进行逻辑输出! <% out.println(x ...
- oracle拆分一个连续的字符串
create or replace procedure pc( sss out varchar2)isstr varchar2(20):='ph,p,cod,do,cu';en integer:=i ...
- c++ 深入理解数组
阅读前提:你得知道啥是数组. 本文需要弄清楚的问题如下: 1,数组作为函数参数,传入的是值,还是地址? 2,数组作为函数参数,数组的长度能否确定? 解决如下 1,数组作为函数参数,传入的是地址.因为数 ...
- Spring Boot Cache Redis缓存
1.集成MyBatis 1.1.引入maven依赖 1.2.生成Mapper 具体可以看MyBatis Generator官网 http://www.mybatis.org/generator/run ...
- .NET Core 网络数据采集 -- 使用AngleSharp做html解析
有这么一本Python的书: <<Python 网络数据采集>> 我准备用.NET Core及第三方库实现里面所有的例子. 这是第一部分, 主要使用的是AngleSharp: ...