zoukankan      html  css  js  c++  java
  • LeetCode——最长公共前缀

    Q:编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。

    示例 1:
    输入: ["flower","flow","flight"]
    输出: "fl"

    示例 2:
    输入: ["dog","racecar","car"]
    输出: ""
    解释: 输入不存在公共前缀。

    A:
    最简单的方法:

    public static String longestCommonPrefix(String[] strs) {
            if (strs.length == 0)
                return "";
            if (strs.length == 1)
                return strs[0];
            int minLen = Integer.MAX_VALUE;
            for (String s : strs) {
                int len = s.length();
                minLen = Math.min(minLen, len);
            }
            String sub = "";
            boolean flag = false;
            for (int i = 0; i < minLen; i++) {
                char c = strs[0].charAt(i);
                for (String str : strs) {
                    if (str.charAt(i) != c) {
                        flag = true;
                        sub = str.substring(0, i);
                    }
                }
                if (flag)
                    break;
            }
            if (!flag) {
                sub = strs[0].substring(0, minLen);
            }
            return sub;
        }
    

    对字符串数组进行排序,然后只要比较首尾两个字符串即可

        public String longestCommonPrefix(String[] strs) {
            StringBuilder result = new StringBuilder();
             
            if (strs!= null && strs.length > 0){
             
                Arrays.sort(strs);
                 
                char [] a = strs[0].toCharArray();
                char [] b = strs[strs.length-1].toCharArray();
                 
                for (int i = 0; i < a.length; i ++){
                    if (b.length > i && b[i] == a[i]){
                        result.append(b[i]);
                    }
                    else {
                        return result.toString();
                    }
                }
            return result.toString();
        }
    

    分治法:

        public String longestCommonPrefix(String[] strs) {
            if (strs == null || strs.length == 0)
                return "";
            return longestCommon(strs, 0, strs.length - 1);
        }
    
        private String longestCommon(String[] strs, int start, int end) {
            if (start == end)
                return strs[start];
            int mid = (start + end) / 2;
            String left = longestCommon(strs, start, mid);
            String right = longestCommon(strs, mid + 1, end);
            return commonPrefix(left, right);
        }
    
        private String commonPrefix(String left, String right) {//这里也可以用二分查找
            int minLen = Math.min(left.length(), right.length());
            for (int i = 0; i < minLen; i++) {
                if (left.charAt(i) != right.charAt(i))
                    return left.substring(0, i);
            }
            return left.substring(0, minLen);
        }
    

    还有使用字典树(直接借用了别人的,链接:https://leetcode-cn.com/problems/longest-common-prefix/solution/zui-chang-gong-gong-qian-zhui-by-leetcode/)

    public String longestCommonPrefix(String q, String[] strs) {
        if (strs == null || strs.length == 0)
             return "";  
        if (strs.length == 1)
             return strs[0];
        Trie trie = new Trie();      
        for (int i = 1; i < strs.length ; i++) {
            trie.insert(strs[i]);
        }
        return trie.searchLongestPrefix(q);
    }
    
    class TrieNode {
    
        // 子节点的链接数组
        private TrieNode[] links;
    
        private final int R = 26;
    
        private boolean isEnd;
    
        // 非空子节点的数量
        private int size;    
        public void put(char ch, TrieNode node) {
            links[ch -'a'] = node;
            size++;
        }
    
        public int getLinks() {
            return size;
        }
        // 假设方法 containsKey、isEnd、get、put 都已经实现了
        // 可以参考文章:https://leetcode.com/articles/implement-trie-prefix-tree/
    }
    
    public class Trie {
    
        private TrieNode root;
    
        public Trie() {
            root = new TrieNode();
        }
    
    // 假设方法 insert、search、searchPrefix 都已经实现了
    // 可以参考文章:https://leetcode.com/articles/implement-trie-prefix-tree/
        private String searchLongestPrefix(String word) {
            TrieNode node = root;
            StringBuilder prefix = new StringBuilder();
            for (int i = 0; i < word.length(); i++) {
                char curLetter = word.charAt(i);
                if (node.containsKey(curLetter) && (node.getLinks() == 1) && (!node.isEnd())) {
                    prefix.append(curLetter);
                    node = node.get(curLetter);
                }
                else
                    return prefix.toString();
    
             }
             return prefix.toString();
        }
    }
    
    
  • 相关阅读:
    BERT基础知识
    TorchText使用教程
    Pytorch-中文文本分类
    预处理算法_5_数据集划分
    预处理算法_4_表堆叠
    预处理算法_3_新增序列
    预处理算法_2_类型转换
    预处理算法_1_表连接
    爬取网站所有目录文件
    如何将Docker升级到最新版本
  • 原文地址:https://www.cnblogs.com/xym4869/p/12652111.html
Copyright © 2011-2022 走看看