Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
I plan to update the implementation of all after-school code exercises in the 23 King's Road data structure. Although the exams are generally written in pseudocode, I have implemented all of them for obsessive-compulsive disorder. The warehouse is here.
- Linear table
2.2.3, 12
- Find duplicate elements, and the number of elements is more than half of the array, so an array has at most one main element
- Violence is to exchange space for time, similar to bucket sorting
- Open a new array, the subscript of the new array corresponds to the element value, and the value corresponds to the number of elements (because the range of the number is determined)
- Time complexity O(n), space complexity O(n)
int find_main(SqList list) {
// 1.初始化一个默认值全为0的数组
int tmp[list.length] = {0};
// 2.下标对应元素值,
for (int i = 0; i < list.length; i++) {
tmp[list.data[i]]++;
}
// 3.遍历找出主元素
for (int i = 0; i < list.length; i++) {
if (tmp[i] > list.length / 2 )
return i;
}
// 4.没找到返回-1
return -1;
}
复制代码
- The second method: we first sort the order and then find the main element
- Record the number of times each element appears, and if the following number is not a repeating element, determine whether the current element is the main element
- Because quicksort is used, the time complexity is O(nlogn), and the space complexity is O(logn)
- The total score is 13 points, and this solution can get up to 11 points, so don't force the optimal solution
int find_main2(SqList list) {
// 1.快排
sort(list.data, list.data+list.length);
// 2.记录每个元素出现的次数,找到主元素
int cnt = 1;
for (int i = 0; i < list.length - 1; i++) {
if (list.data[i+1] == list.data[i]) {
cnt++;
} else {
if (cnt > list.length / 2)
return list.data[i];
cnt = 1;
}
}
return -1;
}
复制代码
- When this question is tested (
probably probably) can not write the specific implementation of sorting that has nothing to do with the topic - Because the space required for each recursion of quicksort is fixed, the number of recursive layers is the space complexity, so the average space complexity of quicksort is O(logn)
- The implementation of quicksort is as follows:
void quick_sort(int l, int r) {
if (l == r) return;
int x = a[(l+r)/2]; // 1.枢纽元素
// 2.分成两块
int i = l-1, j= r+1;
while (i < j) {
while (a[++i] < x); // 找到左边比x大的元素
while (a[--j] > x); // 找到右边比x小的元素
if (i < j) swap(a[i], a[j]); // 互换
}
// 3.递归
quick_sort(l, j);
quick_sort(j+1, r);
}
复制代码
- optimal solution
- Since the number of main elements is more than half of the array, it can offset all other elements and have the remainder
- Time complexity O(n), space complexity O(1)
int find_main3(SqList list) {
int c, cnt = 1;
c = list.data[0];
// 1.选定候选主元素
for (int i = 1; i < list.length; i++) {
if (list.data[i] == c) cnt++;
else {
if (cnt > 0) {
cnt--;
} else {
c = list.data[i];
cnt = 1;
}
}
}
// 2.统计实际数量
if (cnt > 0) {
cnt = 0;
for (int i = 0; i < list.length; i++)
if (list.data[i] == c)
cnt++;
}
// 3.确认主元素
if (cnt > list.length / 2) return c;
return -1;
}
复制代码