【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 companies asked this question

Show Tags

题目的大概意思是:输入多个字符串,找到它们的最长公共前缀。

这道题难度等级:简单

说明:如:“abcd”、“abefh”、“absyz”则其最长公共前缀为"ab";再如:“abcdefg”、“abcfg”、“gabcdest”则其最长公共前缀为空。通过举例说明可知,这里的最长公共前缀指的是从每一个字符串的第一个位置開始。若都同样,则匹配下一个。直到出现一个不同样或者某个字符串完结为止。

了解了题意之后。代码例如以下:

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

提交代码后。顺利AC,Runtime: 4
ms

时间: 2024-12-10 06:57:40

【LeetCode】LeetCode——第14题:Longest Common Prefix的相关文章

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> &

【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 最长公共前缀

公众号:爱写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(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]题(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

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