阿里12年笔试题

第20题:字符串数组seq[] = a,b,c,d,aa,ba,ca,da,ab,bb,cb,db,ac...,aaa,baa,...

(1)aaa是第几个字符串

(2)ababacd是第几个

(3)第1000个字符串是什么

(4)编写函数find(),返回字符串在seq中是第几个(语言不限)

分析: 四进制数,右边是高位。

已知一个字符串,求第几个:

int getindex(char a[])
{
    int len = strlen(a);
    if(len < 1) return 0;
    int sum = 0;
    
    for(int i = 0 ; i<len ; i++)
    {
        int tp = a[i] - 'a' + 1;
        sum = sum * 4 + tp;
    }

    return sum;
}

已知index ,求字符串:

char getchar(int index){
    switch(index){
        case 0 : return 'd';
        case 1 : return 'a';
        case 2 : return 'b';
        case 3 : return 'c';
        default : return '';
    }
}
string getStr(int index){

    string  str = "";
    while(index>0){
        int current = index % 4;
        char c = getchar(current);
        str+=c;
        index = index /4;
    }

    return str;
}

选择题: 判断一包含n个整数a[]中是否存在i、j、k满足a[i] + a[j] = a[k]的时间复杂度为(O(n^2)

bool isExist(vector<int> array){

    int len = array.size();
    if(len < 3) return false;
    sort(array.begin(), array.end());
    for(int k = len -1; k >=2 ;--k)
    {
        int i = 0,j = k-1;
        int target = array[k];
        while(i < j){
            int tp = array[i] + array[j];
            if(tp == target)return true;
            else if(tp < target) ++i;
            else    --j;
        }
    }
    
    return false;
}

1 字符串逆序 但是要保证每个单词不逆序

void reverse(char *str, char *last)
{
    char *first = str;
    while(first < last){
        char tp = *first;
        *first = *last;
        *last = tp;
        ++first;
        --last;
    }
}
void reverseSentence(char * str){

    if(str == NULL) return ;
    int len = strlen(str);
    reverse(str, str + len -1);
    
    char *first = str;
    char *last = str;
    while(*last!= ''){
    
        while(*last != ' ' && *last != '')last++;
        last--;
        reverse(first, last);
        last++;
        while(*last == ' ') last++;
        first = last;
    }

}

附录: 阿里2012年研发笔试题目http://blog.csdn.net/geekcoder/article/details/8058330

         阿里2013年五月实习生笔试题目http://50vip.com/blog.php?i=223

    阿里2013-5-19实习生招聘笔试题目 卷B

         阿里2014年笔试题目http://www.itmian4.com/forum.php?mod=viewthread&tid=3503&extra=page%3D1%26filter%3Dtypeid%26typeid%3D6%26typeid%3D6

原文地址:https://www.cnblogs.com/graph/p/3329840.html