LeetCode 326 Power of Three(3的幂)(递归、Log函数)
翻译
给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适……
跟进:
你能否够不用不论什么循环或递归来完毕。
原文
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?
分析
题意我事实上不是满懂,比方说12究竟可不能够呢?还是说仅仅有:3、9、27、81这样的才行呢?先写个简单的递归试试看吧。
bool isPowerOfThree(int n) {
if (n == 1) return true;
else if (n == 0) return false;
else if (n % 3 == 0)
return isPowerOfThree(n / 3);
else return false;
}
提交成功了,那么自己用12作为參数来试试呢,发现返回false。那么就能够断定题意是上面我说的另外一种了。
是否还记得log函数呢。之前有一道题遇到过。所以这次一下就想到了……
logn3
假设以12来计算的话:
log123=2.26186
int(log123)=2
log123−int(log123)=0.26186
所以直接推断结果是否为0就好了……
bool isPowerOfThree(int n) {
double logAns= log(n) / log(3);
return (logAns- int(logAns) == 0) ? true : false;
}
然而这段代码提交后发现仍然是错误的,243在上面的代码中竟然是false。打断点看看应该是因为精度问题,所以继续改改。
logn3=logn10log310
所以代码就出来了……
代码
class Solution {
public:
bool isPowerOfThree(int n) {
double logAns = log10(n) / log10(3);
return (logAns - int(logAns) == 0) ?
true : false;
}
};
LeetCode 326 Power of Three(3的幂)(递归、Log函数)的更多相关文章
- 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
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 ...
- 326 Power of Three 3的幂
给出一个整数,写一个函数来确定这个数是不是3的一个幂.后续挑战:你能不使用循环或者递归完成本题吗? 详见:https://leetcode.com/problems/power-of-three/de ...
- [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 数论
判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) = 1162261467)的约数 这种方法是我 ...
- 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 + 342. Power of Four
这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...
随机推荐
- docker 实战---多台物理主机的联网,容器桥接到物理网络拓扑图(四)
非常多朋友说上一篇中对网络的描写叙述不够清楚,感谢热心的群友彩笔程序猿: 提供了他理解的图,在这里贴一下: 我自己也补画了一副多台机器互联的图,欢迎大家留言讨论: 主机A和主机B的网卡一都连着物理交换 ...
- 基于Windows下使用Docker 部署Redis
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. 1 去官网下载指定的版本 https:/ ...
- Ubuntu下,terminal经常使用快捷键
# ctrl + l - 清屏 . cLear # ctrl + c - 终止命令. # ctrl + d - 退出 shell,好像也能够表示EOF. # ctrl + r - 从命令历史中找 . ...
- 如何获取不同网站的favicon默认图标
参考文章: 1.利用公共api提取任意网站favicon.ico图标 如何读取favicon 根据设置favicon的方式,就有2种读取favicon的方法: A.默认直接读取网站根目录的favi ...
- android中执行(定时任务)的方法及6位随机码的产生
在网上看了很多类似的文章,比较乱,自己总结了一下,在开发中,常见的执行定时任务的方法有以下几种, 很简单的描述,有什么不懂可以留言,下面来介绍一下这几种常见的方法: 1.直接在线程中睡觉的方法,这个比 ...
- QQ登录整合/oauth2.0认证-01-申请appkey和appid
本节需要你申请appkey和appid还有绑定域名的空间 首先 再讲课之前 你需要准备以下东西 到腾讯开发平台中申请 开发者 获得appid 和appkey 这两个东东 这两个东东 就算没审核 也可以 ...
- Linux-LVS为何不能完全替代DNS轮询
转自:链接 上一篇文章“一分钟了解负载均衡的一切”引起了不少同学的关注,评论中大家争论的比较多的一个技术点是接入层负载均衡技术,部分同学持这样的观点: 1)nginx前端加入lvs和keepalive ...
- Asynchronous and non-Blocking I/O 翻译[收藏好文]
http://www.tornadoweb.org/en/stable/guide/async.html Real-time web features require a long-lived mos ...
- #探究# HTTP长连接和短连接
本文速读: HTTP协议与TCP/IP协议的关系 因TCP协议可靠,所以HTTP通常基于TCP实现 如何理解HTTP协议是无状态的 多次请求之间没有关联关系 什么是长连接.短连接? 每次请求都建立TC ...
- c语言之函数参数传递之数组篇(转)
在VC中写程序都习惯了,一般数组作为函数实参时,最常用的方法就是引用和指针的方法,但是到C语言中就没有引用了,还有一种比较常用的方法: #include <stdio.h>void sor ...