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

求很多string的公共前缀,用个两重循环就可以,代码如下:

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string prefix;
if(strs.size() == || strs[].size() == )
return "";
int start = ;
bool endLoop = false;
char cmp = strs[][];
while(true && !endLoop){
cout << cmp << endl;
for(int i = ; i < strs.size(); ++i){
if(start >= strs[i].size() || cmp != strs[i][start]){
endLoop = true;
break;
}
}
if(!endLoop){
start++;
if(start < strs[].size())
cmp = strs[][start];
} }
return strs[].substr(, start - );
}
};

LeetCode OJ:Longest Common Prefix(最长公共前缀)的更多相关文章

  1. [LeetCode]14. Longest Common Prefix最长公共前缀

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

  2. [leetcode]14. Longest Common Prefix 最长公共前缀

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

  3. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  4. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  5. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  6. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

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

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  8. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  9. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  10. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

随机推荐

  1. Android studio怎么修改文件名

    选中需要重新命名的文件 点击Android studio菜单中列表中的Refactor的选项 选择下拉菜单中的“rename”的选项 弹出rename的选项框,在输入框中输入需要重新的命名的名称. 点 ...

  2. python16_day18【Django_Form表单、分页】

    一.表单 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 1.Form类 创建Form类时,主 ...

  3. delphi WebBrowser获取iframe页面内容及操作

    uses MSHTML, ActiveX; function GetFrame(FrameNo:Integer):IWebbrowser2;var OleContainer:IOleContainer ...

  4. iOS 和服务端交互 数据加密策略

    总体逻辑: 客户端:对称加密数据,上传...回执对称解密 同理服务端:获取上传数据 对称解密 ...下发:对称加密 当且仅当登录接口和 拉新(更新nonce 和 key的接口)是对称加密上传 非对称解 ...

  5. Oracle数据安全(五)审计

    一.审计的概念 审计是监视和记录用户对数据库所进行操作,以供DBA进行统计和分析.利用审计可以完成下列任务 保证用户能够对自己在数据库中的活动负责. 禁止用户在数据库中从事于自己职责不相符的活动 调查 ...

  6. sql 转

  7. 【Head First Servlets and JSP】笔记24:include指令与include动作 & param动作 & foward动作

    include指令与include动作 1.样例代码 <%@ page contentType="text/html;charset=UTF-8" language=&quo ...

  8. 20145229吴姗珊逆向BOF实践

    20145229吴姗珊逆向BOF实践 实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ...

  9. Spring混合配置

    Spring混合配置 一.在JavaConfig中引入其他配置 在JavaConfig中引入JavaConfig配置 使用@Import({OtherConfig1.class,OtherConfig ...

  10. spring security采用自定义登录页和退出功能

    更新... 首先采用的是XML配置方式,请先查看  初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...