采用纵向遍历,即对第一个字符串,取出第一个字符,检查是否出现在随后每一个字符串中,以此类推。当遍历完成或有一个字符串不符合要求,直接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. 使用vue做表单验证

    <template> <Form ref="formInline" :model="formInline" :rules="rule ...

  2. Git误操作 git reset强制回滚 恢复commit方法

    参考: 找回Git中丢失的Commit Git误操作 git reset强制回滚 恢复commit方法 使用Git时,常有误操作,在Commit之后又执行了git reset --hard HEAD强 ...

  3. HTML的Doctype

    1. <!DOCTYPE> 声明位于文档中的最前面,处于 <html> 标签之前.告知浏览器的解析器,用什么文档类型 规范来解析这个文档. 2. 严格模式的排版和 JS 运作模 ...

  4. Java中String型与Date型数据的互相转换

    /** * Date类型转为指定格式的String类型 * * @param source * @param pattern * @return */ public static String Dat ...

  5. ImgNoGoodWindow

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEditor; ...

  6. nodejs项目安装ant design

    1.确保安装好nodejs $ node --version v10.4.1 2.确保npm $ npm -v 6.1.0 3.安装 $ sudo npm install antd-init -g / ...

  7. ubuntu18重装后 基本需求安装

    以下为本人重装ubuntu18后的基本需求安装过程 1 apt-fast sudo add-apt-repository ppa:apt-fast/stable sudo apt-get update ...

  8. ubuntu18+gtx1060 +cuda9+cudnn-v7+opencv3.1.0 配置深度学习环境

    将笔记本的ubuntu系统更新到18版本后重新配置深度学习环境,在此记载方便日后参考 具体配置为 Ubuntu18.04+gtx1060+opencv-3.1 第1步 安装依赖包 sudo apt-g ...

  9. WIN7右键在目录当前打开命令行Cmd窗口

    Win7系统大家习惯“Win+R”的组合键打开命令提示符. 2. 通常情况下,我们点击鼠标右键是没有命令行选项的.. 3.在桌面上先按住Shift键,然后鼠标右键,出现选项“在此处打开命令窗口(W)” ...

  10. java调用dll/so文件

    大家都知道用C++编写的程序如果用于windows使用则编译为xxx.dll文件,如果是Linux使用则编译为libxxx.so文件.下面将java调用dll/so文件的方法粘出来方便下次使用.此处使 ...