[LeetCode]32. Longest Common Prefix最长公共前缀

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

解法:从所有的string的头到尾的字母逐一比较即可。

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int n = strs.size(), i = 0;
        bool flag = false;
        if (n == 0) return string("");
        if (n == 1) return strs[0];
        while (true)
        {
            for (int j = 0; j < n - 1; j++)
            {
                if (i >= strs[j].size() || strs[j][i] != strs[j + 1][i])
                {
                    flag = true;
                    break;
                }
            }
            if (flag) break;
            i++;
        }
        return string(strs[0].begin(), strs[0].begin() + i);
    }
};

精简得到一个更简洁的代码版本:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int n = strs.size();
        if (n == 0) return string("");
        for(int i = 0; i < strs[0].size(); i++)
        {
            for (int j = 0; j < n - 1; j++)
            {
                if (i >= strs[j].size() || strs[j][i] != strs[j + 1][i])
                    return strs[j].substr(0, i);
            }
        }
        return strs[0];
    }
};
时间: 2024-12-19 03:55:41

[LeetCode]32. Longest Common Prefix最长公共前缀的相关文章

leetcode——Longest Common Prefix 最长公共前缀(AC)

Write a function to find the longest common prefix string amongst an array of strings. 其实做起来会感觉很简单,需要注意的是要考虑效率的问题,毕竟可能是很长的字符串数组,所以可以考虑选取所有字符串中最短的那个来首先进行比较,因为最长公共子串肯定不会大于其长度,这样避免了字符串之间长度差异很大造成的效率损失,然后每次比较之后最长公共子串的长度也永远不会大于最短的那个字符串,只会不变或减小,只要遍历字符串数组,挨个

# Leetcode 14:Longest Common Prefix 最长公共前缀

公众号:爱写bug 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"

leetcode Longest Common Prefix 最长公共前缀 (python)

Write a function to find the longest common prefix string amongst an array of strings. class Solution: # @return a string #最长公共前缀 def longestCommonPrefix(self, strs): if strs is None or strs ==[]:return '' result ='' pre =None for cur in xrange(len(s

LeetCode Longest Common Prefix 最长公共前缀

题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短,所以前缀最长也只是最短字符串的长度. 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 string ans=""; 5 if(strs.empty()

[LeetCode] 14. Longest Common Prefix 最长共同前缀

Write a function to find the longest common prefix string amongst an array of strings. 这题有好几种解法,个人认为会1,2的解法就可以了,但这种多方法解题的思路可以好好学习一下.具体可参考:Longest Common Prefix 1. 一个一个字符串取比较word by word matching: 先拿前2个,从第一位开始比较,直到发现有不同的字符,此时前面一样的字符串在去和后面的字符串比较,直到结束.可

LeetCode 14 Longest Common Prefix 最长前缀

题目:Write a function to find the longest common prefix string amongst an array of strings. 翻译:求一个字符串数组中 共同的最长前缀. 思路:以第一个串为基准,逐个位置遍历,并遍历字符串数组,如果出现某个字符串长度小于当前位置,或者出现当前位置的字符不相同,返回字串strs[0].substring(0,pos):思路很简单. 代码: public String longestCommonPrefix(Str

【LeetCode】9.Array and String — 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 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][Java] Longest Common Prefix

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