织梦行云

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Given a set of numbers where each number appears twice in the set except one number t, who only appears once. Now the task is to find out t.

Yes, if it is the first time you find the answer, you may probably scream out. The mechod is very simple, just use an operation: bitwise EXCLUSIVE OR, or XOR (^). Scan the set, keep doing XOR to the current number and previous result to get the new result, and the last result is the number t.

For example, suppose the set is: 1 5 2 1 2, then the result 1 XOR 5 XOR 2 XOR 1 XOR 2 = 5. Does it seem like a magic?

posted on 2010-03-16 00:10  BloodElf  阅读(97)  评论(0编辑  收藏  举报