leetcode524. 通过删除字母匹配到字典里最长单词

àì夳堔傛蜴生んèń 2022-09-11 08:23 202阅读 0赞

在这里插入图片描述
思路

  1. 字符串匹配,暴力匹配就完事了
  2. 注意匹配和不匹配的数字

    int cmp (const void a,const void b){

    1. char *c = *(char**)a;
    2. char *d = *(char**)b;
    3. if(strlen(c) != strlen(d))
    4. return strlen(d)-strlen(c);
    5. return strcmp(c,d);

    }

    char findLongestWord(char s, char ** dictionary, int dictionarySize){

    1. int i=0;
    2. int j=0;
    3. int k=0;
    4. int count =0 ;
    5. int len = strlen(s);
    6. //printf("%d\n",dictionarySize);
    7. char **re = (char **)malloc(sizeof(char*)*dictionarySize);
    8. for(i = 0; i < dictionarySize; i++) {
    9. char *res = (char*)malloc(sizeof(char)*1001);
    10. re[i] = res;
    11. }
    12. for(i = 0; i < dictionarySize; i++){
    13. k = 0;
    14. int len1 = strlen(dictionary[i]);
    15. for(j = 0; j < len1; ) {
    16. while(k < len) {
    17. if(dictionary[i][j]==s[k]) {
    18. k++;
    19. j++;
    20. } else {
    21. k++;
    22. }
    23. if(j==strlen(dictionary[i])) {
    24. strcpy(re[count],dictionary[i]);
    25. count++;
    26. break;
    27. }
    28. }
    29. if(k==len)//到头也要break
    30. break;
    31. }
    32. }
    33. if(count==0)
    34. return "";
    35. qsort(re,count,sizeof(re[0]),cmp);
    36. return re[0];

    }

发表评论

表情:
评论列表 (有 0 条评论,202人围观)

还没有评论,来说两句吧...

相关阅读