试题请參见: 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. birkenfeld / sphinx-contrib — Bitbucket

    birkenfeld / sphinx-contrib — Bitbucket README for sphinx-contrib This repository contains a collect ...

  2. 项目实践中--Git服务器的搭建与使用指南(转)

    一.前言 Git是一款免费.开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.在平时的项目开发中,我们会使用到Git来进行版本控制. Git的功能特性: 从一般开发者的角度来 ...

  3. Entity Framework6使用SQL Server Compact免安装部署

    原文:Entity Framework6使用SQL Server Compact免安装部署 使用Nuget安装以下包: EntityFramework.6.0.2 EntityFramework.Sq ...

  4. gcc的bug? c++模板类中友元函数的訪问权限问题

    原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...

  5. Eclipse常用热键

    ,Ctrl+D 删除选中的几行 ,Alt+上下箭头 移动选中的代码块 ,Alt+左右箭头 回退 前进 ,Alt+Shift+上下箭头 复制选中的代码块 ,sysout+Ctrl space 生成Sys ...

  6. HDOJ 5276 YJC tricks time multimap

    multimap的使用 YJC tricks time Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K ...

  7. There is no Action mapped for namespace / and action name UserAction

    果断收藏了,说的非常具体.刚開始学习的人常常遇到的问题. There is no Action mapped for namespace / and action name UserAction 在网 ...

  8. mysql用户权限分配及主从同步复制

    赋予wgdp用户查询权限: grant select on wg_dp.* to 'wgdp'@'%' IDENTIFIED BY 'weigou123'; grant all privileges ...

  9. Python的包管理

    0.Python的包管理 在刚开始学习Python的时候比较头疼各种包的管理,后来搜到一些Python的包管理工具,比如setuptools, easy_install, pip, distribut ...

  10. cocos2d-x2.x环境搭建配置

    [安装工具] VS2012 Cocos2D-X 2.2.3 Python 2.7.8 一.运行cocos2dx中的hello world! 1.在Cocos2D-X 2.2.3目录下,点击cocos2 ...