326. 3的幂

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

示例 1:

输入: 27

输出: true

示例 2:

输入: 0

输出: false

示例 3:

输入: 9

输出: true

示例 4:

输入: 45

输出: false

进阶:

你能不使用循环或者递归来完成本题吗?

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

Java实现 LeetCode 326 3的幂的更多相关文章

  1. Leetcode 326.3的幂 By Python

    给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输 ...

  2. Java实现 LeetCode 342 4的幂

    342. 4的幂 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶: 你 ...

  3. Java实现 LeetCode 230 2的幂

    231. 2的幂 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = ...

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

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

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. db连接池

    目前常用的连接池有: DBCP:org.apache.commons.dbcp.BasicDataSource dataSource: 要连接的 datasource (通常我们不会定义在 serve ...

  2. failed parsing overlays.

    clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:

  3. Git上传文件、文件夹到github

    上传一个文件: $ git add test.txt 上传多个文件: $ git add test.txt demo.txt 同理: 上传一个文件夹: $ git add learngit 上传多个文 ...

  4. [hdu5358]分类统计,利用单调性优化

    题意:直接来链接吧http://acm.hdu.edu.cn/showproblem.php?pid=5358 思路:注意S(i,j)具有区间连续性且单调,而⌊log2x⌋具有区间不变性,于是考虑枚举 ...

  5. Mybatis学习笔记汇总(包括源码和jar包)

    博客整理 Mybatis学习笔记(一)--对原生jdbc中问题的总结 Mybatis学习笔记(二)--Mybatis框架 Mybatis学习笔记(三)--入门程序 MyBatis学习笔记(四)--入门 ...

  6. CentOS7.2 安装 MongoDB 3.4

    服务器版本 CentOS7.2 MongoDB版本 3.4 1/ 下载所需资源 阿里镜像地址 http://mirrors.aliyun.com/mongodb/yum/redhat/7/mongod ...

  7. Python基础语法day_02——字符串规则

    day_02 使用方法修改字符串的大小写 将字符串首字母变成大写 >>> name = "ada lovelace" >>> print(nam ...

  8. 黑马程序员_毕向东_Java基础视频教程——算术运算符小点(随笔)

    算术运算符小点 ​ 取模 class Test{ public static void main(String[] args){ System.out.println( 1 % -5); System ...

  9. ubuntu下图形程序自启动的几种方法

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ @ 目录 0.前言 1.带桌面环境的自动启动 1.1 ...

  10. git推送代码问题之:ERROR: [abcdefg] missing Change-Id in commit message footer

    一.问题: 在日常的工作中,使用git推送代码时会出现以下报错,“missing Change-Id in commit message” : qinjiaxi:$ git push origin H ...