-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
50 lines (41 loc) · 896 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2x.
Example 1:
Input: n = 1
Output: true
Explanation: 20 = 1
Example 2:
Input: n = 16
Output: true
Explanation: 24 = 16
Example 3:
Input: n = 3
Output: false
Constraints:
- -231 <= n <= 231 - 1
Follow up: Could you solve it without loops/recursion?
*/
class Solution {
func isPowerOfTwo(_ n: Int) -> Bool {
if n <= 0 { return false }
var x = 0
while pow(2, pow: x) <= n {
x += 1
}
return pow(2, pow: x - 1) == n
}
private func pow(_ base: Int, pow: Int) -> Int {
var res = 1
for _ in 0..<pow {
res *= base
}
return res
}
}
let s = Solution()
let r = s.isPowerOfTwo(0)
print(r)