scala version


  • 1
    M
      def findComplement(num: Int): Int = {
        ~num & (Integer.highestOneBit(num) - 1)
      }
    

  • 0
    K

    thx. Very elegant!


  • 0
    K

    my version

        def findComplement(num: Int): Int = {
            if (num == 1) return 0
            val _complementInt = ~num
            val cplBinaryString = _complementInt.toBinaryString.split("0", 2)(1)
            val cplInt = Integer.parseInt(cplBinaryString, 2)
            cplInt
        }
    

Log in to reply
 

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