创新工场涂鸦移动测试题-软件工程师

来源:互联网 发布:背叛了男朋友 知乎 编辑:程序博客网 时间:2024/05/16 14:45

1. 找出n个字符串中出现次数最多的字符串。

 

C/C++:

char* find(char **data,int n);

Java:

String find(Stringdata[]);

 

说明:

1. data是字符串数组,n是数组中字符串的个数,返回值为出现次数最多的字符串。

2. 若结果有多个,返回任意一个即可

3. 不得使用任何库函数/API,如需使用类似功能, 请自行实现

4. 算法效率尽可能高,尽量少的使用内存空间

5. 必须要有代码注释和算法说明

例如:data里面的数据是{“paper”,”cup”,”book”,”cup”,”pen”,”book”}。n= 6。返回结果为”cup”或”book”。


code

#include <iostream>using namespace std;char * find(char **data, int n){char *p=NULL;int strMax = 1;for(int i = 0; i < n; i++){if (data[i]!=" "){int temp = 1;for (int j = i + 1; j < n; j++){if (data[i] == data[j]){data[j] = " ";temp++;}}if (temp>strMax){strMax = temp;p = data[i];}}}cout << strMax<<endl;return p;}int main(){char *data[7] = { "paper", "cup", "book", "cup", "pen", "book", "book" };cout << find(data, 7) << endl;system("pause");return 0;}


0 0
原创粉丝点击