Pergunta de entrevista da empresa Goldman Sachs

write a function to swap integers not using third int?

Respostas da entrevista

Sigiloso

21 de mar. de 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

Sigiloso

19 de mar. de 2011

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

1