zoukankan      html  css  js  c++  java
  • 【leetcode】9 longest common prefix

    数组字符串的最长公共前缀

    注意事项:

    1 抓住前缀这一点,区别最长公共子串

    2 取基准pivot---第一个子串,依次与剩下字符串比较

    3 比较方法:获取两字符串中最短长度n,比较(0,n),从n开始比较,不相等即n--;

    更新pivot长度(0,n)

    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
            if(strs.size()==0)
                return "";
            string pivot=*strs.begin();
            vector<string>::iterator it=strs.begin();//注意iterator语法
            int n=0;
            for(it=it+1;it!=strs.end();it++){
                n=pivot.size()>(*it).size()?(*it).size():pivot.size();//注意size语法
                while(pivot.compare(0,n,*it,0,n)!=0){
                    n--;
                }
                pivot=pivot.substr(0,n);
            }
            return pivot;
        }
    };
     

  • 相关阅读:
    协议与接口相关
    jmeter 使用(1)
    jmeter 压力测试
    shell脚本的规则
    charles的原理及使用
    Linux环境部署和项目构建
    面向对象
    python 基础练习题
    jmeter 使用(2)
    Ext.apply
  • 原文地址:https://www.cnblogs.com/wygyxrssxz/p/4494443.html
Copyright © 2011-2022 走看看