zoukankan      html  css  js  c++  java
  • Longest Common Prefix -最长公共前缀

    问题:链接

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

    解答:

    注意 当传入參数为空,即vector<string> 大小为0时。应该直接返回一个空字符串“”。而不是返回NULL。

    这点须要特别注意。


    代码:

    class Solution {
    public:
        string longestCommonPrefix(vector<string> &strs) {
            if(strs.size() == 0)
                return "";
            int i = 0;
            char a;
            while(1)
            {
                if(i >= (*strs.begin()).size())
                    return strs[0].substr(0,i);
                a = (*strs.begin())[i];
                for(vector<string>::iterator it = strs.begin()+1; it != strs.end(); ++it)
                {
                    if(i >= (*it).size() || a != (*it)[i] )
                        return strs[0].substr(0,i);
                }
                ++i;
            }
        }
    };


查看全文
  • 相关阅读:
    Centos 5.5+PHP5 添加phpjavabrige
    Google /Baidu Ping服务快速收录php
    基站一些信息
    搜索引擎提交入口收集
    druid简单教程
    http关于application/xwwwformurlencoded等字符编码的解释说明
    对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式。
    WinForm RTF生成器
    在 Visual C# .NET 中跟踪和调试
    c#中构建异常处理
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10670845.html
  • Copyright © 2011-2022 走看看