题目:

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?

思路:

这个问题是求一个数是不是3的次方数,可以有两种解法:

1.采用余数的算法

2.利用了java 的对数运算,判断3的对象是否实在整数,其中用到了换底公式

代码1

public class Solution {
    public boolean isPowerOfThree(int n) {
        if(n <= 0){
            return false;
        }else{
            while(n % 3 == 0){
                n = n/3;
            }
            return n == 1;
        }
    }
}

LeetCode之旅(20)-Power of Three的更多相关文章

  1. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  2. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  3. LeetCode 第 231 题 (Power of Two)

    LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of ...

  4. LeetCode之旅(19)-Power of Two

    题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thanks ...

  5. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

  6. &lt;LeetCode OJ&gt; 326. Power of Three

    326. Power of Three Question Total Accepted: 1159 Total Submissions: 3275 Difficulty: Easy 推断给定整数是否是 ...

  7. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  8. [算法学习]开始leetcode之旅

    在此记录一下用javascript刷leetcode的过程,每天都要坚持! 1.Two Sum Given an array of integers, find two numbers such th ...

  9. 【LeetCode】869. Reordered Power of 2 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计每位数字出现的次数 日期 题目地址:http ...

随机推荐

  1. Dynamics CRM2015 The plug-in type does not exist in the specified assembly问题的解决方法

    在用插件工具PluginProfiler调试时,报"The plug-in type  xxxx  does not exist in the specified assembly" ...

  2. 【Android应用开发】Android Studio - MAC 版 - 快捷键详解

    博客地址 : http://blog.csdn.net/shulianghan/article/details/47321177 作者 : 韩曙亮 要点总结 : -- 熟练使用快捷键 : 在任何编程环 ...

  3. Deploying Customizations in Oracle E-Business Suite Release 12.2

    DeployingCustomizations in Oracle E-Business Suite Release 12.2 This documentdescribes how to deploy ...

  4. [C++学习历程]Visual Studio 2010 的HelloWorld

    大学时期曾学过C++的知识,那时候也没有使用VS这样高档的IDE工具,就是C++6.0那样来的.对于重新拾起C++来说,换了个IDE,那么就先从使用IDE学起吧~ 作者:苏生米沿 本文链接:http: ...

  5. 管道模式——pipeline与valve

    在一个比较复杂的大型系统中,假如存在某个对象或数据流需要被进行繁杂的逻辑处理的话,我们可以选择在一个大的组件中进行这些繁杂的逻辑处理,这种方式确实达到了目的,但却是简单粗暴的.或许在某些情况这种简单粗 ...

  6. UITableViewBase&nbsp;UI_09

    1.UITableView API文档总结:      1.UITableView的父类时,UIScrollView,所以它是可以滚动的,但是只能在竖直方向滚动. 2.UITableView是iOS中 ...

  7. Eclipse 创建 Maven 项目、Maven JavaWeb 项目

    Eclipse 创建 Maven 项目         新建一个maven项目          (默认)(如果不行第一个Create a simple ... 也选中) 默认         Jav ...

  8. OC语言大总结(上)

    根据OC学习的知识点,总结了一下,希望能帮到大家! 作者:韩俊强  未经允许,请勿转载! 关注博主:http://weibo.com/hanjunqiang 第一节类于对象   类与对象http:// ...

  9. 4.2、Libgdx各个模块概览

    (原文:http://www.libgdx.cn/topic/34/4-2-libgdx%E5%90%84%E4%B8%AA%E6%A8%A1%E5%9D%97%E6%A6%82%E8%A7%88) ...

  10. 使用Libgdx开发的FlappyBird(像素鸟、疯狂的小鸟)游戏源码

    本帖最后由 宋志辉 于 2014-10-21 15:06 编辑 点击进入下载地址 Flappy Bird(飞扬的小鸟)由一位来自越南河内的独立游戏开发者阮哈东开发,是一款形式简易但难度极高的休闲游戏. ...