LeetCode——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
写一个函数找出字符串数组中的最长共现前缀字符串。
思路:共现。即要求数组中的所有元素的前缀中都要出现。所以所得的结果肯定是最短字符串的部分或所有或都不是,总之要以最短字符串为基准与其它字符串比較。
public static String longestCommonPrefix(String[] strs){
int len = strs.length;
if(len <= 0)
return "";
if(len == 1)
return strs[0];
int minlen = strs[0].length();
String temp = strs[0];
//找出一个长度最短的字符串
for(int i=1;i<len;i++){
if(strs[i].length() < minlen){
minlen = strs[i].length();
temp = strs[i];
}
}
int i=0,j = 0;
StringBuffer buf = new StringBuffer();
while(j < temp.length()){
for(i=0;i<strs.length;i++){
if(strs[i].charAt(j) != temp.charAt(j))
break;
}
if(i < len)
break;
buf.append(temp.charAt(j));
j++;
}
return buf.toString();
}
LeetCode——Longest Common Prefix的更多相关文章
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- [LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution { public String get(String a,String b) { if(a==""||b=="") ...
- leetcode Longest Common Prefix python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...
随机推荐
- pip常见用法汇总
1.pip安装 yum -y install epel-release && yum -y install python-pip 2.pip安装软件 (1)安装单个软件:pip ins ...
- JAVA面向对象编程深入理解图
- Python 绘图与可视化 matplotlib 填充fill和fill_between
参考链接:https://blog.csdn.net/You_are_my_dream/article/details/53457960 fill()填充函数曲线与坐标轴之间的区域: x = np.l ...
- Nodejs之旅開始
web前端是一个门槛低,但精通起来比較难的行业,由于它涉及的范围比較广,也许在十年前.我光靠切图,就能找到一个好的职位,可是如今,仅仅会切图.我们非常难找到自己惬意的工作,如今前端职位要求不仅是htm ...
- [CSS3] Responsive Table -- no more table
When the screen size is small, we can use "no more table" solution. So instead of render t ...
- 强名称程序集(strong name assembly)——为程序集赋予强名称
,唯一标识一个程序集 2,放置程序集被仿冒和被篡改. 3,能够部署到全局程序集缓存(GAC:GlobalAssembly Cache)中:在将强名称程序集不熟在GAC其中以后,强名称程序集也能够称为共 ...
- c# 获取文件夹下面所有文件夹列表
方法一: string dirPath = @"D:\App1"; List<string> dirs = new List<string>(Directo ...
- [Android] Android开发优化之——对界面UI的优化(2)
在Android应用开发过程中,屏幕上控件的布局代码和程序的逻辑代码通常是分开的.界面的布局代码是放在一个独立的xml文件中的,这个文件里面是树型组织的,控制着页面的布局.通常,在这个页面中会用到很多 ...
- oracle rac 巡检过程详解
一 RAC环境 RAC架构,2节点信息 节点1 SQL> show parameter instance NAME TYPE ...
- 制作可以SSH的Docker容器
以 Ubuntu 16.04为例: Docker里的root密码是随机的, 用passwd来设置新的密码 安装完SSH_SERVER后, 默认是不能用root登录的. vi /etc/ssh/sshd ...