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 do it without using any loop / recursion?
解题思路:
对给定的数求以3为底的对数,然后再将结果用于3的次幂,看是否与原来的数相同。
代码如下:
public class Solution {
public boolean isPowerOfThree(int n) {
return n <= 0 ? false : n == Math.pow(3, Math.round(Math.log(n) / Math.log(3)));
}
}
Java [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 231]Power of Two
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...
- 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 ...
随机推荐
- 数字式PID控制的应用总结
PID控制是一个二阶线性闭环控制器,通过调整比例.积分和微分三项参数,使得大多数的工业控制系统获得良好的闭环控制性能.PID控制优点:a. 技术成熟,b. 易被人们熟悉和掌握,c. 不需要建立数学模型 ...
- 【POJ】【2068】Nim
博弈论/DP 这是Nim?这不是巴什博奕的变形吗…… 我也不会捉啊,不过一看最多只有20个人,每人最多拿16个石子,总共只有8196-1个石子,范围好像挺小的,嗯目测暴力可做. so,记忆化搜索直接水 ...
- 【POJ】【2348】Euclid‘s Game
博弈论 题解:http://blog.sina.com.cn/s/blog_7cb4384d0100qs7f.html 感觉本题关键是要想到[当a-b>b时先手必胜],后面的就只跟奇偶性有关了 ...
- Ubuntu的LTS版本
Ubuntu的LTS版本什么意思 LTS是长期支持(Long Term Support)的缩写. 我们每六个月制作一个新的Ubuntu桌面和服务器的版本,这意味着你总能拥有开源世界提供的最新最好的应用 ...
- 解Linux进程间通信(IPC)方式
http://blog.csdn.net/liuhongxiangm/article/details/7928790 linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对U ...
- IMP不到指定的表空间
==============================================================================只导dmp文件中的几个表数据,解决导入时ta ...
- linux服务器重启服务命令说明文档
(前提是电脑上面已经安装好了ssh软件~!)输入ip,用户名,端口(默认22) 输入密码,登陆成功之后, 转入到/usr/local/tomcat/bin 目录,输入命令行: [root@yangch ...
- codeforces 295E Yaroslav and Points (离线操作+离散化+区间合并)
参考链接:http://blog.csdn.net/dyx404514/article/details/8817717 写的很详细,这里就不再赘述,附上我的代码. #include <iostr ...
- JsRender系列demo(6)-无名
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery.j ...
- [转]Ubuntu 12.04 安装屏保
From:http://www.howtogeek.com/114027/how-to-add-screensavers-to-ubuntu-12.04/ How to Add Screensaver ...