您的位置:首页 > 其它

寻找Coder(toupper 的用法)--------去哪儿2015研发工程师笔试题

2016-07-23 21:51 495 查看
寻找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 ;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: