Advertisement

算法复习:求解众数问题(分治)

阅读量:
在这里插入图片描述

解析:
采用分治策略,每次计算s[mid]的出现次数,并进行统计。将字符串s划分为两个子部分,再对每个子部分分别进行处理。

代码:

复制代码
    #include<iostream>
    #include<algorithm>
    #include<vector>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::vector;
    
    
    void search(const vector<int>& s, int low, int high, int &mid, int& left, int& right)
    {
    	mid = (high + low)/2;
    	for (left = low; left <= high; left++)
    		if (s[left] == s[mid])break;
    	for (right = left + 1; right <= high; right

全部评论 (0)

还没有任何评论哟~