找出 1..100 中缺失的整数
figure out which integer is missing
题目详情
你有一个数组,包含集合 中的 99 个互不相同的整数。如何写程序找出缺失的那个整数?
You have an array that contains 99 distinct integers from the set 1,2,3,..., 100. How would you write a program to figure out which integer is missing?
解析
两种常见做法(都为 时间、 额外空间):
方法 1:求和
总和应为
缺失数 。
方法 2:按位异或
利用 、。计算
成对抵消后剩下的就是缺失数。