题意:

Write a function to find the longest common prefix string amongst an array of strings. (Easy)

这两天实验室项目太忙了, 老板各种活,只能挑着先水几道easy题,这两个题是昨天做的没来得及写总结。

分析:

暴力的想法遍历比较一下就行,注意遍历的始末位置。优化的话改天再看看discuss

代码:

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result;
if (strs.size() == ) {
return result;
}
int minLength = 0x7FFFFFFF;
for (int i = ; i < strs.size(); ++i) {
int si = strs[i].size();
minLength = min(minLength, si);
} for (int i = ; i < minLength; ++i) {
int flag = ;
for (int j = ; j < strs.size() - ; ++j) {
if (strs[j][i] != strs[j + ][i]) {
flag = ;
break;
}
}
if (flag == ) {
result += strs[][i];
}
if (flag == ) {
break;
}
}
return result;
}
};

8.6更新:

上面的还是不删除了,算法上这个简单题应该没啥优化的了,无非是另一种方法拿第一个字符串以此与后续求公共前缀;

但是!当初这个代码写的真是太长太丑了...有一些可以优化代码的地方,比如

1.  flag其实没用,不满足情况就直接返回结果就行了;

2.  不要多走一遍求最小来作为遍历范围了,中间加一个判断语句就行;

3.  不用sting的+=,改用一个length最后再substr感觉效率上会高一点;(在leetcode跑的并没变化...)

更新后代码:

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int length = ;
if (strs.size() == ) {
return "";
}
for (int i = ; i < strs[].size(); ++i) {
for (int j = ; j < strs.size() - ; ++j) {
if (strs[j].size() == i|| strs[j][i] != strs[j + ][i]) {
return strs[].substr(,length);
}
}
length++;
}
return strs[].substr(,length);
}
};

LeetCode14 Longest Common Prefix的更多相关文章

  1. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  2. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  3. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  4. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  5. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  6. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

  7. 14. Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...

  8. Leetcode Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  9. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

随机推荐

  1. 【FFT】专题总结

    学了若干天终于学(bei)会了传说中的法法塔 感觉也没那么难用嘛 fft快速傅里叶变换 在大表课件上写就是解决高精乘的工具 其实很有理有据 fft就是用复数的折半引理优化两个多项式相乘的高端东西 他能 ...

  2. 全文索引之nutch与hadoop(转)

    原文:http://blog.csdn.net/chaofanwei/article/details/39476535 全文索引-lucene,solr,nutch,hadoop之lucene 全文索 ...

  3. SurfaceView 和 TextureView

    1.区别 The followings are two limitations of SurfaceView: You can not be animated, transformed and sca ...

  4. C#中自定义消息,与MFc对比

    在C#中采用的是事件驱动方式,但在我们使用的过程中,有时候通过调用系统原有的消息,处理起来会比较简单一些,特别是在处理与DLL文件的交互时,的确是非常的方便.    在C#中使用自定义消息      ...

  5. Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析(转载)

    简介: 这个分为两个部分的系列文章研究了 Apache Tomcat 服务器的系统架构以及其运用的很多经典设计模式.第 1 部分 分析了 Tomcat 的工作原理,第 2 部分将分析 Tomcat 中 ...

  6. 建表的sql

    1. 创建用户表 create table user( id int unsigned not null primary key auto_increment comment '自增id', user ...

  7. 未能加载文件或程序集"System.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"或它的某一个依赖项。系统找不到指定的文件。

    sqlserver 2005打开出现无法正常访问数据,提示信息: 未能加载文件或程序集"System.Data,Version=2.0.0.0,Culture=neutral,PublicK ...

  8. CSS line-height 和 vertical-align 精解(上篇)

    声明本文转自:http://hi.baidu.com/wolongxzg/item/a39ef8299c984283af48f5b0 line-height属性的具体定义列表如下: 语法: line- ...

  9. SQLServer获取随机数据

    1.比较常见和好用的一种 SELECT TOP 10 *, NEWID() AS randomFROM tableORDER BY random --newid函数会随机生成一个guid,很长的一个字 ...

  10. centos下 Apache、php、mysql默认安装路径

    centos下 Apache.php.mysql默认安装路径 http://blog.sina.com.cn/s/blog_4b8481f70100ujtp.html apache: 如果采用RPM包 ...