class Solution {
public:
string longestCommonPrefix(vector<string>& strs) { if(strs.size()==) return "";
if(strs.size()==) return strs[];
string com_str=strs[];
for(auto i=strs.begin();i<strs.end();i++)
{
while((*i).find(com_str,)!=)
{
com_str=com_str.substr(,com_str.size()-);
if(com_str.size()==) return "";
}
}
return com_str;
}
};

算法二:水平扫描
算法


想象数组的末尾有一个非常短的字符串,使用上述方法依旧会进行 S​S​ 次比较。优化这类情况的一种方法就是水平扫描。我们从前往后枚举字符串的每一列,先比较每个字符串相同列上的字符(即不同字符串相同下标的字符)然后再进行对下一列的比较。

//水平扫描

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) { if(strs.size()==) return "";
if(strs.size()==) return strs[];
string com_str=strs[];
for(int i=;i<com_str.size();i++)
{
char c=com_str[i];
for(int j=;j<strs.size();j++)
{
if(strs[j].size()==i||strs[j][i]!=c)
return com_str.substr(,i);
}
}
return com_str;
}
};

 

leetcode14最长公共前缀的更多相关文章

  1. leetcode-14最长公共前缀

    leetcode-14最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower& ...

  2. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  3. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

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

  4. LeetCode14.最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  5. LeetCode14.最长公共前缀 JavaScript

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  6. Leetcode14._最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow&q ...

  7. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  8. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  9. lintcode :最长公共前缀

    题目 最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP ...

随机推荐

  1. 记录错误or日记(更新中)

    前言: 从2018.8-17开始记录 本篇随笔记录做题时的小错误(大多数),考试总结(懒得总结了),做过的每个题的错误 2019.12.7 傻逼学校,给我三个小时假期给你们做题挣工资 2019.11. ...

  2. Python源码:字典

    一.创建增加修改 1.实现代码 #创建 stu_info = { "xiedi":28, "liuhailin":27,"daiqiao": ...

  3. 如何把任意网站制作成RSS

    如何把任意网站制作成RSS 参照一下链接,多试几次就掌握了. 参考链接:https://feed43.com/step-by-step.html

  4. oracle导入Excel表文本数据

    首先导Excel表数据要先建和Excel表字段对应的表,然后将Excel表另存为Txt文本, 然后在Plsql客户端点击工具->文本导入器 然后这里要选择用户及其表,点击导入数据就可以

  5. python asyncio 使用ThreadPoolExecutor和asyncio完成阻塞IO请求

    #使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket fro ...

  6. java构建树形列表(带children属性)

    一些前端框架提供的树形表格需要手动构建树形列表(带children属性的对象数组),这种结构一般是需要在Java后台构建好. 构建的方式是通过id字段与父id字段做关联,通过递归构建children字 ...

  7. System.InvalidOperationException: 'Cannot create more than one System.Windows.Application instance in the same AppDomain.'

    System.Windows.Application is a singleton: its constructor must only be invoked once (including App. ...

  8. 最近在折腾在线编辑,研究了下Wopi,下面粘贴出自己Office Online Server2016搭建与部署

    至少需要两台服务器,一台域控制器,一台部署Office Online Server https://docs.microsoft.com/zh-cn/officeonlineserver/office ...

  9. 英语NanyangJade南阳玉NanyangJade独山玉

    南阳玉NanyangJade又叫做独山玉,独山玉,因产于河南南阳的独山而得名,亦称“南阳玉”. 独山玉是中国四大名玉之一,有南阳翡翠之称,独山玉是一种黝帘石化斜长岩,由多种矿物组成,属多色玉器. 据文 ...

  10. Android系统源码目录

    Android系统源码目录 我们要先了解Android系统源码目录,为后期源码学习打下基础.关于源码的阅读,你可以访问http://androidxref.com/来阅读系统源码.当然,最好是将源码下 ...