题目:

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

代码:

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if ( strs.size()== ) return "";
std::string tmp_str = strs[];
for ( size_t i = ; i < strs.size(); ++i )
{
size_t j = ;
for ( ; j < std::min(tmp_str.size(), strs[i].size()); ++j )
if ( tmp_str[j]!=strs[i][j] ) break;
tmp_str = tmp_str.substr(,j);
}
return tmp_str;
}
};

tips:

空间复杂度O(n1),时间复杂度O(n1+n2...)

strs中的每个string串依次两两比较,保留下这一次比较得出的公共前缀为tmp_str;下次再比较时,可以用上次得到的公共前缀作为目标字符串。

这个解法的好处就是代码比较consice,但空间复杂度和时间复杂度其实都不是最优的。

=====================================================

第二次过这道题,自己写出来了代码,调了两次AC了。

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

【Longest Common Prefix】cpp的更多相关文章

  1. LeetCodeOJ刷题之14【Longest Common Prefix】

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  2. 【Longest Palindromic Substring】cpp

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  3. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  4. 【Longest Consecutive Sequence】cpp

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

  5. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  6. 【leetcode】Longest Common Prefix

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

  7. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  8. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  9. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

随机推荐

  1. Ninject在mvc中的简单配置

    前言 Ninject是一款开源的轻量级的依赖注入插件.从接触ioc以来,一直都是使用这个,感觉用起来还是不错的,配置起来也很方便简单.在mvc中更是基本傻瓜式的配置. 开发前的准备 新建一个mvc3项 ...

  2. tomcat目录简介

    http://www.cnblogs.com/kerrycode/p/3588816.html 主目录下面有bin.lib等目录 bin 存放Tomcat启动.停止服务程序以及一些其他脚本程序 lib ...

  3. 转载字典地址:http://blog.csdn.net/aladdinty/article/details/3591789

    相关文章: http://www.360doc.com/content/13/1003/23/14070959_318861279.shtml http://www.360doc.com/conten ...

  4. Vue.js学习 Item8 -- 方法与事件处理器

    方法处理器 可以用 v-on 指令监听 DOM 事件: <div id="example"> <button v-on:click="greet&quo ...

  5. ElasticSearch部署

    安装jdk1.7 1.卸载Liunx自带的openjdk rpm -qa | grep jdk 查看当前的jdk版本 sudo yum -y remove java-1.7.0-openjdk-hea ...

  6. mac 下 svn ignore 操作

    如何在svn中设备忽略的文件或者文件夹 1.如果你还没有对你的文件夹进行版本控制,则可以直接图形操作上进行ignore,或者在命令中运行 svn propedit svn:ignore 文件夹名 . ...

  7. 西门子SIMATIC IT平台

    西门子公司的SIMATIC IT平台基于ANSI/ISA S95标准开发,包含的功能组件覆盖了ISA S95规定的生产业务操作模型,同时也满足MESA所确定的MES系统11项功能要求. SIMATIC ...

  8. Java 第四天 Mysql

    下载地址 http://dev.mysql.com/downloads/  社区版是免费的 配置将zip 解压copy到本地,如:C:\mysql-5.6.15-winx64,复制配置文件my-def ...

  9. Java 第二天

    1.不带访问修饰符号的类成员变量默认是friendly(可以同一个包中访问) 2.protected的类成员可以在同一个包及其子类中访问 3.方法中可以定义内部类(如下面的代码),该类只能访问方法中的 ...

  10. 利用MVC编程模式-开发一个简易记事本app

    学了极客学院一个开发记事本的课程,利用自己对MVC编程模式的简单理解重写了一遍该app. github地址:https://github.com/morningsky/MyNote MVC即,模型(m ...