leetcode刷题之Longest Common Prefix(14)

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

示例 1:

输入: ["flower","flow","flight"]

输出: "fl"

示例 2:

输入: ["dog","racecar","car"]

输出: ""

解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

思路:假设第一个字符串就是最长前缀,依次和剩下的所有字符串对比,如果不是的话,依次减去一个字符再循环对比一遍,直到找到最大前缀或者pre为"",String.indexOf("") = 0

代码:

Java

public static String solution(String[] strs){

if(strs == null || strs.length == 0) return "";

String pre = strs[0];

int i = 1;

while(i < strs.length){

while(strs[i].indexOf(pre) != 0) {

pre = pre.substring(0, pre.length() - 1);

}

i++;

}

return pre;

}

原文地址:https://www.cnblogs.com/Simon-cat/p/10163325.html

时间: 2024-08-30 01:51:32

leetcode刷题之Longest Common Prefix(14)的相关文章

【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

leetcode Longest Common Prefix 14

Longest Common Prefix Total Accepted: 47436 Total Submissions: 182575 Write a function to find the longest common prefix(前缀) string amongst an array of strings. Hide Tages String 翻译:编写一个函数去查找在字符串数组中的最长公共前缀的字符 尝试一(失败),代码如下: public static String longe

LeetCode之LCP(Longest Common Prefix)问题

这个也是简单题目,但是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an array of strings. 题意是给一个字符串数组,找出这个字符串数组中所有字符串的最长公共前缀. 注意是限定的前缀,而不是子串. 所以,直接的解法就是以第一个字符串为基准,逐个比较每个字符.算法复杂度也显然是O(M*N),M是字符串的个数,N是最长前缀的长度. 代码如下: class So

【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]题(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第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> &

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

题目描述: 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,代码如下:

14. Longest Common Prefix【leetcode】

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 寻找一个数组中最长的公共前缀 例如["baaa","caaabbb","aaaa"]输出"aaa" 结题思路: 判断非空的情况在进行计算 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中 用第一