LeetCode 326. Power of Three
来源:互联网 发布:淘宝海外入驻入口 编辑:程序博客网 时间:2024/06/16 05:29
326. Power of Three
Description
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?
Solution
- 题意即判断一个数是不是3的次方。
- 和之前的2、4次方不同,不能使用位运算来免除循环,但是我们发现,如果一个数是3的次方,那么在范围内最大的3的次方的数被这个数除,余数一定为0,而倘若不是3的次方,则余数不会为0,这个最大的数就是1162261467,在int范围内,可以通过写一个小循环快速找到。代码如下:
// C++/Cclass Solution {public: bool isPowerOfThree(int n) { return n > 0 && 1162261467 % n == 0; }};
# pythonclass Solution(object): def isPowerOfThree(self, n): return n > 0 and 1162261467 % n == 0;
阅读全文
0 0
- [leetcode]326. Power of Three
- Leetcode 326. Power of Three
- Leetcode 326. Power of Three
- [leetcode] - 326. Power of Three
- 326. Power of Three LeetCode
- LeetCode ||326. Power of Three
- [LeetCode]326. Power of Three
- [leetcode] 326. Power of Three
- LeetCode *** 326. Power of Three
- leetcode 326. Power of Three
- 326.[LeetCode]Power Of Three
- LeetCode 326. Power of Three
- #leetcode#326. Power of Three
- Leetcode 326. Power of Three
- Leetcode 326. Power of Three
- leetcode 326. Power of Three
- LeetCode 326. Power of Three
- [leetcode] 326. Power of Three
- 计算机网络的结构
- make makefile cmake qmake
- ZOJ1516_Uncle Tom's Inherited Land_二分图(匈牙利算法)
- 元组UPLE:戴上了枷锁的列表--2017/9/3--1
- 排序之插入排序
- LeetCode 326. Power of Three
- c面试考点
- kuangbin 数论 A题
- Windows bat脚本获取administrator权限
- struts2的核心和工作原理
- Unity入门学习 //05_Inspector窗口
- front()的作用
- 002-支持并发的内部类饿汉单例
- Java Collections.sort和Arrays.sort使用comparator或者comparable接口的compare函数返回值