当前位置:网站首页>Leetcode-326. Power of 3
Leetcode-326. Power of 3
2022-07-23 05:19:00 【KGundam】
Mathematical problems
Topic details
Given an integer , Write a function to determine if it is 3 Power square . If it is , return true ; otherwise , return false .
Integers n yes 3 To the power of : There are integers x bring n == 3^x
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
Ideas :
The first method : Using logarithm . set up logn3 = m, If n yes 3 Integer power of , that m It must be an integer .
class Solution
{
public:
bool isPowerOfThree(int n)
{
//logn3 = log10(n) / log10(3)
//fmod(n, 1) Judge n Is it an integer , Is to return 0
return fmod(log10(n) / log10(3), 1) == 0;
}
};
Another opportunistic method : Because in int Within the scope of 3 The largest power of is 3^19 = 1162261467, If n yes 3 An integer of
Fang , that 1162261467 Divide n The remainder of must be zero ; vice versa .
class Solution
{
public:
bool isPowerOfThree(int n)
{
return n > 0 && 1162261467 % n == 0;
}
};
边栏推荐
- leetcode-415.字符串相加
- Leetcode-309. the best time to buy and sell stocks includes the freezing period
- Duilib Edit占位提示文本实现
- JS - - date Object & Ternary expression
- JS -- Date object & ternary expression
- Verilog pit avoidance Guide (continuously updated)
- leetcode-646. 最长数对链
- Verilog design related (continuous update)
- GIC 基础知识介绍 (一)
- 代码随想录笔记_链表_160相交链表
猜你喜欢
随机推荐
Connect to the server with vscode's remote SSH plug-in in the offline environment
在离线环境下用 VScode 的 Remote-SSH 插件连接服务器
Cookie
BOM browser object model
BOM浏览器对象模型
GIC Introduction (II) - use of gic400
DOM - node operation
Druid源码阅读9-DruidDataSource和DruidConnection中的状态
Jena and fuseki common commands
leetcode-204.计数质数
Arm V8 program guide - Chapter 10 aarch64 exception handling (translation)
“拨”出数位上的数字 - 多种思路实现反向输出一个四位数
leetcode-583. 两个字符串的删除操作
Druid源码阅读6-PreparedStatementPool源码及使用场景分析
handsontable 追加数据
Leetcode - 494. Objectifs et
Druid source code reading 8-druiddatasource removeabandoned mechanism
Introduction to basic knowledge of GIC (I)
leetcode-646. 最长数对链
Druid source code read the basic principle of 3-druiddatasource connection pool








