一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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的n次方。

解题思路:很容易就想到将该数每次都除以3,如果整除则继续除,直到等于1就表示是3的n次方,否则就不是。

class Solution {
public:
    bool isPowerOfThree(int n) {
        int num=n;
        while(num>0 && num%3==0)  //整除
            num/=3;
        return num==1;
    }
};

但是题目中提到,能不能不用循环或者递归来解题。可是想了半天都没有想到,无奈只能求助百度了。

在int范围内的3的n次方最大为3的19次方1162261467,如果num时3的n次方数的话,一定能被1162261467整除。

于是有下面的代码:

class Solution {
public:
    bool isPowerOfThree(int n) {
        return n>0?(1162261467%n==0?true:false):false;
    }
};

还有的方法就是列举法,也就19个数,判断跟这19个数中任意一个相不相等即可。

【一天一道LeetCode】#326. Power of Three的更多相关文章

  1. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  2. 39. leetcode 326. Power of Three

    326. Power of Three Given an integer, write a function to determine if it is a power of three. Follo ...

  3. [LeetCode] 326. Power of Three 3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  4. LeetCode 326 Power of Three

    Problem: Given an integer, write a function to determine if it is a power of three. Could you do it ...

  5. Java [Leetcode 326]Power of Three

    题目描述: Given an integer, write a function to determine if it is a power of three. Follow up:Could you ...

  6. LeetCode 326 Power of Three(3的幂)(递归、Log函数)

    翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...

  7. leetcode 326 Power of Three (python)

    原题: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  8. Leetcode 326 Power of Three 数论

    判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) =  1162261467)的约数 这种方法是我 ...

  9. [LeetCode] 326. Power of Three + 342. Power of Four

    这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...

  10. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

随机推荐

  1. npm run dev 出错的解决办法

    bogon:~ yan$ cd my-project bogon:my-project yan$ npm run dev > my-project@1.0.0 dev /Users/yan/my ...

  2. 【Git】Git工具常用命令

    GitHub使用指南 一.把本地代码上传到GitHub 0. 提前配置好上传地址 [git config --global user.name "username"] [git c ...

  3. 谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?

    在这篇客座文章中,可口可乐公司的 Patrick Brandt 将向我们介绍他们如何使用 AI 和 TensorFlow 实现无缝式购买凭证. 可口可乐的核心忠诚度计划于 2006 年以 MyCoke ...

  4. Linux 定时执行任务:Crontab服务及应用集锦

    系统背景:cenos6.5 操作工具:Xshell5 情况一:正常情况(系统有service命令) 重启服务命令:[root@centos6 /]# service crond restart 启动服 ...

  5. ASP.NET Core部署到Windows IIS

    网上已经有许多ASP.NET Core关于Widows IIS部署的文章,在部署到服务器时遇到了一些问题,在这里我就不再对原理进行阐释(复制)了,只写下一些关键环节,想看原理的同学请参考官网,此文章作 ...

  6. ZOJ-2965

    Accurately Say "CocaCola"! Time Limit: 2 Seconds      Memory Limit: 65536 KB In a party he ...

  7. JQuery 分页显示jquery-pager-1.0.js

    原版是jquery-pager-1.0.js,经过变更修改加上按照项目中的一些需要修改过来. //初始化分页控件 PagerOptions为配置参数 url为要提交的url地址,如果不需要提交则可以为 ...

  8. Jupyter Notebook 快速入门

    Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...

  9. swing JTable

    JTable 实例 import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayo ...

  10. Latex:TexStudio的使用

    http://blog.csdn.net/pipisorry/article/details/54565608 Texsdudio 快捷键 The keyboard shortcuts can be ...