Leetcode25: Longest Common Prefix

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

求若干字符串的最长公共前缀。

首先若无字符串,返回“”;接下来求得其中最短字符串的长度len,比较公共前缀只需最多比较len次;最后比较所有字符串里每一位上的字符。

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.size() == 0)
            return "";
        //if(strs.size() == 1)
            //return strs[0];

        int len = strs[0].length();
        for(int i = 1; i < strs.size(); i++)
        {
            if(len > strs[i].length())
                len = strs[i].length();
        }

        if(len == 0)
            return "";

        int i = 0;
        int j = 1;
        bool a = true;
        while(i<len && a)
        {
            for(j = 1; j < strs.size(); j++)
            {
                if(strs[j][i] != strs[j-1][i])
                {
                    a = false;
                    break;
                }
            }
            if(j == strs.size())
            {
                i++;
            }
        }

        return strs[0].substr(0, i);
    }
};

时间: 2024-07-31 18:19:59

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

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

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