LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
所有字符串的公共前綴最長字符串
特點:(1)公共所有字符串前綴 (好像跟沒說一樣。。。)
? ? ? ? ?(2)在字典樹中特點:任意從根節點觸發遇見第一個分支為止的字符集合即為目標串
參考問題:https://leetcode.com/problems/longest-common-prefix/description/
Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".Example 1:Input: ["flower","flow","flight"] Output: "fl"Example 2:Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.Note:All given inputs are in lowercase letters a-z.題干容易理解,翻譯略
實現步驟
1.構建字典樹
當任意一字符串中的當前節點的branchCount等于輸入的單詞個數時候,那么這個節點就是在最長前綴里
C 語言解法:
#define MAX 30 //the total number of alphabet is 26, a...z struct DicTrie{bool isTerminal;//是否是單詞結束標志int count; //當前字符串出現次數int branchCount; //計數當前節點的孩子數struct DicTrie *next[MAX ]; //每個節點 最多 有 MAX 個孩子節點 結構體嵌套 };int insertTrie(struct DicTrie *root ,char *targetString) {if (!targetString) {return 0;}int len = strlen(targetString);if (len <= 0) {return 0;}struct DicTrie *head = root;for (int i = 0; i < len; i ++) {int res = (int)(targetString[i] - 'a');//當前小寫字母對應數字if (head->next[res] == NULL) { //如果是空節點head->next[res] = (struct DicTrie *)malloc(sizeof(struct DicTrie));//new DicTrie;//則插入新節點元素head = head->next[res]; //更新頭指針 并初始化head->count = 0; // for (int j = 0; j < MAX; j ++) {head->next[j] = NULL;head->isTerminal = false;}head->branchCount = 1;//一個分支} else {head = head->next[res];head->branchCount ++;//分支累計 }}head->count ++;//每次插入一個,響應計數都增加1head->isTerminal = true;return head->count; }char* longestCommonPrefix(char** strs, int strsSize) {int len = strsSize;//邊界處理if (len == 0) {return "";}if (len == 1) {return strs[0];}//組織字典樹struct DicTrie *root = NULL;root = (struct DicTrie *)malloc(sizeof(struct DicTrie));root->count = 0;root->branchCount = 0;for (int i = 0; i < MAX; i ++) {root->next[i] = NULL; // 空節點root->isTerminal = false; // }// for (int i = 0;i < len; i ++) {insertTrie(root, strs[i]);}// int preIndex = 0;struct DicTrie *head = root;bool isFlag = false;int i = 0;int count = strlen(strs[0]);//任意一字符串都可以 從strs[0]中查即可for (preIndex = 0; preIndex< count; preIndex ++) {int targetIndex = strs[0][preIndex] - 'a';head = head->next[targetIndex];if (head->branchCount == len) {i ++;//拿到合法前綴的計數isFlag = true;}}if (isFlag) {preIndex = i;} else {preIndex = 0;}strs[0][preIndex] = '\0';return strs[0]; }自己編輯時候的主函數:
int main(int argc, const char * argv[]) {// insert code here...char *s[30]= {"dog","dracecar","dcar"};// char *s[30]= {"flower","flow","flight"};char *str = longestCommonPrefix(s,3);printf("%s",str);return 0; }?
其實,我這道題在思路上沒有任何問題,工作用的都是面向對象語言,面向過程C,純C少了,所以代碼不符合提交要求
比如創建結構體 我自己寫 就是new DicTrie,但是純C種 用malloc.
還有字符串截取。。。都得自己一點點面向過程敲。不然就是運行錯誤。
?
最后
(1)解這道題的目的:
(2)拓寬思路后綴數組:
(3)這道題的高效解法:
該休息了,剩下的后續補充
?
轉載于:https://www.cnblogs.com/someonelikeyou/p/9065328.html
總結
以上是生活随笔為你收集整理的LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android的Intent和Inten
- 下一篇: Cocos2dx-demo演示项目:Pa