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

寻找Coder算法
本文介绍了一个高效的算法,用于从字符串数组中查找包含Coder的字符串,并按Coder出现次数排序返回。通过案例演示了算法的具体实现过程。
寻找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 ;
	}
};


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值