[LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


014.Longest_Common_Prefix (Easy)

链接

题目:https://oj.leetcode.com/problems/longest-common-prefix/

代码(github):https://github.com/illuz/leetcode

题意

求多个字符串的最长公共前缀。

分析

一位一位判断即可,没什么坑点。

代码

C++:

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
		if (strs.empty())
			return "";
		for (int i = 0; i < strs[0].length(); i++) {
			for (int j = 1; j < strs.size(); j++)
				if (i >= strs[j].length() || strs[j][i] != strs[0][i])
					return strs[0].substr(0, i);
		}
		return strs[0];
    }
};

Java:

public class Solution {

    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0)
            return "";
        for (int i = 0; i < strs[0].length(); i++) {
            for (int j = 1; j < strs.length; j++)
                if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i))
                    return strs[0].substring(0, i);
        }
        return strs[0];
    }
}

Python:

class Solution:
    # @return a string
    def longestCommonPrefix(self, strs):
        if strs == []:
            return ''
        for i in range(len(strs[0])):
            for str in strs:
                if len(str) <= i or str[i] != strs[0][i]:
                    return strs[0][:i]
        return strs[0]
时间: 2024-08-28 17:17:54

[LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)的相关文章

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

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】014 Longest Common Prefix

题目:LeetCode 014 Longest Common Prefix 题意:给出一组字符串求公共前缀 思路:很多个字符串的公共前缀应该不会很高,所以直接暴力解决就好 但是又有个特判,即当只有一个字符串的时候,直接返回即可.另外,一定要注意每次利用下标访问字符串的时候,一定要判断是否在有效范围内. 代码如下: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 int

[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这样单词中只有一个字母不同的单词进行

Java [leetcode 14] Longest Common Prefix

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

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

14. Longest Common Prefix [easy] (Python)

题目链接 https://leetcode.com/problems/longest-common-prefix/ 题目原文 Write a function to find the longest common prefix string amongst an array of strings. 题目翻译 写个函数,找出一个字符串数组中所有字符串的最长公共前缀. 题目描述不清晰...补充几个例子,比如: {"a","a","b"} 返回 &qu

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; whil

[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个,从第一位开始比较,直到发现有不同的字符,此时前面一样的字符串在去和后面的字符串比较,直到结束.可