LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
Show Tags
SOLUTION 1:
解法很直观。先找到最小长度,然后逐个字母遍历,同时逐个遍历所有的字符串。注意各种小细节:
1. break的时候,应该返回上一个索引指向的子串。
2. 如果没有break,表示minlen长度的字串就是最大pre.
public class Solution {
//http://blog.csdn.net/fightforyourdream/article/details/14642079
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == ) {
// bug 2: should not return null.
return "";
}
// Find out the shortest length.
String s0 = strs[];
int len = s0.length();
for (String s: strs) {
len = Math.min(len, s.length());
}
// The index of the character which is examing.
// Bug 3: 当不会break的时候,结果是错的
// Bug 4: forget to add int i = 0;
for (int i = ; i < len; i++) {
// Bug 1: forget to write charAt(i);
char c = s0.charAt(i);
for (int j = ; j < strs.length; j++) {
if (strs[j].charAt(i) != c) {
// Bug 5: write substring to sbustring
return s0.substring(, i);
}
}
}
// Never break, means strs[0].0-len is the solution.
return s0.substring(, len);
}
}
2015.1.2 redo:
public class Solution {
public String longestCommonPrefix(String[] strs) {
String ret = "";
if (strs == null || strs.length == 0) {
return ret;
}
int minLen = Integer.MAX_VALUE;
for (String str: strs) {
minLen = Math.min(str.length(), minLen);
}
for (int i = 0; i < minLen; i++) {
for (int j = 1; j < strs.length; j++) {
if (strs[0].charAt(i) != strs[j].charAt(i)) {
return strs[0].substring(0, i);
}
}
}
return strs[0].substring(0, minLen);
}
}
GITHUB:
LeetCode: Longest Common Prefix 解题报告的更多相关文章
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- [LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...
- LeetCode——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- Lintcode: Longest Common Substring 解题报告
Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
随机推荐
- 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
- IP首部格式[转载]
TCP 传输首部是 IP首部,所以把IP首部格式 拿过来研究下,看IP首部解码过程: 来源:51CTO博客,地址:http://lihuan.blog.51cto.com/4391550/7999 ...
- Nginx防爬虫或限制浏览器访问
假定一个场景:某个网站它可能不希望被网络爬虫抓取,例如测试环境不希望被抓取,以免对用户造成误导,那么需要在该网站中申明,本站不希望被抓取.有如下方法: 方法一:修改nginx.conf,禁止网络爬虫的 ...
- 如何调试bash脚本
Bash 是Linux操作系统的默认Shell脚本.Shell是用来处理操作系统和用户交互的一个程序.Shell的脚本可以帮助用户自动化地和操作系统进行交互.你也可以理解为一种脚本式的编程.即然有编程 ...
- [Android] 使用Include布局+Fragment滑动切换屏幕
前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完毕了 ...
- Java Nashorn--Part 5
Nashorn 的高级应用 Nashorn 是一个复杂的编程环境,它被设计为一个强大的平台,用于部署应用程序,并与Java具有极大的互操作性. 让我们来看一些更高级的用于 JavaScript 到 J ...
- Python小游戏、小程序
python 小游戏之摇骰子猜大小 python 实现一个双色球生成程序 python-循环与判断练习题
- SIMULINK的模块库介绍
SIMILINK模块库按功能进行分为以下8类子库:Continuous(连续模块)Discrete(离散模块)Function&Tables(函数和平台模块)Math(数学模块)Nonline ...
- Android 获取闹钟引发的血案
想做一个锁屏的软件.锁屏后可以显示闹钟信息. 一开始的思路是通过android content provider获取 mActivityObject.getContentResolver().quer ...
- easyui中combobox的值改变onchang事件
今天在公司里,那jquery中的easy-ui-里面的combobox,真的郁闷死了! 把郁闷的事情记下来,下次就不会犯错了! 首先,肯定少不了,引入jquery的js文件!请大家注意了! 下面是代码 ...