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){return "";}
 4     if(strs.length==1){return strs[0];}
 5     int len=strs.length;
 6     int index=0;
 7     boolean flag=false;
 8         for(int i=0;i<strs[0].length();i++){
 9             char x=strs[0].charAt(i);
10             for(int j=1;j<len;j++){
11             if(i>=strs[j].length()){flag=true;index=i;break;}
12             char y=strs[j].charAt(i);
13             if(x!=y){flag=true;index=i;break;}
14             }
15             if(flag){break;}
16             if(i==strs[0].length()-1){index=strs[0].length();}
17         }
18         String ans=strs[0].substring(0,index);
19         return ans;
20     }
21     public static void main(String[]args){
22     String[]strs=new String[2];
23     strs[0]="a";
24     strs[1]="a";
25     System.out.println(longestCommonPrefix(strs));
26     }
27 }
时间: 2024-10-13 00:30:20

Longest Common Prefix的相关文章

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.关

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.思路: 开始想的是数组中每两个相邻字符串进行比较,并取比较的最短的前缀作为整个数组的最短前缀.时间复杂度为

14. 字符串数组的最长公共前缀 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. public class Solution { public string LongestCommonPrefix(string[] strs) { StringBuilder result = new StringBuilder(); if (strs != null && strs.Length > 0) {

LeetCode 14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 让我们在一个字符串中找出所有字符串的共同前缀 class Solution { public: string longestCommonPrefix(vector<string>& strs) { string result = ""; if (!strs.size())return resul

leetCode 14. Longest Common Prefix 字符串

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 题目大意:求一组字符串的最长前缀. 代码如下: class Solution { public:     string longestCommonPrefix(vector<string>& strs) {         if(strs.size() == 0)

LeetCode14 Longest Common Prefix

题意: Write a function to find the longest common prefix string amongst an array of strings. (Easy) 这两天实验室项目太忙了, 老板各种活,只能挑着先水几道easy题,这两个题是昨天做的没来得及写总结. 分析: 暴力的想法遍历比较一下就行,注意遍历的始末位置.优化的话改天再看看discuss 代码: 1 class Solution { 2 public: 3 string longestCommonP

LeetCode #14 Longest Common Prefix (E)

[Problem] Write a function to find the longest common prefix string amongst an array of strings. [Analysis] 思路非常简单,循环验证每一个字符串就可以通过OJ,代码也没有优化. [Solution] public class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0)

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 @ Python

Source: https://oj.leetcode.com/problems/longest-common-prefix/ Write a function to find the longest common prefix string amongst an array of strings. Hint:   strs=['abc','ab','a'] strs=[ 'abc', 'ab', 'a', ] We can think of strs as a column length va