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 ...
随机推荐
- Dubbo框架探讨(转)
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- Linux chattr 命令
不让用户修改.删除文件等,使用 chattr保护 chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files… 最关键的是在[mode]部分,[m ...
- rpm基本命令参考
水可载舟,亦可覆舟! 1. 介绍 在Linux操作系统中,常用的软件有RPM包软件包,src.rpm格式的软件包(源码),以源代码发布的软件包以及.bin格式软件包和绿色软件.如果采用.rpm格式的软 ...
- YAML格式的语法
基本格式 用空格缩进, 不能用tab 用#标记注释 列表: 用短划(-)标记元素 映射: 用冒号(:)分隔key, value. 如果写在一行, 需要用逗号分隔并前后加花括号 字符串: 不加引号, 加 ...
- Linux 中的网络数据包捕获
Linux 中的网络数据包捕获 Ashish Chaurasia, 工程师 简介: 本教程介绍了捕获和操纵数据包的不同机制.安全应用程序,如 VPN.防火墙和嗅探器,以及网络应用程序,如路由程序,都依 ...
- 进程间通过intent传递数据失败
<activity android:name=".activity.CreateMessageActivity" android:pr ...
- Map的深浅拷贝的探究
1. 复制map示例 首先看一个例子,当我使用不同方法将一个源map拷贝到另一个map后,改变源map,复制后的map理应不受影响 import java.math.BigDecimal; impor ...
- shell 脚本启动tomcat服务
#!/bin/bash # kill tomcat进程 tomcat_fashion_dev_pid=`ps aux|grep tomcat_fashion_dev|grep -v "gre ...
- servlet乱码 解决方法 2种方法
public class ResponseDemo1 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServl ...
- 【LeetCode】Binary Tree Upside Down
Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...