Ruby Solution


  • 0
    R
    # @param {Integer} a
    # @param {Integer} b
    # @return {Integer}
    def get_sum(a, b)
        # http://stackoverflow.com/questions/8698959/how-to-force-ruby-to-store-a-small-number-as-32-bit-integer
        a = Array(a).pack('l').unpack('l').first
        b = Array(b).pack('l').unpack('l').first
        
        # https://discuss.leetcode.com/topic/50178/golang-0ms-with-explantation
        #   A	B	C	S
        #   0	0	0	0
        #   1	0	0	1
        #   0	1	0	1
        #   1	1	1	0
        
        return a if b.zero?
        return b if a.zero?
        
        sum   = a ^ b
        carry = (a & b) << 1
        
        get_sum(sum, carry)
    end
    

Log in to reply
 

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