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: ...
随机推荐
- Mysql CPU占用90%
今天网站打开卡,查了下发现Mysql CPU占用到90%! 1.通过putty工具连接mysql putty工具十分的强大,它可以让我们直接访问MySQL.当然,要实现这一步肯定要做点什么.MySQL ...
- netstat命令的常见用法(转)
netstat 的10个基本用法 Netstat 简介 Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监 ...
- iOS开发关于Block代码错误
本文永久地址为http://www.cnblogs.com/ChenYilong/p/4052362.html ,转载请注明出处. iOS开发关于Block代码错误 Incompatible bloc ...
- firefox浏览器不能使用window.close的解决方案
javascript中window.close()函数用来关闭窗体,而且IE.google.firefox浏览均支持,但由于firefox浏览器dom.allow_scripts_to_close_w ...
- webrtc第一篇
1.介绍 众所周知,浏览器本身不支持相互之间直接建立信道进行通信,都是通过服务器进行中转.比如现在有两个客户端,甲和乙,他们俩想要通信,首先需要甲和服务器.乙和服务器之间建立信道.甲给乙发送消息时,甲 ...
- vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面
vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...
- Entity Framework 之Database first(数据库优先)&Model First(模型优先)
一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...
- this.getServletContext().getRealPath("WEB-INF");
this.getServletContext().getRealPath("WEB-INF");
- 服务器如何处理http请求
1.需求 了解服务端如何处理http请求,了解基本的处理流程 2.实战 处理http请求分为7个步骤 2.1 Tcp连接 建立一条tcp链接,(若之前不存在持久链接keep-alive),把客户端的i ...
- sql server 2008 R2 不允许保存更改,您所做的更改要求删除并重新创建以下表
点击菜单栏 [工具]->[选项] 进入如下界面: 将阻止保存要求重新创建表的更改 的勾去掉即可.