[LeetCode][Java] Longest Common Prefix
题目:
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的更多相关文章
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- Leetcode 14——Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...
- LeetCode(54)-Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...
随机推荐
- module_param
该宏定义在include/linux/moduleparam.h中 #define ___module_cat(a,b) __mod_ ## a ## b #define __module_cat(a ...
- poj--2139
Description The cows have been making movies lately, so they are ready to play a variant of the famo ...
- Spring注解@Component、@Repository、@Service、@Controller
@Service用于标注业务层组件 @Controller用于标注控制层组件(如struts中的action) @Repository用于标注数据访问组件,即DAO组件 @Component泛指组件, ...
- 4C. Stars
4C. Stars Time Limit: 2000ms Case Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO forma ...
- [android开发篇]elicpse安装教程
http://www.runoob.com/eclipse/eclipse-install.html Eclipse 安装(Neon 版本) Eclipse 最新版本 Eclipse Neon,这个首 ...
- SPOJ-Grid ,水广搜easy bfs
SERGRID - Grid 一个水广搜我竟然纠结了这么久,三天不练手生啊,况且我快三个月没练过搜索了... 题意:n*m的方格,每个格子有一个数,意味着在此方格上你可以上下左右移动s[x][y]个格 ...
- BZOJ 2005 [Noi2010]能量采集 ——Dirichlet积
[题目分析] 卷积一卷. 然后分块去一段一段的求. O(n)即可. [代码] #include <cstdio> #include <cstring> #include < ...
- angular父子scope之间的访问
1.子可以访问父的scope,也可以更新相同的scope,但父scope不会被刷新 2.父要访问子scope的方法
- 使用FL2440之问题1
随机送的usb转串口线(一头usb一头9针,蓝色),写明HL340,装上驱动后运行正常,但电脑设备管理器显示的却是CH340,以前还用过PL2303,百度总结一下他们的区别: CH340,PL2303 ...
- Linux 下tomcat的配置
参考文档:http://blog.csdn.net/jeamking/article/details/7881196 http://www.cnblogs.com/panxuejun/p/618641 ...