当前位置:网站首页>leetcode 326. Powers of 3
leetcode 326. Powers of 3
2022-08-03 20:12:00 【Luna who can program】
Given an integer, write a function to determine if it is a power of 3.Returns true if so; otherwise, returns false .
An integer n is a power of 3 if there is an integer x such that n == 3x
Example 1:
Input: n = 27
Output: true
Example 2:
Input: n = 0
Output: false
Example 3:
Input: n = 9
Output: true
Example 4:
Input: n = 45
Output: false
Tip:
-231<= n <= 231 - 1
Thinking:
The power of 3 must be greater than 0, and the remainder after dividing the power of 3 by 3 must be 0 (except 1). If the most basic factor of 3 is not divisible, it is definitely not the power of 3number.
class Solution {public:bool isPowerOfThree(int n) {while(n>0 && n%3==0)n/=3;return n==1;}};
The incoming parameter type is int. Some people may say that 1.0/9 is 3 to the power of -2, which is also a power of 3, but if you enter 1.0/9, the function will force the type to be converted to int.In fact, the operation into the function is 0,.
This function only determines whether a number greater than 0 is a power of 3.
leetcode的342. 4的幂 和3的幂思路相同
边栏推荐
猜你喜欢
不要再用if-else
Anaconda 虚拟环境迁移
List类的超详细解析!(超2w+字)
嵌入式分享合集27
RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
危化企业双重预防机制数字化建设进入全面实施阶段
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
- [email protected] 594/[email prote"/>
RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote
开源生态研究与实践| ChinaOSC
随机推荐
基础软件与开发语言开源论坛| ChinaOSC
力扣707-设计链表——链表
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
CLIP论文解读
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法
不知道这4种缓存模式,敢说懂缓存吗?
转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G
边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
JMeter笔记5 |Badboy使用和录制
开源教育论坛| ChinaOSC
利用 rpush 和 blpop 实现 Redis 消息队列
Pytorch GPU 训练环境搭建
【STM32】标准库-自定义BootLoader
Use ControlTemplate or Style from resource file in WPF .cs and find the control
In-depth understanding of JVM-memory structure
Statistical machine learning 】 【 linear regression model
Alexa染料标记RNA核糖核酸|RNA-Alexa 514|RNA-Alexa 488|RNA-Alexa 430
简易电子琴设计(c语言)