0
点赞
收藏
分享

微信扫一扫

力扣面试题(八)

洒在心头的阳光 2024-08-03 阅读 19

1、独一无二出现的次数

思路:先将数组从小到大排序,因为数字相同的数据排在一起,方便统计。然后统计各个数字出现的次数 ,再次排序,查看相邻是否一样,如果不一样,返回true.

int compare(const void * a, const void *b)
{
    return (*(int *)a - *(int *)b);
}

bool uniqueOccurrences(int* arr, int arrSize) {    
    int arr_tmp[arrSize] = {};
    int count = 1;
    int j = 0;
    int i = 0;

    qsort(arr, arrSize, sizeof(int), compare);
    for(i = 1; i < arrSize; i++){
        if(arr[i-1] == arr[i]){
            count++;
        }
        else{
            arr_tmp[j] = count;
            j++;
            count = 1;
        }
    }    
    arr_tmp[j++] = count;
    qsort(arr_tmp, j, sizeof(int), compare);
    for(i = 0; i < j-1; i++){     
        if(arr_tmp[i] == arr_tmp[i+1]){
            return false;
        }
    }

    return true;
}

2、确定两个字符串是否接近

思路:两个字符串相似有以下特征:1、字符串子串都一样,只不过字母的个数不一致;2、两个字符串中也许每个字符出现的次数不一样 ,但是总体上出现的次数应该是一模一样的,什么意思就是比如word1 a b c出现的次数是1,2,3;word2 a b c出现的次数是2,3,1,但是总体来说出现的次数是1,2,3这样才能相互转化,如果不一样就会有问题。

int compareChar(const void *a, const void *b) {
    return (*(char *)a - *(char *)b);
}

int compareInt(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

bool closeStrings(char* word1, char* word2) {
    int len1 = strlen(word1);
    int len2 = strlen(word2);
    int count = 1;
    int j = 1;
    int i = 0;
    int * sameCount1 = malloc(len1*sizeof(int));
    int * sameCount2 = malloc(len2*sizeof(int));
    int count1_len = 0;
    int count2_len = 0;
    bool close = false;

    if(len1 != len2){
        close =  false;
        goto label;
    }    

    qsort(word1, len1, sizeof(char), compareChar);
    qsort(word2, len2, sizeof(char), compareChar);

    for(i = 1; i < len1; i++){
        if(word1[i] == word1[i-1]){
            count++;
        }
        else{
            sameCount1[count1_len++] = count;
            word1[j++] = word1[i];   
            count =1 ;         
        }
    }
    sameCount1[count1_len++] = count;

    if(j < len1){
        word1[j] = '\0';
    }    

    j = 1;
    count = 1;
    for(i = 1; i < len1; i++){
        if(word2[i] == word2[i-1]){
            count++;
        }
        else{
            sameCount2[count2_len++] = count;
            word2[j++] = word2[i]; 
            count = 1;           
        }
    }

    sameCount2[count2_len++] = count;

    if(j < len2){
        word2[j] = '\0';
    }

    if(strcmp(word1, word2)){
        close =  false;
        goto label;
    }

    qsort(sameCount1, count1_len, sizeof(int), compareInt);
    qsort(sameCount2, count2_len, sizeof(int), compareInt);
    
    if(count1_len != count2_len){
        close = false;
        goto label;    
    }

    for(j = 0; j < count1_len; j++){
        if(sameCount1[j] != sameCount2[j]){
            close = false;
            goto label;
        }
    }

    close = true;
label:    
    free(sameCount1);
    free(sameCount2);

    return close;
}

3、相等行队列

 

bool equalElement(int ** grid, int line, int col,int gridSize)
{
    for(int i = 0 ; i < gridSize; i++){
        if(grid[line][i] != grid[i][col]){
            return false;
        }
    }

    return true;
}

int equalPairs(int** grid, int gridSize, int* gridColSize) {
    int res = 0;
    for(int line = 0; line < gridSize; line++){
        for(int col = 0; col < gridSize; col++){
            if(true == equalElement(grid, line, col, gridSize)){
                res++;
            }
        }
    }

    return res;
}
举报

相关推荐

0 条评论