当前位置:网站首页>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的幂思路相同
边栏推荐
猜你喜欢
随机推荐
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
力扣59-螺旋矩阵 II——边界判断
Detailed explanation of JWT
Kubernetes资源编排系列之三: Kustomize篇 作者 艄公(杨京华) 雪尧(郭耀星)
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
云服务器如何安全使用本地的AD/LDAP?
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
leetcode 899. 有序队列
Anaconda 虚拟环境迁移
自定义form表单验证
leetcode 268. 丢失的数字(异或!!)
Hinton2022年RobotBrains访谈记录
小马智行起诉擎天智卡:索赔6000万 彭军称要斗争到底
开源教育论坛| ChinaOSC
算法--交错字符串(Kotlin)
Use ControlTemplate or Style from resource file in WPF .cs and find the control
利用 rpush 和 blpop 实现 Redis 消息队列
Anaconda virtual environment migration
多模态 参考资料汇总
若依集成browscap读取浏览器用户代理