26. Power of Three - Jake blog - Java coding practice log (2017)" /> 26. Power of Three - Jake blog - Java coding practice log (2017)" />

326. Power of Three

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?

If this question comes up in an interview, I'll eat shit.

class Solution { public boolean isPowerOfThree(int n) { if(n ==1) return true; while(n%3 == 0 && n>3){ n = n/3; } if(n ==3){ return true; } else{ return false; } } }

This websiteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"326. Power of Three"

164
0 1 164

Further Reading

Post a reply

Log inYou can only comment after that.

Comment list (1 item)

  • tt
    tt 2018-04-18 00:51

    Like your comment.

Share this page
Back to top