[Leetcode] [JavaScript] Power of

Source: Internet
Author: User

Power of

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

https://leetcode.com/problems/power-of-two/

Two points.

The power of the whole number of 2, either after the radical is a whole number (the number is also 2 of the whole power), or divided by 2 after the root is an integer. Continue to recursively judge the result of the radical until it touches 1 or 2.

For example, 1024 (2^10), which is multiplied by two 32 (2^5), 2048 (2^11), is multiplied by two 32 (2^5) and multiplied by 2.

1 /**2 * @param {number} n3 * @return {Boolean}4  */5 varIspoweroftwo =function(n) {6     if(N < 1){7         return false;8}Else if(n = = 2 | | n = = 1){9         return true;Ten}Else{ One         varsqrt =math.sqrt (n); A         if(sqrt = = =parseint (sqrt)) { -             returnispoweroftwo (sqrt); -}Else{ then = N/2; -sqrt =math.sqrt (n); -             if(sqrt = = =parseint (sqrt)) { -                 returnispoweroftwo (sqrt); +}Else{ -                 return false; +             } A         } at     } -};

[Leetcode] [JavaScript] Power of

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.