采用纵向遍历,即对第一个字符串,取出第一个字符,检查是否出现在随后每一个字符串中,以此类推。当遍历完成或有一个字符串不符合要求,直接return。

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans;
char c;
if(strs.size() < ) return ans;
for(int i=; i<strs[].size(); i++)
{
c = strs[][i];
for(auto s : strs)
if(i+ > s.size() || c != s[i])
return ans;
ans.push_back(c);
}
return ans;
}
};

14. Longest Common Prefix C++的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 14. Longest Common Prefix【leetcode】

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

  3. Leetcode 14. Longest Common Prefix(水)

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

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. C# 写 LeetCode easy #14 Longest Common Prefix

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

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

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

  7. 14. Longest Common Prefix

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

  8. [LeetCode] 14. Longest Common Prefix

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

  9. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  10. 【LeetCode】14 - Longest Common Prefix

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

随机推荐

  1. Nuget 打包 for .Net Standart project

    Create .NET Standard packages with Visual Studio 2015 Publishing packages nuge.exe 放在项目目录中 nuget spe ...

  2. HDU 4318 Power transmission(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=4318 题意: 给出运输路线,每条路线运输时都会损失一定百分比的量,给定起点.终点和初始运输量,问最后到达终点时最 ...

  3. bean标签常用属性

    scope属性: singleton:只有一个 prototpye:每次创建都是新的 对象初始化方法: init-method 对象销毁方法: destroy-method

  4. 生存分析与R--转载

    生存分析与R 生存分析是将事件的结果和出现这一结果所经历的时间结合起来分析的一类统计分析方法.不仅考虑事件是否出现,而且还考虑事件出现的时间长短,因此这类方法也被称为事件时间分析(time-to-ev ...

  5. 八皇后问题 递归实现 C语言 超详细 思路 基础

    八皇后问题 :假设 將八个皇后放到国际象棋盘上,使其两两之间无法相互攻击.共有几种摆法? 基础知识: 国际象棋里,棋盘为8X8格. 皇后每步可以沿直线.斜线 走任意格. 思路: 1.想把8个皇后放进去 ...

  6. colgroup和col的区别

    转载自:http://blog.csdn.net/carefree31441/article/details/3291397 colgroup和col一般出现在表格当中定义表格单独列的任意属性col能 ...

  7. coercing to Unicode: need string or buffer, int found报错

    转为string类型 str(a)

  8. centos7安装tomcat8 新手入门 图文教程

    系统环境 操作系统:64位CentOS Linux release 7.2.1511 (Core) JDK版本:1.8.0_121 下载tomcat8压缩包 访问官网:http://tomcat.ap ...

  9. ext4.2常用的几种弹框

    以下记录了自己在做项目时,经常用到的几种ext弹框.项目中使用的ext是4.2版本的. 1. Ext.Msg.alert() 使用此种方式时,如果提示信息过长则提示信息会被覆盖掉一部分. Ext.Ms ...

  10. loglog()函数

    数据: xd = [1, 2, 3] yd = [0.6, 0.2, 0.2] matlab中双对数函数: 命令: loglog(xd, yd, 'blacko-', 'MarkerFaceColor ...