Given k strings, find the longest common prefix (LCP).

Have you met this question in a real interview?

 
 
Example

For strings "ABCD", "ABEF" and "ACEF", the LCP is "A"

For strings "ABCDEFG", "ABCEFG" and "ABCEFA", the LCP is "ABC"

LeetCode上的原题,请参见我之前的博客Longest Common Prefix

解法一:

class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
string longestCommonPrefix(vector<string> &strs) {
if (strs.empty()) return "";
string res = "";
for (int j = ; j < strs[].size(); ++j) {
char c = strs[][j];
for (int i = ; i < strs.size(); ++i) {
if (j >= strs[i].size() || strs[i][j] != c) return res;
}
res.push_back(c);
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
string longestCommonPrefix(vector<string> &strs) {
if (strs.empty()) return "";
for (int j = ; j < strs[].size(); ++j) {
for (int i = ; i < strs.size() - ; ++i) {
if (j >= strs[i].size() || j >= strs[i + ].size() || strs[i][j] != strs[i + ][j]) {
return strs[i].substr(, j);
}
}
}
return strs[];
}
};

[LintCode] 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]14. Longest Common Prefix最长公共前缀

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

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

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

  4. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  5. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

  6. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  7. [leetcode]14. Longest Common Prefix 最长公共前缀

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

  8. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  9. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

随机推荐

  1. Centos7 wifi

    centos7如果在安装系统选择安装软件的选项是gnome套件(要注意退出选择界面回到安装界面时软件选项显示的是gnome,仅仅选择了gnome的软件也不行),安装完成后就会有wifi的图标,下面的方 ...

  2. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  3. 【webGL】threejs常用的api

    /*** 场景(scene) ***/ var scene = new THREE.Scene(); // 创建场景 scene.add(x); // 插入场景 /*** 相机(camera) *** ...

  4. wpf listview

    <Window x:Class="WpfTutorialSamples.ListView_control.ListViewGridViewSample"        xml ...

  5. win10下安装mysql5.7.16(解压缩版)

    注:本文涉及的是解压缩版的安装 安装教程 下载mysql 地址是:http://dev.mysql.com/downloads/mysql/ 解压缩下载的文件 修改ini文件(在解压缩后的mysql文 ...

  6. Idea 快捷键

    Ctrl+Alt+T 选中代码块加try catch等常用快捷操作.如图: psvm+Tab键 快速创建main方法 pout+Tab键 快速打出System.out.println(); Ctrl+ ...

  7. MFC注册窗口类以及FindWindow按窗口类名查询

    很多玩游戏的人都知道一般游戏客户端程序是不允许双开的,就是说在同一游戏在启动的时候,是无法打开多个窗口.很多其他软件如酷狗播放器等也是这样.如果把打开的窗口最小化,这时重新启动程序,最小化的窗口会被显 ...

  8. Mittag-Leffler定理,Weierstrass因子分解定理和插值定理

    Mittag-Leffler定理    设$D\subset\mathbb C$为区域,而$\{a_{n}\}$为$D$中互不相同且无极限点的点列,那么对于任意给定的一列自然数$\{k_{n}\}$, ...

  9. 如何清除Xcode8打印的系统日志

    Xcode升级成8之后,就会发现控制台打印的日志莫名其妙的变得超级多,最关键的是很多都是没有用的东西,而有些有用的东西却淹没在那无任何卵用的里面,在这我就说一下如何关掉这些没有用的日志. 1.直接快捷 ...

  10. Codeigniter的Redis使用

    1. ./config/redis.php: <?php $config['redis_host'] = '127.0.0.1'; $config['redis_port'] = '6379'; ...