Longest Common Prefix (算法)

找出一个字符串数字里公共的开头字符串,没有的话返回""。自己做的时候想复杂了,其实直接比较久ok的。

提交到网上的版本是Vertical Comparasion的(比较不同字符串相同位置的字符)。在此记录一下Horizonal Comparision的方法。

还有更多更详细的解释在这里

水平比较

 1     public String longestCommonPrefix(String[] strs) {
 2         if (strs.length == 0) return "";
 3         String prefix = strs[0];
 4         for (int i = 1; i < strs.length; i++){
 5             System.out.println(prefix+","+strs[i].indexOf(prefix));
 6             while (strs[i].indexOf(prefix) != 0) { //不存在的时候是-1
 7                 prefix = prefix.substring(0, prefix.length() - 1);
 8                 System.out.println(prefix);
 9                 if (prefix.isEmpty()) return "";
10             }
11         }
12         return prefix;
13     }
时间: 2024-08-01 12:01:17

Longest Common Prefix (算法)的相关文章

[LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现

Q: Write a function to find the longest common prefix string amongst an array of strings. A: 这题的大概意思就是说给你一组字符串找出其中最长的哪个通用的前缀出来.这个东西不难找,但是如何找的又快又好不简单.其实这题本来就是easy题,但是却让我联想到了<数据结构与算法分析>上的一道题目,那道题目是这样的: 给一个8900个字的字典,从中间找出类似abc.bbc.abb这样单词中只有一个字母不同的单词进行

【LeetCode算法】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Exa

LeetCode之LCP(Longest Common Prefix)问题

这个也是简单题目,但是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an array of strings. 题意是给一个字符串数组,找出这个字符串数组中所有字符串的最长公共前缀. 注意是限定的前缀,而不是子串. 所以,直接的解法就是以第一个字符串为基准,逐个比较每个字符.算法复杂度也显然是O(M*N),M是字符串的个数,N是最长前缀的长度. 代码如下: class So

[LeetCode] Longest Common Prefix 字符串公有前序

Write a function to find the longest common prefix string amongst an array of strings. Hide Tags String 这是一道很简单的题目,判断输入的多个字符串的公有前序,简单的逻辑遍历查找就好. 算法流程: 判断输入的字符串数量,小于2时候做出相应返回. 获取最短字符串的长度. 设定标记flag,控制跳出循环,与长度返回的长度len. 在最短字符串长度的范围内循环. 循环中每次遍历全部字符串len 位的字

【Leetcode】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目: Write a function to find the longest common prefix string amongst an array of strings. 算法: [java] view plain copy public String longestCommonPrefix(String[] strs) { if (strs.length == 0) {

LeetCode14——Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题目大意 写一个函数来找出所有字符串里最长的公共前缀. 难度系数:容易 实现 题目不难,基本思路大家都能想到,就是一些细节可能会遗漏.这个也没啥好算法,不管怎样,都需要一个个去比较. 所以没啥好说的,上代码: string longestCommonPrefix(std::vector<string> &strs)

LeetCodeOJ. Longest Common Prefix

试题请參见: https://oj.leetcode.com/problems/longest-common-prefix/ 题目概述 Write a function to find the longest common prefix string amongst an array of strings. 解题思路 这也是比較简单的算法提, 仅仅需比較比較数组中每一个字符串的第i位就可以, 直至不匹配为止. 记录此时i的值, 则为最长公共前缀. 源码 class Solution { publ

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个问题是关于如何寻找字符串数组的公共前缀的,从第一个字母开始,只要共有的都要输出. 思路: 1.首先,如何得到字符串共有前缀,既然共有,那么很容易先想到最短字符串. 2.最短字符串和其余进行比较,这样就省下了大于最短字符串长度的比较. 3.关于字符串的操作,length和null是很必要的判断输入操作,千万别忘记! 4.关

Longest Common Prefix

https://leetcode.com/problems/longest-common-prefix/ Write a function to find the longest common prefix string amongst an array of strings 1 public class Solution { 2 public static String longestCommonPrefix(String[] strs) { 3 if(strs.length==0){retu

14. Longest Common Prefix (最长公共前缀)

一.Description: Write a function to find the longest common prefix string amongst an array of strings. public class Solution { public String longestCommonPrefix(String[] strs) { } } 二.Solutions: 1.思路: 开始想的是数组中每两个相邻字符串进行比较,并取比较的最短的前缀作为整个数组的最短前缀.时间复杂度为