【一天一道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: ...
随机推荐
- Spring使用@Scheduled定时调度
一.spring配置文件中增加对这个注解的支持: 配置文件如下: <?xml version="1.0" encoding="UTF-8"?> &l ...
- Java8的重要新特性
一.Lambda表达式 java8中Lambda表达式的书写方式: (参数) -> 表达式 (参数) -> 单行语句 (参数) -> { 语句 } 1.Lambda遍历List和Ma ...
- SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理
GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...
- 关于一些基础的Java问题的解答(七)
31. 反射的作用与原理 简单的来说,反射机制其实就是指程序在运行的时候能够获取自身的信息.如果知道一个类的名称或者它的一个实例对象, 就能把这个类的所有方法和变量的信息(方法名,变量名,方法,修饰符 ...
- WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式
ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...
- webstorm2017破解
选择"license server" 输入:http://idea.imsxm.com/ 2017支持vue了
- TP中的AJAX返回ajaxReturn()
系统支持任何的AJAX类库,Action类提供了ajaxReturn方法用于AJAX调用后返回数据给客户端.并且支持JSON.XML和EVAL三种方式给客户端接受数据,通过配置DEFAULT_AJAX ...
- gravity和layout_gravity的区别
一.gravity和layout_gravity相同处 两者都是设置对齐方式的属性.内部的属性值相同. 根据英文意思也能理解其中的意思.如center_horizontal表示在水平方向上的位置为中间 ...
- centos 7 安装nvidia显卡驱动
How to install Nvidia drivers in CentOS 7 - Tutorial : http://www.dedoimedo.com/computers/centos-7- ...
- 蚂蚁代理免费代理ip爬取(端口图片显示+token检查)
分析 蚂蚁代理的列表页大致是这样的: 端口字段使用了图片显示,并且在图片上还有各种干扰线,保存一个图片到本地用画图打开观察一下: 仔细观察蓝色的线其实是在黑色的数字下面的,其它的干扰线也是,所以这幅图 ...