所有字符串的公共前缀最长字符串

特点:(1)公共所有字符串前缀 (好像跟没说一样。。。)

(2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串

参考问题:https://leetcode.com/problems/longest-common-prefix/description/

  1. Write a function to find the longest common prefix string amongst an array of strings.
  2.  
  3. If there is no common prefix, return an empty string "".
  4.  
  5. Example 1:
  6.  
  7. Input: ["flower","flow","flight"]
  8. Output: "fl"
  9.  
  10. Example 2:
  11.  
  12. Input: ["dog","racecar","car"]
  13. Output: ""
  14. Explanation: There is no common prefix among the input strings.
  15.  
  16. Note:
  17.  
  18. All given inputs are in lowercase letters a-z.

题干容易理解,翻译略

实现步骤

1.构建字典树

当任意一字符串中的当前节点的branchCount等于输入的单词个数时候,那么这个节点就是在最长前缀里

C 语言解法:

  1. #define MAX 30 //the total number of alphabet is 26, a...z
  2.  
  3. struct DicTrie{
  4. bool isTerminal;//是否是单词结束标志
  5. int count; //当前字符串出现次数
  6. int branchCount; //计数当前节点的孩子数
  7. struct DicTrie *next[MAX ]; //每个节点 最多 有 MAX 个孩子节点 结构体嵌套
  8. };
  9.  
  10. int insertTrie(struct DicTrie *root ,char *targetString)
  11. {
  12. if (!targetString) {
  13. return 0;
  14. }
  15. int len = strlen(targetString);
  16. if (len <= 0) {
  17. return 0;
  18. }
  19. struct DicTrie *head = root;
  20. for (int i = 0; i < len; i ++) {
  21. int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
  22. if (head->next[res] == NULL) { //如果是空节点
  23. head->next[res] = (struct DicTrie *)malloc(sizeof(struct DicTrie));//new DicTrie;//则插入新节点元素
  24. head = head->next[res]; //更新头指针 并初始化
  25. head->count = 0; //
  26. for (int j = 0; j < MAX; j ++) {
  27. head->next[j] = NULL;
  28. head->isTerminal = false;
  29. }
  30. head->branchCount = 1;//一个分支
  31. } else {
  32. head = head->next[res];
  33. head->branchCount ++;//分支累计
  34. }
  35. }
  36. head->count ++;//每次插入一个,响应计数都增加1
  37. head->isTerminal = true;
  38. return head->count;
  39. }
  40.  
  41. char* longestCommonPrefix(char** strs, int strsSize) {
  42.  
  43. int len = strsSize;
  44. //边界处理
  45. if (len == 0) {
  46. return "";
  47. }
  48. if (len == 1) {
  49. return strs[0];
  50. }
  51. //组织字典树
  52. struct DicTrie *root = NULL;
  53. root = (struct DicTrie *)malloc(sizeof(struct DicTrie));
  54. root->count = 0;
  55. root->branchCount = 0;
  56. for (int i = 0; i < MAX; i ++) {
  57. root->next[i] = NULL; // 空节点
  58. root->isTerminal = false; //
  59. }
  60. //
  61. for (int i = 0;i < len; i ++) {
  62. insertTrie(root, strs[i]);
  63. }
  64. //
  65. int preIndex = 0;
  66.  
  67. struct DicTrie *head = root;
  68. bool isFlag = false;
  69. int i = 0;
  70. int count = strlen(strs[0]);//任意一字符串都可以 从strs[0]中查即可
  71. for (preIndex = 0; preIndex< count; preIndex ++) {
  72. int targetIndex = strs[0][preIndex] - 'a';
  73. head = head->next[targetIndex];
  74. if (head->branchCount == len) {
  75. i ++;//拿到合法前缀的计数
  76. isFlag = true;
  77. }
  78. }
  79. if (isFlag) {
  80. preIndex = i;
  81. } else {
  82. preIndex = 0;
  83. }
  84. strs[0][preIndex] = '\0';
  85. return strs[0];
  86. }

自己编辑时候的主函数:

  1. int main(int argc, const char * argv[]) {
  2. // insert code here...
  3. char *s[30]= {"dog","dracecar","dcar"};
  4. // char *s[30]= {"flower","flow","flight"};
  5. char *str = longestCommonPrefix(s,3);
  6. printf("%s",str);
  7. return 0;
  8. }

其实,我这道题在思路上没有任何问题,工作用的都是面向对象语言,面向过程C,纯C少了,所以代码不符合提交要求

比如创建结构体 我自己写 就是new DicTrie,但是纯C种 用malloc.

还有字符串截取。。。都得自己一点点面向过程敲。不然就是运行错误。

最后

(1)解这道题的目的:

(2)拓宽思路后缀数组:

(3)这道题的高效解法:

该休息了,剩下的后续补充

LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串的更多相关文章

  1. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  2. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  3. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  4. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  5. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. [LeetCode] 14. Longest Common Prefix ☆

    Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...

  8. [LeetCode]14. Longest Common Prefix最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  9. LeetCode——14. Longest Common Prefix

    一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...

随机推荐

  1. Google glog error LNK2001: unresolved external symbol "__declspec(dllimport) int fLI::FLAGS_XXXX 错误的解决。

    想在 windows 下使用 glog,使用类似 FLAGS_max_log_size 来设置参数,结果编译报错. 解决办法是在 项目属性 -> C/C++ -> Preprocessor ...

  2. 学习抓包之如何用Charles实现“刷楼”

    为了获取一些网络中的数据,我们需要掌握抓包技术. Charles是一个 HTTP 代理服务器, HTTP 监视器,反转代理服务器.它允许一个开发者查看所有连接互联网的 HTTP 通信.这些包括Requ ...

  3. Ubuntu安装Sun JDK

    Ubuntu 14.04 下安装 Sun JDK 1.8.0 1.下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...

  4. linux上安装python3同时保留python2

    linux上安装python3同时保留python2?这个就要用到上篇说到的path变量了. 具体介绍及操作 这里我下载python3.6版本来进行介绍 django默认数据库为sqlite3,所以安 ...

  5. What is the difference between application server and web server?

    http://stackoverflow.com/questions/936197/what-is-the-difference-between-application-server-and-web- ...

  6. 【转】Web前端开发:为何选择MVVM而非MVC

    在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的技术原因,MVC在Web前端开发中根本无法使用(对的,是无法,而不是不该) 在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的 ...

  7. Hibernate每个子类一张表(使用注释)实例

    在每个子类一张表的情况下,表是根据持久类创建的,但是它们使用主键和外键来重新定义. 所以关系中不会有重复的列. 我们需要在子类中的使用@PrimaryKeyJoinColumn注释和在父类指定@Inh ...

  8. JavaScript 对大小写敏感。

    JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getElementbyID 是不同的. 同样,变 ...

  9. Linux下mongodb安装及数据导入导出教程

    Linux下mongodb安装及数据导入导出教程 #查看linux发行版本 cat /etc/issue #查看linux内核版本号 uname -r 一.Linux下mongodb安装的一般步骤 1 ...

  10. Unity3D学习笔记——Rigdbody刚体组件

    Rigdbody刚体组件:必须和碰撞体(Colliders)一起使用,否则会发生穿过的现象.碰撞体(Colliders)不是必须和刚体一起使用. 刚体的作用:使游戏物体能获得重力,接受外界的受力和扭力 ...