Skip to content

Commit f4376c0

Browse files
committed
Binary Number with Alternating Bits.
1 parent 498bf11 commit f4376c0

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>326.Power of Three (Playground).xcscheme</key>
8+
<dict>
9+
<key>isShown</key>
10+
<false/>
11+
<key>orderHint</key>
12+
<integer>0</integer>
13+
</dict>
14+
</dict>
15+
</dict>
16+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
3+
4+
5+
6+
Example 1:
7+
Input: n = 5
8+
Output: true
9+
Explanation: The binary representation of 5 is: 101
10+
11+
Example 2:
12+
Input: n = 7
13+
Output: false
14+
Explanation: The binary representation of 7 is: 111.
15+
16+
Example 3:
17+
Input: n = 11
18+
Output: false
19+
Explanation: The binary representation of 11 is: 1011.
20+
21+
22+
Constraints:
23+
- 1 <= n <= 231 - 1
24+
*/
25+
class Solution {
26+
func hasAlternatingBits(_ n: Int) -> Bool {
27+
var x = 0
28+
while pow(2, power: x) <= n {
29+
x += 1
30+
}
31+
var arr = Array(repeating: 0, count: x)
32+
33+
var i = n
34+
while i != 0 {
35+
var x = 0
36+
while pow(2, power: x) <= i {
37+
x += 1
38+
}
39+
arr[x - 1] = 1
40+
i -= pow(2, power: x - 1)
41+
}
42+
43+
for (i, c) in arr.enumerated() {
44+
if i > 0 {
45+
if c == 0 && arr[i - 1] == 0 {
46+
return false
47+
} else if c == 1 && arr[i - 1] == 1 {
48+
return false
49+
}
50+
}
51+
}
52+
53+
return true
54+
}
55+
56+
private func pow(_ base: Int, power: Int) -> Int {
57+
if base == 0 { return 0 }
58+
var result = 1
59+
for _ in 0..<power {
60+
result *= base
61+
}
62+
return result
63+
}
64+
}
65+
66+
let s = Solution()
67+
let r = s.hasAlternatingBits(4)
68+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/693.Binary Number with Alternating Bits.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
79. [Sum of Left Leaves](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/404.Sum%20of%20Left%20Leaves.playground/Contents.swift)
8484
80. [Convert a Number to Hexadecimal](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/405.Convert%20a%20Number%20to%20Hexadecimal.playground/Contents.swift)
8585
81. [Longest Palindrome](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/409.Longest%20Palindrome.playground/Contents.swift)
86-
82. [Fizz Buzz](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/409.Fizz%20Buzz.playground/Contents.swift)
86+
82. [Fizz Buzz](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/412.Fizz%20Buzz.playground/Contents.swift)
8787
83. [Third Maximum Number](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/414.Third%20Maximum%20Number.playground/Contents.swift)
8888
84. [Add Strings](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/415.Add%20Strings.playground/Contents.swift)
8989
85. [Number of Segments in a String](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/434.Number%20of%20Segments%20in%20a%20String.playground/Contents.swift)
@@ -123,6 +123,7 @@
123123
119. [Longest Continuous Increasing Subsequence](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/674.Longest%20Continuous%20Increasing%20Subsequence.playground/Contents.swift)
124124
120. [Valid Palindrome II](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/680.Valid%20Palindrome%20II.playground/Contents.swift)
125125
121. [Baseball Game](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/682.Baseball%20Game.playground/Contents.swift)
126+
122. [Binary Number with Alternating Bits](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/693.Binary%20Number%20with%20Alternating%20Bits.playground/Contents.swift)
126127

127128
#### Medium
128129

0 commit comments

Comments
 (0)