title: leetcode-14-最长公共前缀
date: 2019-09-02 08:45:48
categories:
- leetcode
tags: - leetcode
最长公共前缀
-
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
示例 1:
输入: [“flower”,“flow”,“flight”]
输出: “fl”
示例 2:输入: [“dog”,“racecar”,“car”]
输出: “”
解释: 输入不存在公共前缀。
说明:所有输入只包含小写字母 a-z 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-common-prefix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -
解法一:使用java内置函数,时间复杂度n(n是这个字符串数组中所有的字符总和),就是扫描一遍
class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0||strs[0]=="")//判断条件 return ""; String prifix = strs[0]; for (int i=0;i<strs.length;i++){ while (strs[i].indexOf(prifix)!=0){ prifix = prifix.substring(0,prifix.length()-1);//每次从末尾删除一 if (prifix.isEmpty()) return ""; } } return prifix; } }
解释代码:每次不成功就会把尾巴中的最后一位字符删除
-
解法二:快排思想的利用—分治
class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0||strs[0]=="")//判断条件 return ""; return longestCommon(strs,0,strs.length-1); } public String longestCommon(String[] strs,int left,int right){ if(left==right) return strs[left]; else { int mid = (left+right)/2; String lcpLeft = longestCommon(strs,left,mid); String lcpRight = longestCommon(strs,mid+1,right); return CommonProfix(lcpLeft,lcpRight); } } public String CommonProfix(String left,String right){ int len = Math.min(left.length(),right.length()); int i=0; for (;i<len;i++){ if (left.charAt(i)!=right.charAt(i)) break; } return left.substring(0,i); } }
自己创建工具方法,利用工具方法达到二分的目的
-
解法三:我的前缀树设计的有点问题,大家可以拿着改进一下,我的前缀树运行不成功,应为我会覆盖,当初设计前缀树没考虑问题,大家可参考设计,应该很快能出来
public class solution { public static String longestCommonPrefix(String[] strs) { if (strs.length ==0) return ""; else if (strs.length == 1) return strs[0]; else { Trie trie = new Trie(); int i; for (i=0;i<strs.length;i++){ trie.add(strs[i]); } for (i=0;i<strs[0].length();i++){ if (trie.isPrefix(strs[0].substring(0,i+1))==false) return strs[0].substring(0,i); } return i==strs[0].length()?strs[0]:""; } } static class Trie { private class Node { public boolean isWord; public TreeMap<Character, Node> next; public Node(boolean isWord) { this.isWord = isWord; next = new TreeMap<>(); } public Node() { this(false); } } private Node root; private int size; public Trie() { root = new Node(); size = 0; } //获得树中单词数量 public int getSize() { return size; } //向树中添加一个新的单词word public void add(String word) { Node cur = root; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); if (cur.next.get(c) == null) { cur.next.put(c, new Node()); } cur = cur.next.get(c); } if (!cur.isWord) { cur.isWord = true; size++; } } // //查询单词是否存在在树中 // public boolean contains(String word) { // Node cur = root; // for (int i = 0; i < word.length(); i++) { // char c = word.charAt(i); // if (cur.next.get(c) == null) // return false; // cur = cur.next.get(c); // } // return cur.isWord; // } //前缀搜索 public boolean isPrefix(String prefix) { Node cur = root; for (int i = 0; i < prefix.length(); i++) { char c = prefix.charAt(i); if (cur.next.get(c) == null||cur.next.size()!=1) return false; cur = cur.next.get(c); } return true; } } }