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;
            }
        }
    };


查看全文
  • 相关阅读:
    一个滑动小块
    js 封装的函数 总结
    引用公用文件
    静态页面表单中js验证
    linux中Firefox浏览器 手动安装 flash
    将双击“root的主文件”弹出的窗口设置为文件浏览器
    eclipse中的aptana插件的安装
    System Center VMM请注意不同语言版本的差异
    CodeFirst模式开发涉及到mysql简单使用
    Echarts的使用
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10670845.html
  • Copyright © 2011-2022 走看看