题目

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

分析

该题目是求一个字符串容器中所有字符串的最长公共前缀。

AC代码

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == 0)
return "";
else if (strs.size() == 1)
return *(strs.begin()); vector<string>::iterator beg, end;
beg = strs.begin(); //先得到前两个串的公共前缀
string str = CommonPrefix(*beg, *(beg+1)); //迭代器后移两个位置
beg += 2; while (beg != strs.end())
{
if (str == "")
break;
str = CommonPrefix(str, *(beg++));
} return str;
} string CommonPrefix(const string &str1, const string &str2)
{
string common = "";
if (str1 == "" || str2 == "")
return common; int len1 = strlen(str1.c_str()) , len2 = strlen(str2.c_str()); int len = len1 > len2 ? len2 : len1 ; for (int i = 0; i < len; i++)
{
if (str1[i] == str2[i])
common += str1[i];
else
break;
} return common;
}
};

GitHub测试程序代码

LeetCode(14)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 OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  3. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  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

    Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...

  6. LeetCode 14: Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  7. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  8. LeetCodeOJ刷题之14【Longest Common Prefix】

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  9. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

随机推荐

  1. Appium安装说明

    1.安装Appium前,需要先安装node.js .node.js官方网站:https://nodejs.org/, 这里我以Windows 10为例进行安装,选择Windows installer( ...

  2. 题解报告:hdu 1520 Anniversary party(树形dp入门)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

  3. 485 Max Consecutive Ones 最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意:    输入的数组只包 ...

  4. Problem 2238 Daxia & Wzc's problem 1627 瞬间移动

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1627 http://acm.fzu.edu.cn/problem.php ...

  5. [转].NET MVC 分页以及增删查改

    本文转自:http://blog.csdn.net/sust2012/article/details/30761867 . 数据库操作,DAL 层: using System; using Syste ...

  6. AJPFX关于抽象类和接口的区别

    一.设计目的不同:接口体现的是一种规范,,类似于系统的总纲,它制定了系统的各模块应遵守的标准抽象类作为多个子类的共同父类,体现的是模式化的设计,抽象类可以认为是系统的中间产品,已经实现了部分功能,部分 ...

  7. Map集合的实现类

    Map的继承关系: Map接口的常用实现类: 1.HashMap.Hashtable(t是小写) HashMap不是线程安全的,key.value的值都可以是null. Hashtable是线程安全的 ...

  8. VCS filelist 文件格式

    VCS在运行仿真一般都会加仿真参数 –f filelist,filelist 是包含其他的仿真参数和整个工程的文件列表.具体格式如下: //file list format, just for exa ...

  9. 从URL输入到页面展示都发生了什么?

    总的来说分为以下过程: DNS解析 TCP三次握手 发送HTTP请求 服务器端处理请求并返回HTTP保文 浏览器渲染页面 断开连接:TCP的四次挥手 URL到底是什么? URL(Uniform Res ...

  10. xcode6的项目中虚拟键盘无法弹出

    这是因为Xcode6中的模拟器键盘设置跟之前的版本不一样了.之前版本是模拟器的键盘和电脑的键盘都可以使用,但是Xcode6的模拟器键盘只能使用一种,即要么是模拟器键盘,要么是电脑键盘.快捷键切换键盘类 ...