当前位置:网站首页>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的幂思路相同
边栏推荐
- ThreadLocal详解
- 宁德时代2号人物黄世霖辞任副董事长:身价1370亿
- Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
- 1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
- 数据驱动的软件智能化开发| ChinaOSC
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
- Hinton2022年RobotBrains访谈记录
- RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
- 第三方验收测试报告有什么作用?如何获取权威软件测试报告?
- Detailed steps for tensorflow-gpu2.4.1 installation and configuration
猜你喜欢

Hinton2022年RobotBrains访谈记录

Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)

【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)

【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停

Edge box + time series database, technology selection behind Midea's digital platform iBuilding

力扣203-移除链表元素——链表

Anaconda 虚拟环境迁移

收藏-即时通讯(IM)开源项目OpenIM-功能手册

async 和 await 原来这么简单

CLIP论文解读
随机推荐
第三方验收测试报告有什么作用?如何获取权威软件测试报告?
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
Anaconda virtual environment migration
CLIP论文解读
C中的数据存储
wordpress建立数据库连接时出错
【飞控开发高级教程4】疯壳·开源编队无人机-360 度翻滚
leetcode 2119. 反转两次的数字
华为设备配置VRRP负载分担
leetcode 326. 3 的幂
LeetCode 1374. 生成每种字符都是奇数个的字符串
Golang死信队列的使用
开源生态研究与实践| ChinaOSC
Go语言类型与接口的关系
调用EasyCVR云台控制接口时,因网络延迟导致云台操作异常该如何解决?
Detailed explanation of JWT
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
软件测试基本流程有哪些?权威的第三方软件检测机构推荐