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. 就是返回 ...
随机推荐
- Windows Server 2008及以上系统磁盘无法查看(About UAC and ACE)
在windows Server2008及以上系統,如果UAC Enabled,ACE列表中不會包含Administrators成員的SID,所以即使你是administrators的成員,也無法訪問D ...
- 跳出NSDate
感觉任何语言关于时间的格式化处理,时区的处理都是多的,最近被NSDate的各种问题坑了好久 先看看关于NSDate自己的问题 1.NSDate NSDate获取当前时间 NSDate *date=[N ...
- phpmail发送邮件
---恢复内容开始--- 首先.需要phpmailer的包. 地址:https://github.com/Synchro/PHPMailer 解开压缩包,将class.phpmailer.php,cl ...
- python split(),os.path.split()和os.path.splitext()函数用法
https://blog.csdn.net/T1243_3/article/details/80170006 # -*- coding:utf-8 -*- """ @ ...
- js浏览器调试
JS调试 sources界面(主要用来控制执行) 打断点,右上角四个按钮分别是:跳到下一个断点,单步调试,跳入,跳出. 鼠标悬浮在变量上可以查看变量的属性: console界面(主要用于查看输出) 主 ...
- 白话Redis分布式锁
redis分布式 简单来说就是,操作redis实例时,不是常规(单机)操作一个实例,而是操作两台或多台,也就是基于分布式集群: 而且redis内部是单进程.单线程,是数据安全的(只有自己的线程在操作数 ...
- (数据库之pymysql)
权限管理http://www.cnblogs.com/linhaifeng/articles/7267587.html#_label6一.pymysql模块(安装与查询) 1.安装pymysql(py ...
- Cocos2dx3.1-Android环境搭建初体验
初玩Cocos2dx,多多包涵. 感觉版本号之间的差异比較大.相对前面的版本号来说.3.X更easy上手.更方便了. 一.安装python.我的python-2.7.3. 配置环境变量 系统变量里:在 ...
- [译转]深入理解LayoutInflater.inflate()
原文链接:https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ 译文连接:http://bl ...
- 五分钟学会 Kotlin 语法
为什么使用Kotlin 项目一期在收尾了终于有时间折腾了,一个多月以来Kotlin从入门到现在,坚持用来开发的切身感受.因为语法与Java的区别挺大的一开始很想放弃,如果不是因为项目在使用,想必很少人 ...