본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode. Count Primes [Math]

by 벤진[Benzene] 2020. 6. 15.

Problem:

Given an integer, write a function to determine if it is a power of three.

 

Example 1:

Input: 27

Output: true

 

Example 2:

Input: 0

Output: false

 

Example 3:

Input: 9

Output: true

 

Example 4:

Input: 45

Output: false

 

Follow up:
Could you do it without using any loop / recursion?

 

-Summary-

1. Check if n is bigger than 0 for when input is 0 

2. Check if given n can divide the maximum number for the power of three. (1162261467 = 3^19)

discussion을 보니 array 를 써서 intuitive하게 바로 푼 답도 있었다.

leetcode.com/problems/power-of-three/discuss/77903/Two-simple-solutions-without-recursion-or-iteration:-O(1)-time-and-O(1)-space

 

Two simple solutions without recursion or iteration: O(1) time and O(1) space - LeetCode Discuss

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

댓글