博客
关于我
【LeetCode】[14] 最长公共前缀
阅读量:105 次
发布时间:2019-02-26

本文共 2638 字,大约阅读时间需要 8 分钟。

/* * @lc app=leetcode.cn id=14 lang=java * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-common-prefix/description/ * * algorithms * Easy (31.53%) * Total Accepted:    49.1K * Total Submissions: 155K * Testcase Example:  '["flower","flow","flight"]' * * 编写一个函数来查找字符串数组中的最长公共前缀。 *  * 如果不存在公共前缀,返回空字符串 ""。 *  * 示例 1: *  * 输入: ["flower","flow","flight"] * 输出: "fl" *  *  * 示例 2: *  * 输入: ["dog","racecar","car"] * 输出: "" * 解释: 输入不存在公共前缀。 *  *  * 说明: *  * 所有输入只包含小写字母 a-z 。 *  */class Solution {       public String longestCommonPrefix(String[] strs) {           StringBuffer longestCommonString = new StringBuffer("");        if (strs.length > 0) {                 for (String word : strs) {                   if (word == "") {                       return longestCommonString.toString();                }            }            int n_min = strs[0].length();            char cTemp;            for (String word : strs) {                   if (n_min > word.length())  n_min = word.length();            }            for (int n = 0; n < n_min; n++) {                   cTemp = strs[0].toCharArray()[n];                for (int i = 1; i < strs.length; i++) {                       if (cTemp == strs[i].toCharArray()[n])                        continue;                    else                        return longestCommonString.toString();                }                longestCommonString.append(cTemp);            }             }        return longestCommonString.toString();    }      }

大概有这五种思路, 一般都会采用第四种, 但是耗时太多

  • 1、所求的最长公共前缀子串一定是每个字符串的前缀子串。所以随便选择一个字符串作为标准,把它的前缀串,与其他所有字符串进行判断,看是否是它们所有人的前缀子串。这里的时间性能是O(mnm)。

  • 2、列出所有的字符串的前缀子串,将它们合并后排序,找出其中个数为n且最长的子串。时间性能为O(nm+mnlog(mn))

  • 3、**纵向扫描:**从下标0开始,判断每一个字符串的下标0,判断是否全部相同。直到遇到不全部相同的下标。时间性能为O(n*m)。

  • 4、**横向扫描:**前两个字符串找公共子串,将其结果和第三个字符串找公共子串……直到最后一个串。时间性能为O(n*m)。

  • 5、借助trie字典树。将这些字符串存储到trie树中。那么trie树的第一个分叉口之前的单分支树的就是所求。

java参考代码

class Solution {       public String longestCommonPrefix(String[] strs) {        if (strs.length == 1){               return strs[0];        }        StringBuilder sb = new StringBuilder();        if (strs.length>1) {               int len = strs[0].length();            for (int i = 0; i < len; i++) {                   char curr = strs[0].charAt(i);                for (int j = 1; j < strs.length; j++) {                       if (strs[j].length()<=i ||strs[j].charAt(i) != curr) {                           return sb.toString();                    }                    if (strs[j].charAt(i) == curr && j == strs.length - 1) {                           sb.append(curr);                    }                }            }        }       return sb.toString();    }}

转载地址:http://asik.baihongyu.com/

你可能感兴趣的文章
mysql 协议的退出命令包及解析
查看>>
mysql 参数 innodb_flush_log_at_trx_commit
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
MySQL 命令和内置函数
查看>>
MySQL 和 PostgreSQL,我到底选择哪个?
查看>>
mysql 四种存储引擎
查看>>
MySQL 在并发场景下的问题及解决思路
查看>>
MySQL 在控制台插入数据时,中文乱码问题的解决
查看>>
MySQL 基础架构
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 处理插入重主键唯一键重复值办法
查看>>
MySQL 备份 Xtrabackup
查看>>
mysql 复杂查询_mysql中复杂查询
查看>>
mYSQL 外键约束
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>