Skip to content

Commit df9bdb9

Browse files
committed
Number complement.
1 parent 169d3f3 commit df9bdb9

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed
Lines changed: 16 additions & 0 deletions
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>461.Hamming Distance (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>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
3+
4+
For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
5+
Given an integer num, return its complement.
6+
7+
 
8+
9+
Example 1:
10+
Input: num = 5
11+
Output: 2
12+
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
13+
14+
Example 2:
15+
Input: num = 1
16+
Output: 0
17+
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
18+
 
19+
20+
Constraints:
21+
- 1 <= num < 231
22+
 
23+
24+
Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/
25+
*/
26+
class Solution {
27+
func findComplement(_ num: Int) -> Int {
28+
var x = 0
29+
while pow(2, x) <= num {
30+
x += 1
31+
}
32+
x -= 1
33+
var arr = decimalToBinary(num, maxBit: x)
34+
print(arr)
35+
arr = arr.map { $0 == 1 ? 0 : 1 }
36+
print(arr)
37+
var res = 0
38+
for (i, e) in arr.enumerated() {
39+
if e == 1 {
40+
res += pow(2, i)
41+
}
42+
}
43+
return res
44+
}
45+
46+
private func pow(_ base: Int, _ power: Int) -> Int {
47+
if base == 0 { return 0 }
48+
var res = 1
49+
for _ in 0..<power {
50+
res *= base
51+
}
52+
return res
53+
}
54+
55+
private func decimalToBinary(_ num: Int, maxBit bit: Int) -> [Int] {
56+
var n = num
57+
var i = bit
58+
var arr = Array(repeating: 0, count: bit + 1)
59+
while i >= 0 && n > 0 {
60+
while pow(2, i) <= n {
61+
n -= pow(2, i)
62+
arr[i] = 1
63+
}
64+
i -= 1
65+
}
66+
return arr
67+
}
68+
}
69+
70+
71+
let s = Solution()
72+
let r = s.findComplement(18)
73+
print(r)
Lines changed: 4 additions & 0 deletions
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/476.Number Complement.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
90. [Assign Cookies](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/455.Assign%20Cookies.playground/Contents.swift)
9595
91. [Repeated Substring Pattern](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/459.Repeated%20Substring%20Pattern.playground/Contents.swift)
9696
92. [Hamming Distance](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/461.Hamming%20Distance.playground/Contents.swift)
97+
93. [Number Complement](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/476.Number%20Complement.playground/Contents.swift)
9798

9899
#### Medium
99100

0 commit comments

Comments
 (0)