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

posted on 2017-04-19 10:48  Sempron2800+  阅读(122)  评论(0编辑  收藏  举报