leetcode 题解 || Longest Common Prefix 问题

problem:

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

寻找 0 ~n 个字符串的最长公共前缀

thinking:

(1)公共前缀非常好理解。按位匹配就可以

(2)非常easy忘记处理0、1个字符串的情况。

code:

string prefix(string &str1, string &str2)
{
    string tmp;
    int i=0;
    while((i<str1.size())&&(i<str2.size())&&(str1.at(i)==str2.at(i)))
    {
        tmp.push_back(str1.at(i));
        i++;
    }
    return tmp;
}
class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        if(strs.size()==0)
            return "";
        if(strs.size()==1)
            return strs.at(0);
        string result=prefix(strs.at(0),strs.at(1));
        for(int i=1;i<strs.size();i++)
        {
            result=prefix(strs.at(i),result);
        }

        return result;
    }
};
时间: 2024-08-16 06:30:45

leetcode 题解 || Longest Common Prefix 问题的相关文章

LeetCode 014 Longest Common Prefix

[题目] Write a function to find the longest common prefix string amongst an array of strings. [题意] 求一组字符串的最长公共前缀 [思路] 使用归并思想求解 要求字符串[1,2,..,k,k+1,...n]的最大公共前缀,先分别求[1,2,...k]和[k+1,...,n]的公共前缀 [代码] class Solution { public: string commonPrefix(vector<stri

【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) {

[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 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)

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)

Java [leetcode 14] Longest Common Prefix

小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 该问题就是找到所有数组字符串里面的最长相同前字串.所以我的思路是先找到数组中最短的那个字符串,然后每次比较的时候最多循环该长度就行,这样避免字符串下标溢出的问题.设置StringBuilder对象用于存

[LeetCode][Java] Longest Common Prefix

题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函数.找到一组数组中的最长公共子串. 算法分析: 须要构建两重循环.第一层是最短子串的长度,还有一层是遍历字符串数组中的每一个成员. brute force的想法,以第一个字符串为标准.对于每一个字符串从第一个字符開始,看看是不是和标准一致,假设不同,则跳出循环返回当前结果.否则继续下一个字符. AC

LeetCode 14 Longest Common Prefix (C,C++,Java,Python)

Problem: Write a function to find the longest common prefix string amongst an array of strings. Solution: 时间复杂度O(n) 题目大意: 给一个字符串数组,要找到这些字符串的最大前缀公共子串. 解题思路: 既然是公共子串,那每个字符串肯定都包含有,并且在头部,首先把第一个字符串作为默认最大,然后依次与后边每一个字符串对比,计算所有的最大匹配长度,长度最小的就是 Java源代码(用时263ms