231. Power of 2 | Power of

Source: Internet
Author: User

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

Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.

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

Example 1:

Input: 1 output: true explanation: 20 = 1

Example 2:

Input: 16 Output: true explanation: 24 = 16

Example 3:

Input: 218 Output: false
24ms
1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3         ifN <1{return false}4         varNum:int =N5          while(num! =1)6         {7             ifnum%2==1{return false}8             Else{num/=2}9         }Ten         return true One     } A}

24ms

1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3Guard n >0 Else{return false }4         ifn = =1{return true }5         varnum =26          whileNum <N {7Num *=28         }9         returnnum = =NTen     } One}
1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3Guard n >0 Else{return false }4         ifn = =1{return true }5         varnum =26          whileNum <N {7Num *=28         }9         returnnum = =NTen     } One}

20ms

1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3         ifn = =1 {4             return true5         }6         7         varX:int =28          while(x <=N)9         {Ten             ifx = =N { One                 return true A             } -              -X *=2 the         } -         return false -     } -}

20ms

1 class Solution {2     func ispoweroftwo (_ N:int), Bool {3         //  Power of 1 '4         return15    } means  6 }

231. Power of 2 | 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.