326 Power of Three 3的幂
给出一个整数,写一个函数来确定这个数是不是3的一个幂。
后续挑战:
你能不使用循环或者递归完成本题吗?
详见:https://leetcode.com/problems/power-of-three/description/
C++:
方法一:
class Solution {
public:
bool isPowerOfThree(int n) {
while(n&&n%3==0)
{
n/=3;
}
return n==1;
}
};
方法二:
class Solution {
public:
bool isPowerOfThree(int n) {
return (n>0&&1162261467%n==0);
}
};
参考:https://www.cnblogs.com/grandyang/p/5138212.html
326 Power of Three 3的幂的更多相关文章
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- 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 ...
- <LeetCode OJ> 326. Power of Three
326. Power of Three Question Total Accepted: 1159 Total Submissions: 3275 Difficulty: Easy 推断给定整数是否是 ...
- LeetCode 326 Power of Three(3的幂)(递归、Log函数)
翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...
- 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 ...
- 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 ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
- 【LeetCode】326. Power of Three 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 递归 取对数 判断是不是最大3的倍数的因子 日 ...
随机推荐
- PAT 1079. 延迟的回文数
PAT 1079. 延迟的回文数 给定一个 k+1 位的正整数 N,写成 ak...a1a0 的形式,其中对所有 i 有 0 <= ai < 10 且 ak > 0.N 被称为一个回 ...
- Java Web学习总结(29)——Java Web中的Filter和Interceptor比较
1. 背景 在设计web应用的时候,用户登录/注册是必不可少的功能,对用户登录信息进行验证的方法也是多种多样,大致可以认为如下模式:前端验证+后台验证.根据笔者的经验,一般会在前端进行一些例如是否输入 ...
- 九度oj 题目1053:互换最大最小数
题目1053:互换最大最小数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7538 解决:3049 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后 ...
- spring boot & JsonParser
spring boot https://stackoverflow.com/questions/29313687/trying-to-use-spring-boot-rest-to-read-json ...
- jQuery下拉列表操作(转)
转地址:http://www.cnblogs.com/yaoshiyou/archive/2010/08/24/1806939.html jQuery获取Select选择的Text和Value:语法解 ...
- HBase单节点的安装与配置
HBase的安装配置1.下载:http://mirror.bit.edu.cn/apache/hbase/stable/ hbase-1.2.6-bin是直接编译好的,直接安装. hbase- ...
- jsp内置对象(转)
JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...
- php处理管道文件流
<?php #!/usr/local/bin/php -q function read(){ $fp = fopen("php://stdin", "r" ...
- kendo中需要kendo.default.min.css
kendo中需要kendo.default.min.css,这个默认是没有加入头文件的,还是需要手动加入一下 <link href="~/Scripts/kendo/styles/ke ...
- springboot整合dubbo的简单案例
使用框架: jdk 1.8 springboot-2.1.3 dubbo-2.6 spring-data-jpa-2.1.5 一.开发dubbo服务接口: 按照Dubbo官方开发建议,创建一个接口项目 ...