longestCommonPrefix
Description:
Write a function to find the longest common prefix string amongst an array of strings.
Thoughts:
1.定义一个结果字符串result="";
2.如果List的长度为0,那么直接返回result;
3.找到数组中最短的字符串min_str;
4.将min_str从索引为0开始的字符,逐一的和其他的字符串的相应位置的字符进行比较
5.如果所有字符串当前的字符都是一致的话,就将当前字符append到result中;否则的话返回result;
6.重复过程4,5直到min_str全部比较完成
以下是我的java代码
package easy;
public class LongestCommonPrefix {
/*返回长度最短的字符串在List中所在的位置*/
private static int shortestStringLength(String[] strs){
//判断当前的List中是否有字符串,没有的话返回-1,否则返回当前list中长度最短的字符串所在的位置
if(strs.length == 0){
return -1;
}else{
int result = strs[0].length();
int record = 0;
for(int i = 1; i<strs.length;i++){
if(result>strs[i].length()){
result = strs[i].length();
record = i;
}
}
return record;
}
}
public static String longestCommonPrefix(String[] strs){
String result = "";
int m = shortestStringLength(strs);
/*shortestStringLength的返回值等于-1,说明strs中没有字符串,结果为空;
* 否则的话,利用最短的字符串,逐个的比较其他的字符串是否包含当前最短字符串的前缀。从而不断地增加前缀的长度。
*/
if(m == -1){
return result;
}else{
int n = strs.length;
for(int i = 0;i<strs[m].length();i++){
char a = strs[m].charAt(i);
for(int j = 0; j< n;j++){
if(a != strs[j].charAt(i)){
return result;
}
}
result = result+a;
}
return result;
}
}
public static void main(String[] args){
String[] strs = new String[]{"ac","ac","a","a"};
String result = longestCommonPrefix(strs);
System.out.println(result);
}
}
longestCommonPrefix的更多相关文章
- leetcode — longest-common-prefix
/** * Source : https://oj.leetcode.com/problems/longest-common-prefix/ * * Created by lverpeng on 20 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
随机推荐
- TCP的定时器系列 — 超时重传定时器
主要内容:TCP定时器概述,超时重传定时器.ER延迟定时器.PTO定时器的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd Q:一条TCP连接会使用 ...
- Leetcode_27_Remove Element
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41577997 Remove Element Given a ...
- 客户地点分配多OU
DECLARE l_num_user_id NUMBER; l_num_appl_id NUMBER; l_num_resp_id NUMBER; cust_account_rec_type hz_c ...
- Leetcode_168_Excel Sheet Column Title
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42554641 Given a positive integ ...
- PLSQL_R12 MOAC多组织的四个应用(案例)
一.摘要 R12 Form 或者其他二次开发时,很多情况下会涉及R12 MOAC多组织开发,以下介绍了4个常见的应用,如有遗漏还请学友继续补充 1. 开发时打开Form自动弹出组织选择实现方式(增加C ...
- Useful Scripts for E-Business Suite Applications Analysts
In this Document Purpose Questions and Answers IMPORTANT: 1. How to find versions of files i ...
- JAVA中重写equals()方法的同时要重写hashcode()方法
object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true:注意:当此方法 ...
- C语言之linux内核可变参实现printf,sprintf
昨天,我发表了一篇用可变参实现的fprintf函数,其实说实话还不完全是可变参实现的,因为用到了FILE * 这样的指针,需要包含stdio.h这个头文件才能实现这个函数,今天我们就来看看,如何抛弃s ...
- javascript语言扩展:可迭代对象(2)
在文章迭代器(1)中我们简单介绍了如何创建一个可迭代对象:出于某种考虑你可能想从可迭代对象中显式获取一个迭代器对象,这时你可以调用Iterator()函数(该函数是定义在JavaScript 1.7中 ...
- javascript中正则表达式和ruby中的一点差异
看到一个例子,不过这个例子中正则表达式的格式貌似是错的: Function.prototype.get_name = function(){ return this.name || this.toSt ...