Leetcode 细节实现 Longest Common Prefix

Longest Common Prefix

Total Accepted: 17298 Total
Submissions: 63704My Submissions

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

题意:在一个字符串数组中找到最长的公共前缀

思路:

扫描数组,直到遇到一个在各个字符串不一样的字符

复杂度:时间O(n1 + n2 + ...) --> 最差的情况下,每个字符串都要扫描一遍,空间O(1)

string longestCommonPrefix(vector<string> &strs){
	if(strs.size() == 0) return "";
	if(strs.size() == 1) return strs[0];
	int c = 0;
	while(1){
		for(int i = 1; i < strs.size(); ++i){
			if(strs[i].size() <= c || strs[0].size() <= c || strs[i][c] != strs[0][c]) return strs[0].substr(0, c);
		}
		c++;
	}
}

时间: 2024-11-09 21:58:02

Leetcode 细节实现 Longest Common Prefix的相关文章

leetcode -- 刷效率 Longest Common Prefix

题目描述: Write a function to find the longest common prefix string amongst an array of strings. 很简单的一道题目,但是我写了3个不一样的版本,运行时间确实数倍之差..贴代码如下: 版本1: 这个版本的运行时间为  44 ms 版本2: 这个版本的运行时间为  16 ms 两者之间的区别便是:有无创建string ret返回字符串..  [updated]版本3:使用字符数组,时间同样是16ms,代码如下:

【LeetCode】014 Longest Common Prefix

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

[LeetCode][Python]14: Longest Common Prefix

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-common-prefix/14: Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.===Comments by Dabay===注意边

# 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

一. 题目描述 Write a function to find the longest common prefix string amongst an array of strings. 二. 题目分析 题目的大意是,给定一组字符串,找出所有字符串的最长公共前缀. 对比两个字符串的最长公共前缀,其前缀的长度肯定不会超过两个字符串中较短的长度,设最短的字符串长度为n,那么只要比较这两个字符串的前n个字符即可. 使用变量prefix保存两个字符串的最长公共前缀,再将prefix作为一个新的字符串与

【LeetCode】14 - Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. Solution: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { //runtime:4ms 4 string str=""; 5 if(strs.empty())return

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题目: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) {        string res;        int s

【LeetCode算法】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