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 without using any loop / recursion?
Summary:
用非循环/递归的方法判断数n是否为3的整数幂。
Analysis:
1. 循环:将n逐次除以3,判断是否为3的整数幂。
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= ) {
return false;
}
while (n > ) {
if (n % ) {
return false;
}
n /= ;
}
return true;
}
};
2. 递归:若n为3的整数幂,n/3必为3的整数幂,以这个思想构建递归。
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= ) {
return false;
}
if (n == ) {
return true;
}
return (n % == ) && isPowerOfThree(n / );
}
};
3. 非循环/递归:n若为3的整数幂,则 n = 3log3n = 3log2n / log 23
class Solution {
public:
bool isPowerOfThree(int n) {
return (n > ) && (n == pow(, round(log(n) / log())));
}
};
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 ...
- 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: ...
随机推荐
- web网页颜色色谱
snow 255 250 250 #fffafa ghostwhite 248 248 255 #f8f8ff whitesmoke 245 245 245 #f5f5f5 gainsboro 220 ...
- LYDSY模拟赛day9 2048
/* 大模拟题,做的时候思路还是比较清晰的 */ #include<iostream> #include<cstdio> #include<string> #inc ...
- 计划安装SQL Server2012需求详细
1.查看 SQL Server2012 安装的安装要求.系统配置检查和安全注意事项. 1.1 硬件要求 [参考资料http://msdn.microsoft.com/zh-cn/library/ms1 ...
- iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例
一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...
- BZOJ1251——序列终结者
给你一个数列,让你实现区间加上一个值,区间翻转,区间最大值 裸splay,懒标记一发即可 #include <cstdio> #include <cstdlib> #inclu ...
- autolayout的各种坑
xocde7的autolayout 在viewDidLoad之前, 使用frame改变布局是没有用的, 简单的视图才可以使用autolayout, 稍微复杂写的都要使用代码来编写 获取当前view的宽 ...
- STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...
- C语言的执行
在ANSIC 的任何一种实现中,有两种不同的环境 翻译环境:将源代码转换为可执行的机器代码 执行环境:用于执行代码 这两种环境可以运行于同一个机器上,也可以运行于不同的机器上 例如交叉编译器:在一台机 ...
- 二分图------》Hopcroft-Karp算法 hdu2389
Hopcroft-Karp算法 该算法由John.E.Hopcroft和Richard M.Karp于1973提出,故称Hopcroft-Karp算法. 原理 为了降低时间复杂度,可以在增广匹配集合M ...
- intellij Idea快捷键
CTRL+ALT+O 优化导入的类和包 Alt + Center 导入类,实现接口 CTRL+N 查找类CTRL+SHIFT+N 查找文件CTRL+SHIFT+ALT+N 查找类中的方法或变 ...