-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path476_Number_Complement.js
More file actions
41 lines (35 loc) · 1.02 KB
/
476_Number_Complement.js
File metadata and controls
41 lines (35 loc) · 1.02 KB
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
/*
476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.
*/
const expect = require('expect');
describe('476 Number Complement', () => {
it('returns a complement number', () => {
//arrange
const input = 5;
const expected = 2;
//act
const actual = findComplement(input);
//assert
expect(actual).toBe(expected);
});
it('returns a large complement number', () => {
//arrange
const input = 4234663296;
const expected = 60303999;
//act
const actual = findComplement(input);
//assert
expect(actual).toBe(expected);
});
});
const findComplement = (nums) => {
const result = [];
for (const digit of nums.toString(2)) {
result.push(digit === '0' ? '1' : '0');
}
return parseInt(result.join(''), 2);
};