题目:

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. centos6.5下yum安装lnmp(适合刚入职的新手的方法)

    新入职的员工,开始的时候都是让配环境,本地写代码用的wamp,在lnmp或lamp测试,除非有些土豪公司 用的是(果机). 另外安装时,把整个流程在脑子里先过一篇(记不全也没关系,一回生二回熟),重在 ...

  2. Oracle VM VirtualBox 设置本机与虚拟机共享文件

    文章转载自http://wenku.baidu.com/link?url=5uZ1xWnGC55FGW2XUqzLVdttRcik2xCqwRKEdh6z-nZJ63UQn3j2750ES_q4Oro ...

  3. 简单实现兼容各大浏览器的js复制内容到剪切板

    因为网站文章需要提供几个按钮,单击后实现复制文章内容到剪贴板. 在网上搜索了很多内容,发现都比较乱这里自己整理下,分享给大家 效果图如下: 之前使用的是window.clipboardData.set ...

  4. Windows 2008 R2 X64 安装WebsitePanel(WSP虚拟主机管理面板)

                   Windows 2008 R2 X64  安装WebsitePanel(WSP2.0虚拟主机管理面板) 估计很多同学都还不知道WebsitePanel是什么东东吧,Web ...

  5. Dev的DocumentManager添加窗体

    1.DocumentManager要设置自己的MdiParent属性 2.主窗体设置IsMidContainer为True 3.要生成的窗体设置MdiParent为主窗体 4.正常创建窗体,然后就可以 ...

  6. Python生态环境简介[转]

    Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy  原译: Python生态环境简介 当 ...

  7. 安卓手机的touchend事件不触发问题

    问题描述 $(document).on("touchstart touchmove",".btn-highlight",function(event){ $(t ...

  8. 一款点击图片进行无限循环的jquery手风琴特效

    一款点击图片进行无限循环的jquery手风琴特效,点击手风琴折合点,可以无限循环的点击下去,很炫酷的手风琴哟! 还有每张图片的文字介绍,因为兼容IE6所以找来分享给大家这个jquery特效. 适用浏览 ...

  9. php中的一些小细节(1)

    <?php $a=false; echo $a; //var_dump($a); ?> 输出结果为:     (即为空): 为什么会这样子? 查看官网对echo的相关资料得出: 结论:ec ...

  10. How to get Financial Dimension Value from Worker Position[AX2012]

    To get financial dimension value from worker position, add a new method in hcmWorker Table with scri ...