【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"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

存储string类型的向量一定程度上可以看成二维数组。 最重要的思想是按照最小长度的字符串作为最外层遍历循环。

class Solution {
public:
string longestCommonPrefix(vector<string>& strs)
{
    //首先,找到所有字符串中最短的那一个.
    int min=0;
    for (int i = 0; i < strs.size(); i++)
    {
        if (min < strs[i].size()) min = strs[i].size();
    }
    if (min == 0) return "" ;
    //然后根据最短的那个字符串进行遍历
    string result="" ;
    int counter = 0;//计数器
    for (int i = 0; i < min; i++)
    {
        char compare = strs[0][i];
        for (int j = 0; j < strs.size(); j++)
        {
            if (strs[j][i] == compare) {
                continue;
            }
            else return result;
        }
        result.push_back(compare);
    }
    return result;
}
};

原文地址:https://www.cnblogs.com/hu-19941213/p/10970807.html

时间: 2024-07-30 12:14:24

【LeetCode】9.Array and String — 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]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 =

[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 Longest Common Prefix 最长公共前缀

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

【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

LeetCode[String]: Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个题目非常简单,只要弄清楚题意就可以非常快地解决,我的C++代码实现如下: string longestCommonPrefix(vector<string> &strs) { if (strs.empty()) return ""; string commonPrefix = strs[0]

【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