试题请參见: https://oj.leetcode.com/problems/longest-common-prefix/

题目概述

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

解题思路

这也是比較简单的算法提, 仅仅需比較比較数组中每一个字符串的第i位就可以, 直至不匹配为止.
记录此时i的值, 则为最长公共前缀.

源码

class Solution {
public:
std::string longestCommonPrefix(std::vector<std::string> &strs) {
bool isMatched = true;
int index = 0;
char character = 0; if ( strs.size() == 0 ) {
return "";
} do {
character = strs[0][index]; for ( size_t i = 0; i < strs.size(); ++ i ) {
if ( index >= strs.at(i).size() || strs.at(i).at(index) != character ) {
isMatched = false;
}
} ++ index; } while ( isMatched ); return strs[0].substr(0, index - 1);
}
};

LeetCodeOJ. Longest Common Prefix的更多相关文章

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

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

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

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

  3. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  4. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  5. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

  6. 14. Longest Common Prefix

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

  7. Leetcode Longest Common Prefix

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

  8. [LeetCode] 14. Longest Common Prefix

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

  9. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...

随机推荐

  1. aix 下 实现goldengate 随os启动而自己主动启动的脚本

    aix 下 实现goldengate 随os启动而自己主动启动的脚本: 1.用oracle用户建立/u01/info.txt,文件内容例如以下: sh date start mgr 2.chmod + ...

  2. UVA1455 - Kingdom(并查集 + 线段树)

    UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...

  3. shell命令批量杀死MySQL连接进程

    (1)将全部的MySQL连接进程杀掉 for i in `mysql -uroot -pzhangyun -Bse "show processlist" | grep -v &qu ...

  4. ajax相关体会

    参考原文: 例子:http://blog.csdn.net/beijiguangyong/article/details/7725596 原理讲解:http://www.cnblogs.com/min ...

  5. U10vim程序编辑器

    vim需要多加练习. 1.你可以将vim视为vi的高级版本.vi分成三种模式:一般模式,编辑模式和命令行模式. 一般模式:以vi打开一个文件就直接进入一般模式了(这也是默认的模式).在这个模式中,你可 ...

  6. poj3176--Cow Bowling(dp:数塔问题)

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14028   Accepted: 9302 Desc ...

  7. 使用VBA将批量的WORD文档转换为PDF

    Sub BatchConvertToPDF() Dim destFolderPath As String destFolderPath = GetFolderPath If destFolderPat ...

  8. Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. WPF学习之绘图和动画--DarrenF

    Blend作为专门的设计工具让WPF如虎添翼,即能够帮助不了解编程的设计师快速上手,又能够帮助资深开发者快速建立图形或者动画的原型. 1.1   WPF绘图 与传统的.net开发使用GDI+进行绘图不 ...

  10. coco2d-x 基于视口的地图设计

    <pre name="code" class="plain"> 基于视口的地图设计 DionysosLai 2014-06-14 第三人称游戏,玩家 ...