zoukankan      html  css  js  c++  java
  • 最长公共前缀

    方法一 循环:

    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
            if(strs.empty())
            {
                return "";
            }               //注意为空的情况
            string str1;    //从vector中取
            string str0=strs[0];    //用作输出
            int num=strs.size();
            for(int j=1;j<num;j++)
            {
                str1=strs[j];
                int num0=str0.size();   //用作输出的
                int num1=str1.size();   //用作迭代的
                int count=0;
                string str3;
                if(num1<=num0)
                {
                    count=num1;         //输出长度大于迭代长度,交换输出和迭代
                    str3=str0;
                    str0=str1;
                    str1=str3;
                }
                else
                {
                    count=num0;
                }
                for(int i=0;i<count;i++)
                {
                    if(str1[i]!=str0[i])
                    {
                        str0.erase(str0.begin()+i,str0.end());         //删除输出与迭代不一致的部分
                        break;
                    }
                }
                if(str0.empty())
                {
                    return "";
                }                       //判断输出是否为空
            }
            return str0;
        }
    };

  • 相关阅读:
    win10新特性,ubuntu子系统(安装及配置)
    excel计算后列填充
    word中利用宏替换标点标点全角与半角
    CFD-post的奇技淫巧
    mfix模拟流化床燃烧帮助收敛的方法
    cygwin下配置alias
    endnote 使用方法
    win8系统换win7系统
    python中的函数以及递归
    01python算法--算法和数据结构是什么鬼?
  • 原文地址:https://www.cnblogs.com/wzhtql/p/10222074.html
Copyright © 2011-2022 走看看