leetcode_Longest Common Prefix

描述:

Write a function to find the longest common prefix string amongst an array of strings.

思路:

有两种思路:

1.从0向最大的公共前缀长度进行,i=0,即每次从0循环至strs.length,所有的字符都相等,则count++,直至有一个字符不相同为止,循环终止

2.假设 longest common prefix 等于字符串数组的最短字符串的长度,从0循环至strs.length,在前面最长公共最大长度的基础上比较相邻两个串的最大公共子串

个人感觉还是第一种思路更好,用到的额外存储空间更少,时间也更短

代码:

思路1:从0向最大的公共前缀长度进行,i=0,即每次从0循环至strs.length,所有的字符都相等,则count++,直至有一个字符不相同为止,循环终止

public String longestCommonPrefix(String[] strs) {
        if(strs==null)
            return null;
        String str=new String("");
        if(strs.length==0)
            return str;
        if(strs.length==1)
            return strs[0];
        int min=strs[0].length();
        int len=0;
        for(int i=1;i<strs.length;i++)
        {
            len=strs[i].length();
            min=min<len?min:len;
        }
        char temp='0';
        int count=0,i=0,j=0;
        boolean flag=true;
        for(i=0;i<min;i++)
        {
            temp=strs[0].charAt(i);
            flag=true;
            for(j=1;j<strs.length;j++)
            {
                if(temp!=strs[j].charAt(i))
                {
                    flag=false;
                    break;
                }
            }
            if(flag)
                count++;
            else
                break;
        }
        return strs[0].substring(0,count);

    }

思路二:假设 longest common prefix 等于字符串数组的最短字符串的长度,从0循环至strs.length,在前面最长公共最大长度的基础上比较相邻两个串的最大公共子串

public String longestCommonPrefix(String[] strs) {
        if(strs==null)
            return null;
        String str=new String("");
        if(strs.length==0)
            return str;
        if(strs.length==1)
            return strs[0];
        int min=strs[0].length();
        int len=0;
        for(int i=1;i<strs.length;i++)
        {
            len=strs[i].length();
            min=min<len?min:len;
        }
        int count=min,i=0,j=0;
        for(i=1;i<strs.length;i++)
        {
            for(j=min-1;j>=0;j--)
            {
                  if(!strs[i-1].substring(0,j+1).equals(strs[i].substring(0,j+1)))
                  {
                      count--;
                  }else
                    break;
            }
            min=count;
        }
        return strs[0].substring(0,count);

    }
时间: 2024-08-29 00:05:32

leetcode_Longest Common Prefix的相关文章

[转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描:对所有串,从字符串第0位开始比较,全部相等则继续比较第1,2...n位,直到发生不全部相等的情况,则得出最长公共前缀串. 横向扫描算法实现: //LeetCode_Longest Common Prefix //Written by zhou //2013.11.22 class Solution

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

LeetCode-- 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) { int strslen=strs.length; if(strslen==0) return ""; String temp=null; f

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