试题请參见: 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. SUSE Linux 报错:too many open files in system

     现网执行的oracle数据库,有一天突然报错(alert日志):too many open files in system,须要对操作系统同意句柄数进行扩充,查阅了非常多资料,改动点主要集中在例 ...

  2. 【Bug Fix】Error : Can&#39;t create table &#39;moshop_1.#sql-534_185&#39; (errno: 150)

    运行alter操作, alter table xx_shop_info add index FK9050F5D83304CDDC (shop_area), add constraint FK9050F ...

  3. Qt笔记——MOC(莫克)

    moc 代表 Meta-Object Compiler,"元对象编译器".Qt 程序在交由标准编译器编译之前,先要使用 moc 分析 C++ 源文件. 假设它发如今一个头文件里包括 ...

  4. WPF案例 (四) 模拟Windows7桌面任务栏

    原文:WPF案例 (四) 模拟Windows7桌面任务栏 这个程序模彷了Windows7的桌面任务栏,当在桌面上双击某个快捷方式时,将打开一个新的子界面,并且在任务栏里创建一个链接到此界面的任务栏图标 ...

  5. poj3278(bfs)

    题目链接:http://poj.org/problem?id=3278 分析:广搜,每次三种情况枚举一下,太水不多说了. #include <cstdio> #include <cs ...

  6. 陈一舟《情系人人》:先搞钱,再搞人才_DoNews-IT门户-移动互联网新闻-电子商务新闻-游戏新闻-风险投资新闻-IT社交网络社区

    陈一舟<情系人人>:先搞钱,再搞人才_DoNews-IT门户-移动互联网新闻-电子商务新闻-游戏新闻-风险投资新闻-IT社交网络社区 陈一舟<情系人人>:先搞钱,再搞人才

  7. LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  8. SAE微信公众号PHP SDK, token一直验证失败

    用的是SAE,创建的是微信公众号PHP SDK框架,里面example文件夹下有server.php用来验证token的.但是问题来了,无论我怎么输入URL和token,一直告诉我token验证失败. ...

  9. hdu3555(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:求区间[a,b]内包含有'49'的数的总个数. 分析:dp[pos][0]表示到第pos位 ...

  10. hdu3966(树链剖分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:一颗树上,每个点有权值,定义三种操作: 1)I操作表示从a到b节点之间的节点都加上一个值 ...