寻找Coder(toupper 的用法)--------去哪儿2015研发工程师笔试题
2016-07-23 21:51
495 查看
寻找Coder
请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回。结果字符串的顺序按照"Coder"出现的次数递减排列,若两个串中"Coder"出现的次数相同,则保持他们在原数组中的位置关系。
给定一个字符串数组A和它的大小n,请返回结果数组。保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。
测试样例:
第三次做:
请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回。结果字符串的顺序按照"Coder"出现的次数递减排列,若两个串中"Coder"出现的次数相同,则保持他们在原数组中的位置关系。
给定一个字符串数组A和它的大小n,请返回结果数组。保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。
测试样例:
["i am a coder","Coder Coder","Code"],3
返回:["Coder Coder","i am a coder"]
class Coder { private: struct node { int ip ; int count ; node( int _ip, int _count ) : ip( _ip ) , count( _count ) {} } ; static bool cmp ( node p, node q ) { return p.count > q.count ; } public: vector<string> findCoder(vector<string> vec, int n) { // write code here vector<string> retVec ; if ( vec.empty() == true || n <= 0 ) return retVec ; vector<node> counterVec ; for ( int i = 0; i < vec.size(); ++ i ) { int num = 0 ; string str = vec[i] ; for ( int j = 0; j < str.size(); ) { if ( toupper( str[j] ) == 'C' && toupper( str[j + 1] ) == 'O' && toupper( str[j + 2] ) == 'D' && toupper( str[j + 3] ) == 'E' && toupper( str[j + 4] ) == 'R' ) { ++ num ; j += 4 ; } else { j += 1 ; } } if ( num > 0 ) { counterVec.push_back( node( i, num ) ) ; } } std::stable_sort( counterVec.begin(), counterVec.end(), cmp ) ; for ( int i = 0; i < counterVec.size(); ++ i ) { retVec.push_back( vec[ counterVec[i].ip ] ) ; } return retVec ; } };
第二次做:
class Coder { struct node { int ip ; int count ; node(int _ip, int _count): ip( _ip ), count( _count ) {} } ; private: static bool cmp( node first, node next ) { return first.count > next.count ; } public: vector<string> findCoder(vector<string> vec, int n) { // write code here vector<node> counterVec ; vector<string> retVec ; for ( int i = 0; i < vec.size(); ++ i ) { int num = 0 ; string str = vec[i] ; for ( int j = 0; j < str.size(); ) { // 这里不要写 ++ j ; ! if ( toupper( str[j] ) == 'C' && toupper( str[j + 1] ) == 'O' && toupper( str[j + 2] ) == 'D' && toupper( str[j + 3] ) == 'E' && toupper( str[j + 4] ) == 'R' ) { ++ num ; j += 4 ; } else { j += 1 ; } } if ( num > 0 ) { counterVec.push_back( node( i, num ) ) ; } } stable_sort( counterVec.begin(), counterVec.end(), cmp ) ; for ( int i = 0; i < counterVec.size(); ++ i ) { retVec.push_back( vec[counterVec[i].ip] ) ; } return retVec ; } };
第三次做:
bool cmp(pair<string, int> first, pair<string, int> next){ return first.second > next.second; } class Coder { public: vector<string> findCoder(vector<string> vec, int n) { // write code here vector<pair<string, int>> tmp ; for ( int i = 0; i < vec.size(); ++ i ) { string str = vec[i] ; int time = 0 ; for ( int j = 0; j < str.size(); ) { if ( toupper( str[j] ) == 'C' && toupper( str[j + 1] ) == 'O' && toupper( str[j + 2] ) == 'D' && toupper( str[j + 3] ) == 'E' && toupper( str[j + 4] ) == 'R' ) { ++ time ; j += 5 ; } else { j += 1 ; } } tmp.push_back( make_pair( vec[i], time ) ) ; } stable_sort( tmp.begin(), tmp.end(), cmp ) ; vector<string> result ; for ( int i = 0; i < tmp.size(); ++ i ) { if ( tmp[i].second != 0 ) { result.push_back( tmp[i].first ) ; } else { break ; } } return result ; } };
相关文章推荐
- 关于CUDA实现最值问题
- POJ3438 ZOJ2886 UVALive3822 Look and Say【数列+水题】
- Dialog源码分析
- 组合问题与动态规划的联系之应用
- 适配器(一)
- Log4Net使用详解
- poj1703 并查集
- 关键字、结构体总结以及typedef,枚举与#define区别
- 8086汇编语言自学经验分享 除数16位的DIV指令
- (linux)idr(integer ID management)机制
- Understanding HDFS Recovery Processes (Part 2)
- HDU 1372 BFS
- grails数据库部署
- 二分查找算法的C++和Python实现
- 个人博客搭建之旅
- ggplot2 geom相关设置—分布图
- Linux系统常用命令(咋个办呢 zgbn)
- 中缀表达式转换成前缀表达式和后缀表达式的极其简单方法
- 7.14 编一个程序,将两个字符串s1和s2进行比较。
- 红黑树