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 ...
随机推荐
- native2ascii命令
native2ascii命令 native2ascii的命令行的命名格式: native2ascii -[options] [inputfile [outputfile]] 说明: ...
- XML跨平台,你懂的
XML跨平台,你懂的 [引子] 90后小妹,问我,"都说XML跨平台,偶真的,不理解.XML语言的这大优势,倒是深深记在脑海里了." 当然,偶立马应声答到,& ...
- C语言通讯录管理系统
本文转载自:http://blog.csdn.net/hackbuteer1/article/details/6573488 实现了通讯录的录入信息.保存信息.插入.删除.排序.查找.单个显示等功能. ...
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
由于andorid不推荐用传统的方向传感器,推荐用加速度传感器和地磁传感器来构造得到方向传感器的数据,其实主要是z轴的旋转角度 具体代码示例 代码如下 public class MainActivit ...
- linux:你不知道的echo
linux的echo命令功能是在显示器上显示一段文字.一般格式为: echo [ -n ] 字符串.参数n是指行尾不换行 echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开, 并在最后 ...
- rambbit mq 安装
https://blog.csdn.net/lmjy102/article/details/78571078 http://www.rabbitmq.com/tutorials/tutorial-on ...
- 015-OC基础语法-OC笔记
学习目标 1.[了解]Objective-C语言简介 2.[掌握]第一个OC程序 3.[掌握]OC中的字符串 4.[熟悉]OC中的一些玩意 5.[了解]面向过程与面向对象 6.[掌握]类的声明和实现 ...
- mysql进阶(十七)Cannot Connect to Database Server
Cannot Connect to Database Server 缘由 由于不同的项目中使用的数据库用户名与密码出现了不一致的情况,在其中之前较早一个项目执行过程中出现"The user ...
- Android增量升级的方法和原理
总结: 我们使用delta编码算法减少Android应用升级程序的大小.我们通过bsdiff和bspatch工具在android上实现delta编码算法.服务器软件和android应用已经部署.当前, ...
- Java线程专栏文章汇总
转载自 http://blog.csdn.net/ghsau/article/details/17609747 JDK5.0之前传统线程 Java线程(一):线程安全与不安全 J ...