题目要求:

给定一个整数,写一个函数来判断它是否是 3 的幂次方。

示例:

输入: 27

输出: true

代码:

class Solution {
public:
bool isPowerOfThree(int n) {
double tmp = n / 1.0;
while(tmp >= 3.0) {
tmp /= 3.0;
} if(tmp == 1.0) {
return true;
}
return false;
}
};

LeetCode. 3的幂的更多相关文章

  1. LeetCode | 2 的幂

    LeetCode 题库的第 231 题 —— 2 的幂 这题也是比较容易的一题,前提是找到规律即可.如果从 10 进制的角度观察 2 的幂次方,可能并不容易发现规律,那么可以从 2 进制的角度进行观察 ...

  2. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  3. LeetCode 231.2的幂

    LeetCode 231.2的幂 题目: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 算法: 若一个数是2的幂次的话定会有n & (n - 1) == 0这个关系成立 所以直接用 ...

  4. LeetCode Power of Two (2的幂)

    题意:判断1个数n是否刚好是2的幂,幂大于0. 思路:注意会给负数,奇数.对于每个数判断31次即可. class Solution { public: bool isPowerOfTwo(int n) ...

  5. leetcode刷题笔记231 2的幂

    题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为 ...

  6. leetcode刷题笔记342 4的幂

    题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不使 ...

  7. leetcode刷题笔记326 3的幂

    题目描述: 给出一个整数,写一个函数来确定这个数是不是3的一个幂. 后续挑战:你能不使用循环或者递归完成本题吗? 题目分析: 既然不使用循环或者递归,那我可要抖机灵了 如果某个数n为3的幂 ,则k=l ...

  8. LeetCode 50 - Pow(x, n) - [快速幂]

    实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...

  9. 不使用循环或递归判断一个数是否为3的幂(leetcode 326)

    326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...

随机推荐

  1. IntelliJ IDEA实时代码模板

    首先,通过 Settings > Editor - Live Templates进入设置窗口 在Abbreviation:中设置缩写,在 Template text:中设置模板内容,通过 Cha ...

  2. postgresql 创建索引:ERROR: operator class "gin_trgm_ops" does not exist for access method "gin"

    g_trgm is an extension, so: CREATE EXTENSION pg_trgm; If you get the following error ERROR: could no ...

  3. Windows下OpenFOAM开发及使用环境配置指南 (1)【转载】

    转载自:http://openfoam.blog.sohu.com/158614863.html *************************************************** ...

  4. JAVA基础知识|synchronized和lock

    一.synchronized 是jvm的一个关键字,使用过程均由jvm控制 有三种使用方式: 修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁 修饰代码块,同方法 修饰静态方法,作用 ...

  5. gacutil.exe的位置

    如果我们需要用gacutil去注册dll ,就需要使用Visual Studio的Command Prompt,前提是需要安装Visual Studio,但是客户端上一般是没有安装VS的,所以你就需要 ...

  6. django post 403

    同一个地址GET方式可以正常访问 在POST 提交数据过程中报403错误, 原来是1.3版本settings.py 文件中 'django.middleware.csrf.CsrfViewMiddle ...

  7. Cesium中的坐标系及转换

    在我们开始学习Entity之前,我们首先需要先学习下Cesium中的坐标系,Cesium中有多个坐标系,在进行添加Entity时经常会使用到. 一.坐标系介绍 我们先来列举下Cesium中的坐标系:W ...

  8. PHP try catch 如何使用

      <?php   try { if (file_exists('test_try_catch.php')) { require ('test_try_catch.php'); } else { ...

  9. vue-resource在vuecli3中请求headers修改

    this.$resource.delete({ user_code: Cookie.get("empid"), date: date, file_name: file_name } ...

  10. 【集成模型】Boosting

    0 - 思想 Bagging算法思想是减少预测方差(variance),Boosting算法思想是为了减少预测偏差(bias). Boosting算法思想是将“弱学习算法”提升为“强学习算法”.一般来 ...