题意:

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. cocos2d-x使用python创建vs模板

    cocos2d-x 2.2推荐使用create_project.py创建工程,所有的平台都可以通过这个python文件创建工程.这个文件位置在源码cocos2d-x-2.2.2\tools\proje ...

  2. 在PhpStorm9中与Pi的xdebug进行调试

    PI的配置参考 http://www.cnblogs.com/yondy/archive/2013/05/01/3052687.html 在PhpStorm 9.0中参考下面的截图进行配置 配置完成以 ...

  3. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  4. HDU1003前导和

    简单维护前导和 #include<stdio.h> int main() { ],cas,key=; scanf("%d",&cas); while(cas-- ...

  5. conn,stmt,rset 的关闭(规范)

    Connection conn = null; Statement stmt = null; ResultSet rset = null; try { conn = dataSource.getCon ...

  6. ]用EnumChildWindows遍历窗口的方法

    最近项目有需要,得到一个非自己实现的窗口控件对象.于是想起曾经做过类似功能.总结如下: 调用EnumChildWindows(this->m_hWnd, EnumChildProc, NULL) ...

  7. Unity3d:延迟加载ScrollView的内容

    问题描述:在一个scrollview中加载了大量的数据,有文字.图片.视频等等,首次加载的时候会很慢很卡,而且加载出来后,内存占用很大.解决方案1:思:固定一块区域,当物体滚动到这区域的时候再加载物体 ...

  8. Redis本地环境搭建

    Windows 下环境搭建 1. 设置hosts set duapphosts=127.0.0.1 sqld.duapp.com set redisduapphosts=127.0.0.1 redis ...

  9. Regular Expression--Good parts

    匹配URL的正则表达式 <!doctype html><html lang="en"><head> <meta charset=" ...

  10. Chrome developer tool:本人钟爱的 console、Network 功能简谈

    在最开始时,本人调试查看网页,一直用的是 firefox 的 firebug 插件,并没有使用 chrome 的 developer tool .只不过,在日常生活使用过程中,一直使用的是 chrom ...