LeetCode_326. Power of Three
326. Power of Three
Given an integer, write a function to determine if it is a power of three.
Example 1:
Input: 27
Output: true
Example 2:
Input: 0
Output: false
Example 3:
Input: 9
Output: true
Example 4:
Input: 45
Output: false
Follow up:
Could you do it without using any loop / recursion?
package leetcode.easy;
public class PowerOfThree {
public static void main(String[] args) {
PowerOfThree sol = new PowerOfThree();
int iterations = 1000000; // See table header for this value
long startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree1(i);
}
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree2(i);
}
endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree3(i);
}
endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
startTime = System.currentTimeMillis(); // 获取开始时间
for (int i = 0; i < iterations; i++) {
sol.isPowerOfThree4(i);
}
endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
}
public boolean isPowerOfThree1(int n) {
if (n < 1) {
return false;
}
while (n % 3 == 0) {
n /= 3;
}
return n == 1;
}
public boolean isPowerOfThree2(int n) {
return Integer.toString(n, 3).matches("^10*$");
}
public boolean isPowerOfThree3(int n) {
return (Math.log10(n) / Math.log10(3)) % 1 == 0;
}
public boolean isPowerOfThree4(int n) {
return n > 0 && 1162261467 % n == 0;
}
@org.junit.Test
public void test() {
System.out.println(isPowerOfThree1(27));
System.out.println(isPowerOfThree1(0));
System.out.println(isPowerOfThree1(9));
System.out.println(isPowerOfThree1(45));
System.out.println(isPowerOfThree2(27));
System.out.println(isPowerOfThree2(0));
System.out.println(isPowerOfThree2(9));
System.out.println(isPowerOfThree2(45));
System.out.println(isPowerOfThree3(27));
System.out.println(isPowerOfThree3(0));
System.out.println(isPowerOfThree3(9));
System.out.println(isPowerOfThree3(45));
System.out.println(isPowerOfThree4(27));
System.out.println(isPowerOfThree4(0));
System.out.println(isPowerOfThree4(9));
System.out.println(isPowerOfThree4(45));
}
}
LeetCode_326. Power of Three的更多相关文章
- 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表
1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...
- 一起学微软Power BI系列-使用技巧(4)Power BI中国版企业环境搭建和帐号问题
千呼万唤的Power BI中国版终于落地了,相信12月初的微软技术大会之后已经铺天盖地的新闻出现了,不错,Power BI中国版真的来了,但还有些遗憾,国际版的一些重量级服务如power bi emb ...
- 一起学微软Power BI系列-使用技巧(3)Power BI安卓手机版安装与体验
Power BI有手机版,目前支持安卓,苹果和WP,不过没有WP手机,苹果在国内还不能用,要FQ和用就不测试了.安卓的我也也是费了九牛二虎之力才把app下载下来,把方法分享给大家. FQ太麻烦,所以建 ...
- Power BI官方视频(3) Power BI Desktop 8月份更新功能概述
Power BI Desktop 8月24日发布了更新版本.现将更新内容翻译整理如下,可以根据后面提供的链接下载最新版本使用. 1.主要功能更新 1.1 数据钻取支持在线版 以前的desktop中进行 ...
- 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库
说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...
- 千呼万唤始出来,微软Power BI简体中文版官网终于上线了,中文文档也全了。。
前几个月时间,研究微软Power BI技术,由于没有任何文档和资料,只能在英文官网瞎折腾,同时也发布了英文文档的相关文章:系列文章,刚好上周把文章发布完,结果简体中文版上线了.哈哈,心里有苦啊,早知道 ...
- 微软新神器-Power BI横空出世,一个简单易用,还用得起的BI产品,你还在等什么???
在当前互联网,由于大数据研究热潮,以及数据挖掘,机器学习等技术的改进,各种数据可视化图表层出不穷,如何让大数据生动呈现,也成了一个具有挑战性的可能,随之也出现了大量的商业化软件.今天就给大家介绍一款逆 ...
- 免费的精品: Productivity Power Tools 动画演示
Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率.它的出现一定程度上弥补和完善了 Visual Studio 自身的不足, ...
- 微软Power BI技术文章与资源目录
下面是本博客原创的微软Power BI技术相关文章,对于部分转载文章和资源,会注明出处. 本博客将发布基于微软Power BI相关的基础入门文章,视频教程等资源,敬请关注. 个人建立的Power BI ...
随机推荐
- 大小端示例-arm c51
大小端系列文章https://blog.csdn.net/liming0931/article/details/100016425 MDK(Keil5,STM32F407)C语言: #include ...
- hello world 程序的生成过程
一个c / c ++文件需要经过预先(预处理),编译(编译),编译(汇编)和链接(链接)等四步,才能生成可执行程序. 在日常编译中,通常“编译”统称这四步: gcc -c xxx .s:汇编 gcc ...
- qt加快编译速度
菜单栏->工具-> 选项->构建与运行->构建套件->点击自动检测内容->在同一页面找到 Environment ->点击change ->在弹出的窗口 ...
- leetcode解题报告(31):Kth Largest Element in an Array
描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
- 《挑战30天C++入门极限》C++中类的多态与虚函数的使用
C++中类的多态与虚函数的使用 类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持 ...
- shell history 命令
1.history命令可以显示历史执行过的命令: 2.使用!+序号执行该序号对应的命令: 例子 $ history sed 's/haha/hello/g' test cat test cat tes ...
- git图形化统计工具 - windows下gitstats的安装和使用
gitstats 是一款git历史统计工具,可以生成定量的统计数据,并以html图表的形式展示.统计文件包括文件数量.代码量.提交量.作者信息.每天活跃度.每周活跃度.每月活跃度以及提交数排名等等,信 ...
- 开放-封闭原则(OCP)
怎样的升级才能面对需求的改变却可以保持相对稳定,从而使得系统可以在第一个版本以后不断推出新的版本呢?开放-封闭原则(The Open-Closed Principle, OCP)为我们提供了指引.软件 ...
- c# lock TransactionScope
c# lock TransactionScope TransactionOptions option = new TransactionOptions(); //option.IsolationLev ...
- linux系统下以存储从大到小并以K,M,G为单位的方式查看当前目录下的文件信息
zhuazai:https://blog.csdn.net/sty945/article/details/79830915 前言 ls命令 ls -a ls -l ll du命令 du -s du - ...