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 ...
随机推荐
- BZOJ 3262 陌上花开 (三维偏序CDQ+树状数组)
题目大意: 题面传送门 三维偏序裸题 首先,把三元组关于$a_{i}$排序 然后开始$CDQ$分治,回溯后按$b_{i}$排序 现在要处理左侧对右侧的影响了,显然现在左侧三元组的$a_{i}$都小于等 ...
- js应用中的小细节-时间戳的转换和input输入框有效数字
1 input输入框内value值保留有效数字,js自带的方法.toFixed(),但是直接使用会报错,因为不论输入框内输入汉字.字母还是数字,类型都是string.解决的办法是将其转换为number ...
- Java String内存释放
Java String内存释放 这是一个坑,Java对于String对象,不进行内存的回收: 处理大数据量的时候,少用String. 与JDK有关系:jdk1.6环境下,内存只占用10M,jdk1.8 ...
- scp报错:Host key verification failed. REMOTE HOST IDENTIFICATION HAS CHANGED!
1 scp报错:REMOTE HOST IDENTIFICATION HAS CHANGED! [root@xx ~]# scp yum-3.4.3.tar.gz 10.xx.xx.12:/root ...
- PyQt: LineEdit的智能输入提示
使用的的类是QtGui.QCompleter from PyQt4 import QtGui,QtCore str = QtCore.QStringList(['a','air','airbus']) ...
- Collection 和 Collections 的差别?
Collection 是 java.util 下的接口,它是各种集合的父接口,继承于它的 接口主要有 Set 和 List:Collections 是个 java.util 下的类.是针对集合的 帮助 ...
- hdu 4882 ZCC Loves Codefires(贪心)
# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; ...
- 深刻理解Docker镜像大小
都说容器大法好,可是假设没有Docker镜像,Docker该是多无趣啊. 是否还记得第一个接触Docker的时候,你从Docker Hub下拉的那个镜像呢?在那个处女镜像的基础上.你执行了容器生涯的处 ...
- nyoj 585 取石子(六) 【Nim】
取石子(六) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 近期TopCoder的PIAOYI和HRDV非常无聊,于是就想了一个游戏,游戏是这种:有n堆石子,两个人 ...
- BZOJ 2844 高斯消元 线性基
思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...