C#-Simple way and Accepted Solution


  • -1
    R

    public class Solution
    {
    static int result = 0;

    public void testing(int k, int index)
         {
                if (k == 0)
                    k = 1;
                else
                   k = 0;
                   result += k * Convert.ToInt32(Math.Pow(2, index));
        }
    public int FindComplement(int num) 
    {
            int n, index=0; 
            result = 0;
            while(num!=0)
            {
                n =   num % 2;
                num = num / 2;
                testing(n,index);
                index++;
            }
            return result;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.