- 浏览: 183450 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
判断一个整数是否为3的幂。如果一个整数是3的幂,那么就可以表示为
3 ^ x = n => log(3^x) = log(n) => xlog(3) = log(n) => x = log(n) / log(3)。我们只需要判断x是否为整数就可以了,代码如下:
Follow up:
Could you do it without using any loop / recursion?
判断一个整数是否为3的幂。如果一个整数是3的幂,那么就可以表示为
3 ^ x = n => log(3^x) = log(n) => xlog(3) = log(n) => x = log(n) / log(3)。我们只需要判断x是否为整数就可以了,代码如下:
public class Solution { public boolean isPowerOfThree(int n) { if(n <= 0) return false; double d = Math.log10(n) / Math.log10(3); return d % 1 == 0 ? true : false; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Power of Three.java
1.6 Is Power Of Three(是否是3的幂) 1.7 Count Primes(质数的个数) 2. Algorithm Implementation Questions (算法实现题) 3. Linked List Questions(链表相关问题) 4. Array Questions(数组相关问题) 5. ...
标题与描述均提到了"三电平NPC逆变器在三相四线电力质量补偿器中的应用通过广义三维空间矢量调制(pdf)",这表明文章主要关注于三电平NPC逆变器如何作为三相四线电力质量补偿器,并通过广义三维空间矢量调制(3DSVM)...
指数运算如2^3读作"two to the power of three",根号如√4读作"the square root of four"。方程如2x + 3 = 5可以读作"two x plus three equals five"。 微积分领域,导数的符号"dy/dx"读作"the derivative of y ...
- **乘方**:“^”或“**”读作“raised to the power of”,如“2^3”读作“two raised to the power of three”。 - **开方**:“√”读作“square root of”,如“√25”读作“square root of twenty-five”。 - ...
例如,2^3 表示 "two to the power of three"。对数符号 "log" 用于表示 "logarithm",而圆周率 π 用 "pi" 表示。 在微积分中,导数符号 "′" 或 "d/dx" 代表 "derivative with respect to",积分符号 "∫" 说成 ...
人工智能 Lecture 16: Three Learning Principles Occam’s Razor simple, simple, simple! Sampling Bias match test scenario as much as ... Power of Three relatives, bounds, models, tools, principles
231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [C++](./C++/power-of-two.cpp) [Python](./Python/power-of-two.py) | _O(1)_ | _O(1)_ | Easy | LintCode | 260 | [Single Number III]...
Simulation schematic, STATCOM ... In the simulation model to simulate the use of three-phase power grid section ends connected load, STATCOM main circuit reactance in parallel by connecting to the grid
The **Three-Phase Fault Block** in Simulink is a powerful tool used to simulate various types of faults in three-phase power systems. This block is particularly useful for studying the impact of ...
This project is essentially a simulation of hydroelectric dam created on MATLAB ,to predict the electricity supplied by hydroelectric dam by forecasting the next hour electricity demand based on three...
Advances in optical technology and computing power are bringing life-like 3DTV closer, with potential applications not only in entertainment, but also in education, scientific research, industry, ...
A real synchrophasor set composing of three different types of disturbance events from the Frequency Monitoring Network (FNET) is used. An image embedding technique called Gramian Angular Field is ...
Three.js is the most popular JavaScript library for displaying 3D content on the web, giving you the power to display incredible models, games, music videos, and scientific/data visualizations in your...
His three-book series on Power Electronics focuses on three essential topics in the power sequence based on applications relevant to this age of sustainable energy such as wind turbines and hybrid ...
power device that consists of three components: a sensing ... algorithm for nodes localization in three-dimensional WSN. Since MDS-MAP is one of the algorithms that produce ... Finally, we conclude ...
本文中提及的模型是为了在PSS®E(Power System Simulator for Engineering)软件中对三相三绕组变压器或自耦变压器进行正序负载流分析。正序负载流分析是电力系统稳态分析中的一种方法,主要关注的是在正常运行条件...
Three.js is the most popular JavaScript library for displaying 3D content on the web, giving you the power to display incredible models, games, music videos, and scientific/data visualizations in your...