This topic has been archived. It cannot be replied.
-
工作学习 / IT杂谈 / 求助,有道题帮忙做做,找到算法即可。thanks!!Given an array, any element inside the array occurs even number of times, so for example if 3 is in array then it occurs even number of times
ect, but there is one such number which is occuring odd number of times. Find it, you do not know whether the number are in some given range. the algorithm has to be of orderO(n).
-dajiayihao(打假);
2005-3-29
{323}
(#2209853@0)
-
hashtable h;
arrary a;
for each element in a {
if(h.find(a[i])) {h.remove(a[i])} else { h.put(a[i]);}
}
get the element in h, that it is.
-davidwan(David);
2005-3-30
(#2210121@0)
-
xor them together if all the elements are number (integer). The hashtable algorithm is not o(n), becaus the sort/find will take time too.
-jchonc(James);
2005-3-30
(#2210375@0)
-
Good idea, but how to implement? xor only gives logical outputs of 0/1. Could you elaborate on example (1,3,1,3,4)? Thx.
-sammy(694 days);
2005-3-30
(#2210449@0)
-
of course most of the language has bit xor operator.
-jchonc(James);
2005-3-30
(#2210588@0)
-
yeah, just like encrypt and decrypt. Thx.
-sammy(692 days);
2005-3-30
(#2210635@0)
-
why encryption? c/c++/pascal/java/c#/asm8086 all has it.
-jchonc(James);
2005-3-30
(#2210876@0)
-
"like"
-sammy(692 days);
2005-3-30
(#2210944@0)