Skip to content

Commit 05d4977

Browse files
authored
Create 476-number-complement.js
1 parent cdab6a2 commit 05d4977

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

476-number-complement.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number} num
3+
* @return {number}
4+
*/
5+
const findComplement = function(num) {
6+
const toBin = num => (num >>> 0).toString(2)
7+
const flip = str => {
8+
let res = ''
9+
for(let c of str) res += (c === '1' ? '0' : '1')
10+
return res
11+
}
12+
return parseInt(flip(toBin(num)), 2)
13+
};

0 commit comments

Comments
 (0)