problem:

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

寻找 0 ~n 个字符串的最长公共前缀

thinking:

(1)公共前缀非常好理解。按位匹配就可以

(2)非常easy忘记处理0、1个字符串的情况。

code:

string prefix(string &str1, string &str2)
{
string tmp;
int i=0;
while((i<str1.size())&&(i<str2.size())&&(str1.at(i)==str2.at(i)))
{
tmp.push_back(str1.at(i));
i++;
}
return tmp;
}
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.size()==0)
return "";
if(strs.size()==1)
return strs.at(0);
string result=prefix(strs.at(0),strs.at(1));
for(int i=1;i<strs.size();i++)
{
result=prefix(strs.at(i),result);
} return result;
}
};

leetcode 题解 || Longest Common Prefix 问题的更多相关文章

  1. [LeetCode 题解]: Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解: 寻找一组字符串的最 ...

  2. LeetCode题解——Longest Common Prefix

    题目: 给定一系列的字符串,找出这些字符串的最长公共前缀. 解法: 暴力法,依次比较每个字符串的每个字符,碰到第一个不同的就返回之前找到的前缀. 代码: class Solution { public ...

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

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

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

  5. 【leetcode】Longest Common Prefix

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

  6. [LeetCode] 14. Longest Common Prefix

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

  7. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  8. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  9. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

随机推荐

  1. Android菜鸟笔记- 获取未安装的APK图标、版本号、包名、名称、是否安装、安装、打开

    周末闲来无事,把Android的基础知识拿出来复习复习,今天主题是<获取未安装的APK图标.版本号.包名.名称.是否安装.跳转安装.打开> 一.获取APK图标 通常读取APK的图标能够用, ...

  2. Linux 安装Redis 5.0

    结构如下: Redis 官方不建议Redis安装在WINDOWS 服务器上(尤其是生产中分布式事物缓存). linux 下Redis 5.0主从复制(一主二从)哨兵模式的搭建:https://www. ...

  3. ROS-Rviz-turtlebot3仿真信息查看

    前言:Rviz是ROS自带的一种3D可视化工具. 一.安装turtlebot3功能包 1.1 安装依赖包 sudo apt-get install ros-kinetic-joy ros-kineti ...

  4. 编译安装FFmpeg 要支持xvid、x264、mp3、ogg、amr、faac

    编译安装FFmpeg 要支持xvid.x264.mp3.ogg.amr.faac libfaac    faac格式的编解码包libmp3lame    mp3格式编解码包libopencore-am ...

  5. java实现sql批量插入参数

    背景: 需要更新一些不规范的时间格式,如将某个时间格式化为yy-MM-dd,实际上为 yy-MM-dd hh:mm:ss,并且需要提供回滚脚本. 例如:规范化时间的脚本如下: ,) WHERE tes ...

  6. http协议以及防盗链技术

    http协议,又称为超文本传输协议,顾名思义,http协议不仅能传输文本,还能传输图片,视频,压缩包等文件,http协议是建立在tcp/ip协议的基础之上的,http协议对php程序员来讲可以说是重中 ...

  7. DirectUI界面编程(零)简介

    有过Win32.MFC编程经验的朋友应该都知道,传统Windows应用中的按钮.编辑框等控件都是一个子窗口,操作系统通过窗口句柄来唯一标识该窗口. 使用Windows 标准控件创建用户界面,美化起来是 ...

  8. php语法学习:轻松看懂PHP语言

    基础语法 开头结尾 PHP脚本以 "<?php " 开头以 "?>" 结尾 <!DOCTYPE html> <html>&l ...

  9. Python笔记(28)-----继承

    来自https://blog.csdn.net/sunwukong_hadoop/article/details/80175292 1.Python的继承以及调用父类成员 python子类调用父类成员 ...

  10. mount --bind

    [root@iZwz9i55e7v33yn8ksnh8nZ ~]# mkdir /tmp/dir1 [root@iZwz9i55e7v33yn8ksnh8nZ ~]# mkdir /tmp/dir2 ...