【LeetCode】014 Longest Common Prefix

题目:LeetCode 014 Longest Common Prefix

题意:给出一组字符串求公共前缀

思路:很多个字符串的公共前缀应该不会很高,所以直接暴力解决就好

但是又有个特判,即当只有一个字符串的时候,直接返回即可。另外,一定要注意每次利用下标访问字符串的时候,一定要判断是否在有效范围内

代码如下:

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {
 4         int n = strs.size();
 5         if(n == 0) return "";
 6         string str = strs[0];
 7         for(int i = 1; i < n; i++)
 8         {
 9             int len1 = str.size(), len2 = strs[i].size();
10             if(len1 == 0 || len2 == 0) return "";
11             int len = 0;
12             string tmp = "";
13             while(len < len1 && len < len2 && str[len] == strs[i][len])
14                 tmp += str[len++];
15             str = tmp;
16         }
17         return str;
18     }
19 };
时间: 2024-10-25 09:27:20

【LeetCode】014 Longest Common 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】1143. Longest Common Subsequence

题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order

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 -- 刷效率 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][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

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