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 ...
随机推荐
- hive的top n
注意 hive 的hsql没有 top n这个功能,不像sql. 所以实现top n如下: 我想说的SELECT TOP N是取最大前N条或者最小前N条. Hive提供了limit关键字,再配合ord ...
- javascript之BOM事件注册和案例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- FSG报表打印报错,log文件显示java.sql.SQLException: No corresponding LOB data found
报错信息: +---------------------------------------------------------------------------+ Plsql 程序的日志信息开始 ...
- ViewPager 实现 Galler 效果, 中间大图显示,两边小图展示
正常情况下, ViewPager 一页只能显示一项数据, 但是我们常常看到网上,特别是电视机顶盒的首页经常出现中间大图显示两端也都露出一点来,这种效果怎么实现呢?先上一张效果图: 大家第一眼肯定想到了 ...
- (六十一)Xcode的git版本控制
打开终端 1.为项目添加git: 首先到达项目的根目录内部,输入git init,初始化一个空的代码仓库(隐藏文件.get). 接下来使用git add . --all .表达把当前目录及子目录中的文 ...
- android 加载图片oom若干方案小结
本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...
- crontab 任务程序执行乱码的问题
今天碰到一个坑爹的问题,定时用php程序从远程的mssql读取数据,并写入到mysql中,手动用php执行程序的时候,程序运行没有问题,但当用crontab任务定时执行php程序的时候就出问题了,插入 ...
- 使用 /sys 文件系统访问 Linux 内核
sysfs 与 /sys sysfs 文件系统总是被挂载在 /sys 挂载点上.虽然在较早期的2.6内核系统上并没有规定 sysfs 的标准挂载位置,可以把 sysfs 挂载在任何位置,但较近的2.6 ...
- div+css基础教程
本文存下来作为备忘. 第一节 了解div+css 一.什么是div+css div元素是html(超文本语言)中的一个元素,是标签,用来为html文档内大块(block-level)的内容提供结构和 ...
- Swing组件 创建窗口应用
package com.swing; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt. ...