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. 就是返回 ...
随机推荐
- 巨蟒python全栈开发-第24天 内置常用模块3
一. 1.re模块基础知识 2.python模块&re正则模块 3.实战:re模块&python(链家&电影天堂&豆瓣) 复习:上节课重点(1)sys.path 模块的 ...
- hdu 1513 Palindrome【LCS滚动数组】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- Windows File 管理工具:junction And Subinacl
junction.exe 是 Sysinternals 出品的命令行工具.使用前建议将其复制到%SystemRoot%/system32目录下 创建一个名为 D:/LINK 的[junction ...
- Powershell计算时间间隔(New-TimeSpan)
在Windows PowerShell里New-TimeSpan cmdlet提供了一种方法做日期算法. 计算时间间隔: 这个命令告诉你今天的日期与2006年除夕之间的天数: New-TimeSpan ...
- JavaScript 学习(3)核心对象
##JavaScript 学习 3 1.核心对象 1.1 String对象 声明和生成 var myString="Hello"; var myString=new String( ...
- Web 编程中编码问题
1. 常见字符编码 iso-8859-1(不支持中文) gbk(国标码) utf-8 (万国码, 支持全世界的编码) 2. 响应编码 当使用 response.getWriter() 来向客户端发送字 ...
- redis数据库设计(转)
原文:http://segmentfault.com/q/1010000000316112 redis是什么 redis就是一个存储key-value键值对的仓库,如何使用redis在于如何理解你需要 ...
- django--mysql设置
mysql基本配置 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '127.0.0.1', 'PORT': 3306, 'USE ...
- Android项目使用Ant多渠道打包(最新sdk)
参考文章: http://blog.csdn.net/liuhe688/article/details/6679879 http://www.eoeandroid.com/thread-323111- ...
- R 基本函数总结
基本一.数据管理 vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量 list:列表 data.frame:数据框 c:连接为向量或列表 len ...