C++ N/3重复编号
问题是找出数组中任何整数的出现次数是否超过n/3次。 该解决方案基于Moore的投票算法(如果我们用与e不同的所有其他元素抵消元素e的每次出现,那么e将一直存在,如果它是多数元素) 现在我们只需要检查候选元素的计数是否大于n/2 在寻找多数元素的情况下,这种寻找多数元素候选者的方法是可以理解的,即至少比其他元素重复次数多的数字,即使是一个,也是候选者,但其在该问题中的应用尚不清楚 这里写到,数组中有3个不同的元素,如果将它们从数组中删除,则答案不变,并保留2个元素及其计数 在多数情况下,我们保留一个作为多数候选人,但为什么在这里保留两个,而不考虑所需的更改次数。 示例:对于n=3/4/5,其大于1倍,对于n=6/7/8,其大于2倍,等等。伪代码如下:C++ N/3重复编号,c++,algorithm,data-structures,array-algorithms,C++,Algorithm,Data Structures,Array Algorithms,问题是找出数组中任何整数的出现次数是否超过n/3次。 该解决方案基于Moore的投票算法(如果我们用与e不同的所有其他元素抵消元素e的每次出现,那么e将一直存在,如果它是多数元素) 现在我们只需要检查候选元素的计数是否大于n/2 在寻找多数元素的情况下,这种寻找多数元素候选者的方法是可以理解的,即至少比其他元素重复次数多的数字,即使是一个,也是候选者,但其在该问题中的应用尚不清楚 这里写到,数组中有3个不同的元素,如果将它们从数组中删除,则答案不变,并保留2个元素及其计数 在多数情况下,我们保留
When we encounter a new element, there are 3 cases possible :
1)We don’t have 2 elements yet. So add this to the list with count as 1.
2)This element is different from the existing 2 elements. As we said before, we
have 3 distinct numbers now. Removing them does not change the answer. So
decrements 1 from count of 2 existing elements. If their count falls to 0,
obviously its not a part of 2 elements anymore.
3)The new element is same as one of the 2 elements. Increment the count of that
element.
代码如下:
for (int i = 0; i < n; i++) {
// if this element is previously seen,
// increment count1.
if (first == arr[i])
count1++;
// if this element is previously seen,
// increment count2.
else if (second == arr[i])
count2++;
else if (count1 == 0) {
count1++;
first = arr[i];
}
else if (count2 == 0) {
count2++;
second = arr[i];
}
// if current element is different from
// both the previously seen variables,
// decrement both the counts.
else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
// Again traverse the array and find the
// actual counts.
for (int i = 0; i < n; i++) {
if (arr[i] == first)
count1++;
else if (arr[i] == second)
count2++;
}
if (count1 > n / 3)
return first;
if (count2 > n / 3)
return second;
Consequently, the answer will be one of the 2 elements left behind. If they are
not the answer, then there is no element with count > N / 3
for(inti=0;in/3)
先返回;
如果(计数2>n/3)
返回第二;
因此,答案将是留下的两个要素之一。如果是
不是答案,则不存在计数>N/3的元素
为什么要考虑候选人是两个和资格标准?因为这似乎是一个关于算法的问题,而不是关于具体的实现,我删除了
c++
标记,并添加了语言不可知
标记。可能重复,因为这似乎是一个关于算法的问题,而不是关于具体实现我删除了c++
标记,并添加了语言不可知标记
for (int i = 0; i < n; i++) {
// if this element is previously seen,
// increment count1.
if (first == arr[i])
count1++;
// if this element is previously seen,
// increment count2.
else if (second == arr[i])
count2++;
else if (count1 == 0) {
count1++;
first = arr[i];
}
else if (count2 == 0) {
count2++;
second = arr[i];
}
// if current element is different from
// both the previously seen variables,
// decrement both the counts.
else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
// Again traverse the array and find the
// actual counts.
for (int i = 0; i < n; i++) {
if (arr[i] == first)
count1++;
else if (arr[i] == second)
count2++;
}
if (count1 > n / 3)
return first;
if (count2 > n / 3)
return second;
Consequently, the answer will be one of the 2 elements left behind. If they are
not the answer, then there is no element with count > N / 3