• C语言 | Leetcode C语言题解之第187题重复的DNA序列


    题目:

    题解:

    1. #define MAXSIZE 769/* 选取一个质数即可 */
    2. typedef struct Node
    3. {
    4. char string[101];
    5. int index;
    6. struct Node *next; //保存链表表头
    7. } List;
    8. typedef struct
    9. {
    10. List *hashHead[MAXSIZE];//定义哈希数组的大小
    11. } MyHashMap;
    12. List * isInHash(List *list,char * stringKey)
    13. {
    14. List *nodeIt = list;
    15. //通过链表下遍历
    16. while (nodeIt != NULL)
    17. {
    18. if (strcmp(stringKey, nodeIt->string)== 0 )
    19. {
    20. return nodeIt;
    21. }
    22. nodeIt = nodeIt->next;
    23. }
    24. return NULL;
    25. }
    26. MyHashMap* myHashMapCreate()
    27. {
    28. int i;
    29. MyHashMap* newHash= (MyHashMap* )malloc(sizeof(MyHashMap));
    30. /* 对链表的头结点赋初值 */
    31. for (i = 0; i < MAXSIZE; i++)
    32. {
    33. newHash->hashHead[i] = NULL;
    34. }
    35. return newHash;
    36. }
    37. int myHashID(char * str)
    38. {
    39. long h = 0;
    40. for(int i = 0; i < strlen(str); i++)
    41. {
    42. h = (h * 26 % MAXSIZE + str[i] - 'A') % MAXSIZE;
    43. // 字符串的hashcode, 权为26是因为小写字母,不限制时为128,这样能够让结点尽可能分布均匀,减少地址冲突
    44. // 取模是为了防止int型溢出
    45. }
    46. return h % MAXSIZE;
    47. }
    48. void myHashMapPut(MyHashMap* obj, char* stringKey,int index)
    49. {
    50. //一定不再这里面
    51. List * it= isInHash(obj->hashHead[myHashID(stringKey)],stringKey);
    52. if(it != NULL)
    53. {
    54. //在表里面更新键值
    55. it->index = index;
    56. }
    57. else
    58. {
    59. //不在表里面
    60. List *newNode = (List*)malloc(sizeof(List));
    61. strcpy(newNode->string , stringKey);
    62. newNode->next = NULL;
    63. newNode->index = index;
    64. if(obj->hashHead[myHashID(stringKey)] != NULL)
    65. {
    66. //当前头链表不为空,则需要将后续的链表接上
    67. //需要主要这里表头也代表一个数据的值
    68. newNode->next = obj->hashHead[myHashID(stringKey)];
    69. }
    70. //修改头链表
    71. obj->hashHead[myHashID(stringKey)] = newNode;
    72. }
    73. }
    74. int myHashMapGet(MyHashMap* obj, char* stringKey)
    75. {
    76. List * it= isInHash(obj->hashHead[myHashID(stringKey)],stringKey);
    77. if( it!= NULL)
    78. {
    79. return it->index;
    80. }
    81. return -1;
    82. }
    83. void myHashMapFree(MyHashMap* obj)
    84. {
    85. int i;
    86. List *freeIt;
    87. List *curIt;
    88. for (i = 0; i < MAXSIZE; i++)
    89. {
    90. if(obj->hashHead[i] != NULL)
    91. {
    92. freeIt = NULL;
    93. curIt = obj->hashHead[i];
    94. while(curIt != NULL)
    95. {
    96. freeIt = curIt;
    97. curIt= curIt->next;
    98. free(freeIt);
    99. }
    100. obj->hashHead[i]= NULL;
    101. }
    102. }
    103. free(obj);
    104. }
    105. char ** findRepeatedDnaSequences(char * s, int* returnSize)
    106. {
    107. char* stringKey = (char * )malloc(sizeof(char ) * 11);
    108. int len = strlen(s);
    109. if(len < 10)
    110. {
    111. *returnSize = 0;
    112. return NULL;
    113. }
    114. MyHashMap * hsahMap = myHashMapCreate();
    115. int maxCount = 0;
    116. for(int i = 10; i <= len; i++)
    117. {
    118. memcpy(stringKey, &s[i-10], 10);
    119. stringKey[10] = '\0';
    120. int count = myHashMapGet(hsahMap, stringKey);
    121. if(count == -1)
    122. {
    123. count = 1;
    124. }
    125. else
    126. {
    127. count++;
    128. }
    129. myHashMapPut(hsahMap,stringKey,count);
    130. maxCount = fmax(maxCount, count);
    131. }
    132. if(maxCount < 2)
    133. {
    134. *returnSize = 0;
    135. return NULL;
    136. }
    137. *returnSize = 0;
    138. char ** returnMatStr = (char ** )malloc(sizeof(char *) * len);
    139. int i;
    140. List *freeIt;
    141. List *curIt;
    142. for (i = 0; i < MAXSIZE; i++)
    143. {
    144. if(hsahMap->hashHead[i] != NULL)
    145. {
    146. freeIt = NULL;
    147. curIt = hsahMap->hashHead[i];
    148. while(curIt != NULL)
    149. {
    150. freeIt = curIt;
    151. curIt= curIt->next;
    152. if(freeIt->index == maxCount)
    153. {
    154. returnMatStr[*returnSize] = (char * )malloc(sizeof(char ) * 11);
    155. strcpy(returnMatStr[*returnSize], freeIt->string);
    156. *returnSize = *returnSize + 1;
    157. }
    158. free(freeIt);
    159. }
    160. hsahMap->hashHead[i]= NULL;
    161. }
    162. }
    163. free(hsahMap);
    164. return returnMatStr;
    165. }
  • 相关阅读:
    智源AI日报(2022-09-01):吴恩达:中国的丰厚AI投资回报将在接下来十年显现
    AES和Rijndael的区别
    FASTAPI的简单理解
    Django建立mysql数据库,和使用navicat连接mysql数据库
    EBI、DDD及其演变架构史
    目录遍历漏洞
    教你如何制作浪漫的3D相册表白网站 HTML+CSS+JavaScript
    五分钟k8s实战-Istio 网关
    cnpm的版本锁定问题的解决方案
    获取两个字符串的最大公共子序列(LCS)
  • 原文地址:https://blog.csdn.net/m0_59237910/article/details/139910758