Pregunta de entrevista de Goldman Sachs

write a function to swap integers not using third int?

Respuestas de entrevistas

Anónimo

21 mar 2011

I believe this can be solved with simple subtraction: A = A + B B = A - B A = A - B For example, if A = 10 and B = 24: A = 10 + 24 = 34 B = 34 - 24 = 10 A = 34 - 10 = 24

7

Anónimo

19 mar 2011

IIRC, this can be done using three XOR operations: A = A xor B B = A xor B A = A xor B

1