当前位置:网站首页>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的幂思路相同
边栏推荐
猜你喜欢
随机推荐
力扣707-设计链表——链表
ES6解构赋值--数组解构及对象解构
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
极验深知v2分析
alicloud3搭建wordpress
ES6简介及let、var、const区别
转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G
leetcode 2119. 反转两次的数字
leetcode 899. 有序队列
盘点在线帮助中心对企业能够起到的作用
开源教育论坛| ChinaOSC
华为设备配置VRRP负载分担
LeetCode 1374. 生成每种字符都是奇数个的字符串
leetcode 461. 汉明距离
【微信小程序2】事件传参与数据同步[03]
LeetCode 622. 设计循环队列
RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote
C51 存储类型与存储模式
后台图库上传功能
为什么 BI 软件都搞不定关联分析









