本文共 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/