LeetCode 算法题476 Number Complement
原题
2. Add Two Numbers
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.
Example 1:
1 | Input: 5 |
Example 2:
1 | Input: 1 |
思路
把int型输入转化为二进制字符串,然后根据字符串里每个字符是1还是0进行转换,最后再转为int型即可。
这个通过golang实现的话比较简单,熟悉基础库即可。
代码仅供参考。
代码实现
Golang实现
https://github.com/cook-coder/my-leetcode-solution/tree/master/easy/476