题目:

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

题意:

写出一个函数。找到一组数组中的最长公共子串。

算法分析:

须要构建两重循环。第一层是最短子串的长度,还有一层是遍历字符串数组中的每一个成员。

brute force的想法,以第一个字符串为标准。对于每一个字符串从第一个字符開始,看看是不是和标准一致,假设不同,则跳出循环返回当前结果。否则继续下一个字符。

AC代码:

public String longestCommonPrefix(String[] strs)
{
if(strs == null || strs.length == 0)
return ""; int minLen=Integer.MAX_VALUE;
for(String str: strs)
{
if(minLen > str.length())
minLen = str.length();//找到这些字符串中的最短子串
}
if(minLen == 0) return ""; for(int j=0; j<minLen; j++)
{
char prev='0';
for(int i=0; i<strs.length ;i++)
{
if(i==0)
{
prev = strs[i].charAt(j);//每次仅仅取第一个字符串的头一个字符測试
continue;
} if(strs[i].charAt(j) != prev)//若全部的字符串的这个字符都相等,就取下一个字符,若不相等,就返回这个字符前面的那个些公共前缀
{
return strs[i].substring(0, j);
}
}
} return strs[0].substring(0,minLen);//全部字符串在最短字符串长度下前缀都相等
}

[LeetCode][Java] Longest Common Prefix的更多相关文章

  1. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  2. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  3. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  4. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  5. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  6. [LeetCode] 14. Longest Common Prefix

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

  7. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  8. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  9. LeetCode(54)-Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...

随机推荐

  1. 总线(bus);设备(devices);驱动(drivers)

    Linux Cross Reference Free Electrons Embedded Linux Experts • Source Navigation  • Diff Markup  • Id ...

  2. lombok安装、配置、使用

    eclipse安装Lombok 运行安装: java -jar ${path}\lombok.jar 选择IDE所在路径点击Install/Update即可使用. 手动安装1. 将lombok.jar ...

  3. Luogu 2216 [HAOI2007]理想的正方形 (单调队列优化)

    题意: 给出一个 N×M 的矩阵,以及一个数值 K ,求在给定的矩阵中取出一个 K×K 的矩阵其中最大值减去最小值的最小值. 细节: 没有细节来发暴力走天下,20分也是分啊~~~ QAQ. 分析: 感 ...

  4. codeforces Lightsabers (hard)

    题目大意: 给定每种球的数量,求从中选取k个球有多少种不同的取法,同种球视为相同的. 题解: 多项式(1+x+x^2+...+x^a[1])*(1+x+x^2+...+x^a[2])*(1+x+x^2 ...

  5. CSS里总算是有了一种简单的垂直居中布局的方法了

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. Working out (DP)

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  7. TOJ 5021: Exchange Puzzle

    5021: Exchange Puzzle  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit ...

  8. 九度oj 题目1140:八皇后

    题目描述: 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的 ...

  9. Ubuntu安装 Docker CE,VNC访问docker图形界面并安装ROS

    从包安装 如果您无法使用Docker的存储库来安装Docker CE,则可以下载.deb适用于您的发行版的 文件并手动安装.每次要升级Docker CE时都需要下载新文件. 安装Docker CE,将 ...

  10. BZOJ 1861 [Zjoi2006]Book 书架 ——Splay

    [题目分析] 模板题目. 首尾两个虚拟结点,十分方便操作. [代码] #include <cstdio> #include <cstring> #include <cma ...