LeetCode第[14]题(Java): Longest Common Prefix
题目:最长公共前缀
难度:EASY
题目内容:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
翻译:编写一个函数,在字符串数组中查找最长公共前缀字符串。
如果没有公共前缀,则返回空字符串。
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
所有输入都是小写字母a-z。
我的思路:最简单的方法对String[]中最短的那一个长度进行遍历,在遍历中取String[]中每一个的当前位置字符与下一个比较,一旦不一样就返回结果。
还有一种就是,用Set,在内部遍历中用set将String[]中每一个的当前位置字符放入,出来的时候判断size()是否==1,好吧这种空间复杂度更高,显得更蠢。。
public String longestCommonPrefix(String[] strs) {
if (strs.length < 1) {
return "";
}
int minlen = Integer.MAX_VALUE;
for (int i = 0;i < strs.length; i++) {
if (strs[i].length() < minlen) {
minlen = strs[i].length();
}
} // 获得最短长度minLen
StringBuffer sb = new StringBuffer();
for (int j = 0; j < minlen; j++) {
for (int i = 0; i< strs.length - 1; i++) {
if (strs[i].charAt(j) != strs[i+1].charAt(j)) {
return sb.toString();
}
}
sb.append(strs[0].charAt(j));
}
return sb.toString();
}
我的时间复杂度: O(N*M) N为字符串个数,M最短字符串长度
编码过程问题:
1、一开始直接拿strs[0]的长度做的循环,导致测试用例{aa,a}越界,写了minLen后通过
2、minLen的初值一开始写成了MIN_VALUE.
参考答案Code:
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
String pre = strs[0];
for (int i = 1; i < strs.length; i++)
while (strs[i].indexOf(pre) != 0)
pre = pre.substring(0, pre.length()-1);
return pre;
}
9行! 厉害了,。。
答案复杂度:O(N*M) 虽然复杂度看起来一样,但是其实每次while循环并没有循环M次,也少了取minLen的操作
答案思路:取任意一个字符串做初始前缀,然后对后面每一个进行while循环:如果当前的前缀 与你进行字符串匹配的结果不是0【是0则是前缀,null,和其他字符都不行】,则把pre的最后一个字符去掉,再匹配,直到为0.
由于是取大家最长的公共前缀,所以也就类似于木桶短板效应,最后的值会是那个最短的,所以把pre保存给下一个再进行while循环。
LeetCode第[14]题(Java): Longest Common Prefix的更多相关文章
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode每天一题】Longest Common Prefix(最长前缀)
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [LeetCode][Java] Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函 ...
- Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- leetcode第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
随机推荐
- cygwin简介,安装及卸载(体验UNIX & Linux环境)
对于爱好者或初学者来说,为了体验UNIX & Linux环境,去安装虚拟机或双系统稍显麻烦,cygwin是一个很好的选择 具/原料 安装windows的电脑一台(可以联网) 法/步骤 ...
- druid
实时分析型数据库 Druid | Interactive Analytics at Scale http://druid.io/ Druid is primarily used to store, q ...
- fun_action
make an absolute URI from a relative one http://php.net/manual/en/function.header.php <?php /* Re ...
- 一.数据库连接对象connection
1.python 3.5,需要把MySQLdb换成pymysql
- 《深入理解Linux内核》阅读笔记 --- Chapter 3 Processes
Process Switching 1.The set of data that must be loaded into the registers before the process resume ...
- Python(进程池与协程)
1.进程池与线程池: 为什么要用“池”:池子使用来限制并发的任务数目,限制我们的计算机在一个自己可承受的范围内去并发地执行任务 池子内什么时候装进程:并发的任务属于计算密集型 池子内什么时候装线程:并 ...
- DBCC SHRINKFILE收缩日志/收缩数据库/收缩文件
DBCC SHRINKFILE 收缩相关数据库的指定数据文件或日志文件大小. 语法 DBCC SHRINKFILE ( { file_name | file_id } { [ ,t ...
- HDU1081:To The Max(最大子矩阵,线性DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...
- 中文Appium API 文档
该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...
- mysql的-F与master-data理解(一个小型的big-log恢复)
例子: 使用mysqlbin-log恢复,有两种情况,一个是停数据库,一个是不停 在不停数据库的情况下,为了防止新的写入,需要将bin-log切割,然后新的数据会保存在新的bin-log里面 在此之前 ...