reference:
http://www.geeksforgeeks.org/find-the-number-occurring-odd-number-of-times/
Problem Definition:
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.
Solution:
Do bitwise XOR of all the elements. Finally we get the number which has odd occurrences.
Code:
int getOddOccurrence(int ar[], int ar_size)
{
int i;
int res = 0;
for (i=0; i < ar_size; i++)
res = res ^ ar[i];
return res;
}
本博客介绍了一个算法,用于在给定数组中找到出现奇数次的整数,仅需O(n)时间复杂度和常数空间。通过执行位操作XOR运算,最终得到所需整数。
4036

被折叠的 条评论
为什么被折叠?



