use XOR to sovle it


  • 0
    D
    public int findComplement(int num) {
          int eNum = num;
          int comNum = 1;
          while(eNum > 0){
    	  eNum = (eNum >> 1);
    	 comNum = (comNum << 1);
          }
         return (comNum-1)^num;
    }
    

Log in to reply
 

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