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 ...
随机推荐
- C# 因IIS回收导致定时器失效的解决方案
首先不要设置iis自动回收,一般设置凌晨1-2点左右回收一次,当凌晨iis回收应用程序池的时候,会调用Application_End,执行里面的代码, 重新启动网站,建议定时器的代码放在Session ...
- mybatis自定义插件动态修改sql语句
step1:定义Interceptor实现org.apache.ibatis.plugin.Interceptor import org.apache.commons.logging.Log; imp ...
- Runway for Mac(UML 流程图绘图工具)破解版安装
1.软件简介 Runway 是 macOS 系统上一款强大实用的软件开发工具,Runway for Mac 是一个界面简单功能强大的UML设计师.此外,Runway for Mac 带给你所有你 ...
- 替换SQL字段中的换行符,回车符
替换SQL字段中的换行符,回车符: 在富文本内容中通常会出现回车.换行内容.在sql数据库中这些回车.换行符,输出html后,表现为空格. 这里是在数据导出.导入中发现的,通常把回车.换行符找出来,用 ...
- 基于matplotlib的数据可视化 -
matplotlib.pyplot(as mp or as plt)提供基于python语言的绘图函数 引用方式: import matplotlib.pyplot as mp / as plt 本章 ...
- 视图不能由多个 ListView 共享 (View can't be shared by more than one ListView) 的一个解决方法
1.问题的起因 在WPF中遇到一异常,如题. 因此做一个Demo代码来复现问题,代码如下: <Window x:Class="WpfAppThread.MainWindow" ...
- Chrome封掉不在chrome商店中的插件解决办法
添加chrome的管理模板,chrome.adm文件:下载地址:链接:http://pan.baidu.com/s/1c08st7i 密码: e811 然后进行如下的设置,重启chrome就可以解除封 ...
- Android 开发之修改 app 的字体大小(老人模式)
新的需求(可参见 微信和QQ改变字体): app 字体不随着系统字体大小变化 app 设置中有设置字体大小的开关,变大以后,整个 app 字体变大. 解决方案:(字体需要采用 dp 为单位,不能使用 ...
- constexpr与常量表达式(c++11标准)
关键字 constexpr 是C++11中引入的关键字,是指值不会改变并且在编译过程中就得到计算结果的表达式.(运行中得到结果的不能成为常量表达式,比如变量). 声明为constexpr的变量一定是一 ...
- 浏览器中beforeunload的使用
打开一些慢的网站的时候只见浏览器在不停转圈,但是页面还停留在当前页面的,有些网站的效果是你点击链接要跳到另一个页面的时候,在当前页面弹出一个框提示“正在加载中....”, 用到了浏览器的beforeu ...