leetcode第14题--Longest Common Prefix

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

就是返回一个字符串数组的所有的公共前缀。不难。我是已第一个字符串为参考,然后依次遍历其他的字符串,一旦遇到不同的。就break。然后返回第一个字符串的相应子序列。

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        int flag = 1, i;
    if(strs.size() == 0)
    {
        return "";
    }
    for ( i = 0; i < strs[0].size(); i++)
    {
        for(int j = 0; j < strs.size(); j++)
        {
            if (strs[0][i] != strs[j][i])
            {flag = 0; break;}
        }
        if (flag == 0)
            break;
    }
    return strs[0].substr(0,i);
    }
};
时间: 2024-12-20 07:14:17

leetcode第14题--Longest Common Prefix的相关文章

【LeetCode OJ 14】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. 解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀,依次与后面的元素进行比较,取其公共部分,比较结束后,剩下的就是该字符串数组的最长公共前缀,示例代码如下: public clas

LeetCode(14)Longest Common Prefix

题目如下: Python代码: def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if not strs: return '' for i,letter_group in enumerate(zip(*strs)): if len(set(letter_group))>1: return strs[0][:i] else: retur

# 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 14: Longest Common Prefix

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) { int length = strs.size(); if (length <=

leetcode_14题——Longest Common Prefix(字符串)

Longest Common Prefix Total Accepted: 44093 Total Submissions: 169565My Submissions Question Solution Write a function to find the longest common prefix string amongst an array of strings. Hide Tags String Have you met this question in a real intervi

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】LeetCode——第14题:Longest Common Prefix

14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Submissions: 345681 Difficulty: Easy Write a function to find the longest common prefix string amongst an array of strings. Subscribe to see which compan

LeetCode第[14]题(Java): Longest Common Prefix

题目:最长公共前缀 难度:EASY 题目内容: 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: ["flow

【leetcode刷题笔记】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer. 代码如下: 1 public class Solution { 2 public String longestCommonPrefix