[LeetCode]-011-Longest Common Prefix

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

[]
=>""
["abcweed","htgdabc","sabcrf"]
=>""
["abcweed","abhtgdc","abacrf"]
=>"ab"

题目大意:求字符串数组的最长子前缀

 1 public class Solution{
 2     public String longestCommonPrefix(String[] strs) {
 3         //System.out.println(Arrays.toString(strs));
 4         if(strs.length == 0)
 5             return "";
 6         int min = Integer.MAX_VALUE;
 7         for(String s : strs){
 8             if(min>s.length())
 9                 min = s.length();
10         }
11         if(min==0)
12             return "";
13         //System.out.println(min);
14         String prefix = "", tmp = "";
15         int i=0;
16         for( ; i<min; i++){
17             prefix = strs[0].substring(0,(i+1));
18             //System.out.println("prefix=" + prefix);
19             for(int j=1; j<strs.length; j++){
20                 tmp = strs[j].substring(0,(i+1));
21                 //System.out.println("tmp=" + tmp);
22                 if(!prefix.equals(tmp))
23                     return strs[0].substring(0,(i));
24             }
25         }
26         System.out.println("i=" + strs[0].substring(0,(i)));
27         return "";
28     }
29
30
31     public static void main(String[] args){
32         String[] arr = {"","asdffg","aswd"};
33         if(args.length!=0)
34             arr = args;
35         Solution solution = new Solution();
36         String res = solution.longestCommonPrefix(arr);
37         System.out.println(res);
38     }
39 }
时间: 2024-10-09 03:08:39

[LeetCode]-011-Longest Common Prefix的相关文章

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

题目链接: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) {

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

LeetCode 14. 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 result = ""; if (!strs.size())return resul

leetCode 14. Longest Common Prefix 字符串

14. 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) {         if(strs.size() == 0)

LeetCode #14 Longest Common Prefix (E)

[Problem] Write a function to find the longest common prefix string amongst an array of strings. [Analysis] 思路非常简单,循环验证每一个字符串就可以通过OJ,代码也没有优化. [Solution] public class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0)

Java [leetcode 14] Longest Common Prefix

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

[LeetCode][Java] Longest Common Prefix

题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函数.找到一组数组中的最长公共子串. 算法分析: 须要构建两重循环.第一层是最短子串的长度,还有一层是遍历字符串数组中的每一个成员. brute force的想法,以第一个字符串为标准.对于每一个字符串从第一个字符開始,看看是不是和标准一致,假设不同,则跳出循环返回当前结果.否则继续下一个字符. AC

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[String]: Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个题目非常简单,只要弄清楚题意就可以非常快地解决,我的C++代码实现如下: string longestCommonPrefix(vector<string> &strs) { if (strs.empty()) return ""; string commonPrefix = strs[0]