leetcode刷题,总结, 记录,备忘326
来源:互联网 发布:c语言 面向过程 编辑:程序博客网 时间:2024/05/22 03:39
leetcode326Power 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?
class Solution {public: bool isPowerOfThree(int n) { if (n <= 0) { return false; } if (n == 1) { return true; } return n % 3 == 0 && isPowerOfThree(n / 3); }};题目中有比较高的要求不使用递归或者循环,那该怎么办呢,看了leetcode的几个高投票的答案。
class Solution {public: bool isPowerOfThree(int n) { double t = log10(n) / log10(3); if (t == (int)t) { return true; } else { return false; } }};使用对数计算的方式,我特地去会议了下中学时学习的对数计算公式,在此做个例子就很好理解了。比如log10(3)和log10(9),log10(9) = log10(3x3) = log10(3) + log10(3);所以如果n是3的指数次幂的话,log10(n)的值一定是log10(3)的整数倍,很好理解了吧,科科。
0 0
- leetcode刷题,总结, 记录,备忘326
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- Sumsets(递推)
- 使用Eclipse一点心得
- CollectionView实现瀑布流(swift)
- GNU assembler not found, install/update gas-preprocessor
- 20160114html学习笔记表单框架颜色脚本字符实体
- leetcode刷题,总结, 记录,备忘326
- 内存泄漏
- jenkins资料
- 解析如何读取文件形成InputSplit
- Erlang语言学习系列(四)
- ABAP屏幕基础
- 程序员应该常去逛逛的地方(技术社区)
- 微软Madoko:在线的类似CSDN markdown但是功能更强的编辑器
- 编写高质量iOS代码的方法总结