71-Power of Three
2016-05-17 12:29
288 查看
Power of Three My Submissions QuestionEditorial Solution
Total Accepted: 41963 Total Submissions: 113431 Difficulty: Easy
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?
先给出有循环的答案,因为事先用map或set存储后面比较的方法并没有更快
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 136 ms
beats65.89%
用map实现反而更慢。。
Submission Details
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 416 ms
beats:0.79%
Power of Three
Submission Details
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 392 ms
事实上仅仅比map快一点点
Total Accepted: 41963 Total Submissions: 113431 Difficulty: Easy
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?
先给出有循环的答案,因为事先用map或set存储后面比较的方法并没有更快
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 136 ms
beats65.89%
class Solution { public: bool isPowerOfThree(int n) { if(n==0)return false; while(n!=1){ if((n%3)!=0)return false; n=n/3; } return true; } };
用map实现反而更慢。。
Submission Details
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 416 ms
beats:0.79%
class Solution { public: bool isPowerOfThree(int n) { map<int,int> map3; map3[1]=1; map3[3]=1; map3[9]=1; map3[27]=1; map3[81]=1; map3[243]=1; map3[729]=1; map3[2187]=1; map3[6561]=1; map3[19683]=1; map3[59049]=1; map3[177147]=1; map3[531441]=1; map3[1594323]=1; map3[4782969]=1; map3[14348907]=1; map3[43046721]=1; map3[129140163]=1; map3[387420489]=1; map3[1162261467]=1; if(map3.count(n))return true; else return false; } };
Power of Three
Submission Details
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 392 ms
事实上仅仅比map快一点点
#define IMAX numeric_limits<int>::max() class Solution { public: bool isPowerOfThree(int n) { set<int> s; s.insert(1); s.insert(3); s.insert(9); s.insert(27); s.insert(81); s.insert(243); s.insert(729); s.insert(2187); s.insert(6561); s.insert(19683); s.insert(59049); s.insert(177147); s.insert(531441); s.insert(1594323); s.insert(4782969); s.insert(14348907); s.insert(43046721); s.insert(129140163); s.insert(387420489); s.insert(1162261467); if(s.count(n))return true; else return false; } };
相关文章推荐
- 2016 UESTC Training for Dynamic Programming Q - 柱爷的宝藏 斜率优化
- 关于js出现的error :<!DOCTYPE html> Uncaught SyntaxError: Unexpected token
- 量子力学里为什么要有复数?
- 第11周补充项目1-是春哥吗
- sun.misc.BASE64Encoder找不到jar包的解决方法(eclipse中红叉)
- Mysql的简单使用(二)
- 二分查找算法
- poj1651矩阵连乘(DP)+路径
- Visual Studio Code中文文档(一)-快速入门
- 最小费用最大流-POJ-2195-Going Home
- 使用 NGINX 进行微程序缓存的好处
- 证券
- 基于ListView的滑动删除、添加、修改
- 文件存储与B+数据结构
- 【MATLAB 学习笔记】 SimMechanics 流程攻略(1)
- 一个牧场目前一共有20头刚出生的羊,母羊、公羊各一半。假如母羊5岁时后每年生一胎(母羊,公羊各一半)。羊活到10岁后死亡。请问20年后这个牧场有多少只羊? 请用C#写出算法。
- 2016 UESTC Training for Dynamic Programming P - 柱爷的矩阵 矩阵、递推
- php面试总结
- 剑指offer03--空格用%20替换
- Python多线程编程