public class Solution { public int FindComplement(int num) { //计算数字二进制的反码 var list = new List<int>(); do { var n = num % 2; list.Add(n); num /= 2; } while (num != 0); var length = list.Count; for (int i = 0; i < length; i++) { if (list[i] == 0) { list[i] = 1; } else { list[i] = 0; } } int result = 0; for (int i = 0; i < length; i++) { result += list[i] * Convert.ToInt32(Math.Pow(2, i)); } //Console.WriteLine(result); return result; } }
https://leetcode.com/problems/number-complement/#/description