当前位置:网站首页>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的幂思路相同
边栏推荐
- Golang死信队列的使用
- 剑指 Offer II 044. 二叉树每层的最大值-dfs法
- 149. The largest number on a straight line, and check the set
- 【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
- Auto.js脚本程序打包
- Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
- ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
- 若依集成browscap读取浏览器用户代理
- ES6-箭头函数
- ThreadLocal详解
猜你喜欢
随机推荐
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
子结点的数量(2)
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
tensorflow-gpu2.4.1安装配置详细步骤
双线性插值公式推导及Matlab实现
宁德时代2号人物黄世霖辞任副董事长:身价1370亿
glide set gif start stop
Mapper输出数据中文乱码
力扣206-反转链表——链表
ES6解构赋值--数组解构及对象解构
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
盘点在线帮助中心对企业能够起到的作用
Solidity智能合约开发 — 4.1-合约创建和函数修饰器
matplotlib画polygon, circle
抖音web逆向教程
Anaconda virtual environment migration
leetcode 899. 有序队列
leetcode 16.01. 交换数字(不使用临时变量交换2个数的值)
leetcode 剑指 Offer 58 - II. 左旋转字符串
Detailed AST abstract syntax tree








