leetcode326题 题解 翻译 C语言版 Python版
来源:互联网 发布:怎么查找网络打印机 编辑:程序博客网 时间:2024/06/06 15:37
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?
326.3的幂
给定一个整数,写一个函数来判断它是不是3的幂
进一步的:
你能不用任何循环和递归解决它吗?
思路:判断一个数是不是3的幂只需要不断地除以3即可,只要出现除不通的情况就判断为不是3的幂,如果能一直除到商为1,则它是3的幂。
bool isPowerOfThree(int n) { if (n < 1) return false; if (n == 1) return true; while (n > 1){ if (n%3) return false; n /= 3; } return true;}
class Solution(object): def isPowerOfThree(self, n): """ :type n: int :rtype: bool """ if n < 1: return False if n == 1: return True while n > 1: if n % 3: return False n /= 3 return True
0 0
- leetcode326题 题解 翻译 C语言版 Python版
- leetcode292题 题解 翻译 C语言版 Python版
- leetcode258题 题解 翻译 C语言版 Python版
- leetcode104题 题解 翻译 C语言版 Python版
- leetcode237题 题解 翻译 C语言版 Python版
- leetcode226题 题解 翻译 C语言版 Python版
- leetcode283题 题解 翻译 C语言版 Python版
- leetcode100题 题解 翻译 C语言版 Python版
- leetcode242题 题解 翻译 C语言版 Python版
- leetcode171题 题解 翻译 C语言版 Python版
- leetcode235题 题解 翻译 C语言版 Python版
- leetcode206题 题解 翻译 C语言版 Python版
- leetcode328题 题解 翻译 C语言版 Python版
- leetcode191题 题解 翻译 C语言版 Python版
- leetcode70题 题解 翻译 C语言版 Python版
- leetcode83题 题解 翻译 C语言版 Python版
- leetcode263题 题解 翻译 C语言版 Python版
- leetcode231题 题解 翻译 C语言版 Python版
- HDU-1016-Prime Ring Problem( C && 经典DFS题 )
- 深拷贝与浅拷贝
- 个人关于对KVC和KVO
- when the android meet dagger2
- 1965: [Ahoi2005]SHUFFLE 洗牌 快速幂+扩展欧几里得算法
- leetcode326题 题解 翻译 C语言版 Python版
- 【PA2014】【BZOJ3711】Druzyny
- ELF文件学习
- C++类型转换
- MYSQL优化
- x265-1.8版本-encoder/bitcost.cpp注释
- x265-1.8版本-encoder/bitcost.h注释
- TI CC2540 BLE
- win10 无法访问移动硬盘 提示“参数错误”