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

 
Example

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

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

解法一:

 class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
int min(int a, int b) {
return ((a < b) ? a : b);
} int calCount(string & a, string & b) {
int la = a.size();
int lb = b.size();
int count = ; for (int i = ; i < la && i < lb; ++i) {
if (a[i] == b[i]) {
count++;
} else {
return count;
}
}
} string longestCommonPrefix(vector<string> &strs) {
int count = INT_MAX;
int size = strs.size(); if (size == ) {
return "";
} for (int i = ; i < strs.size() - ; ++i) {
count = min(calCount(strs[i], strs[i + ]), count);
} return strs[].substr(, count);
}
};

解法二:

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

 

78. Longest Common Prefix【medium】的更多相关文章

  1. 14. Longest Common Prefix【leetcode】

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

  2. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  3. leetcode:Longest Common Prefix【Python版】

    1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) class Solution: # @retur ...

  4. spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】

    多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...

  5. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  6. 【leetcode】Longest Common Prefix

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

  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】14. Longest Common Prefix 最长前缀子串

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

  9. 【Longest Common Prefix】cpp

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

随机推荐

  1. java统计abacbacdadbc中的每个字母出现的次数,输出格式是:a(4)b(3)c(3)d(2)

    import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; /* ...

  2. 快捷键jdeveloper

    alt+home:定位文件ctrl+alt+space:代码自动提示alt+enter:自动导包ctrl+x:删除ctrl+-:类搜索ctrl+=:弹出当前打开列表ctrl+shift+back:最后 ...

  3. 链接、ip地址及端口号

    # encoding=utf-8 #python 2.7.10 #xiaodeng #链接(即报文如何通过传输控制协议链接从一个地方搬移到另外一个地方) #HTTP权威指南 13页 #TCP/IP # ...

  4. 彻底抛弃脚本录制,LR脚本之使用web_custom_request函数自定义http请求

    初学性能测试时候,第一步必学脚本录制,但一路下来各种录制失败.回放脚本失败的问题层出不穷,究其原因一是LR本身存在对测试环境的兼容性问题导致录制失败,更深层次的原因是录制者不清楚LR录制脚本的原理,或 ...

  5. eclipse下使用cygwin的方法(Windows下用eclipse玩gcc/g++和gdb)

    明天就回国了,今晚回国前写写如何配置eclipse和CDT.这个配置方法网上讨论不是很多,可能用的人少,毕竟Windows上写C++程序多数喜欢VS,即使写的是Linux程序,很多人仍然会用VS(说只 ...

  6. Swift内存管理、weak和unowned以及两者区别(如何使用Swift 中的weak与unowned?)

    Swift 是自动管理内存的,这也就是说,我们不再需要操心内存的申请和分配. 当我们通过初始化创建一个对象时,Swift 会替我们管理和分配内存.而释放的原则遵循了自动引用计数 (ARC) 的规则:当 ...

  7. iOS10 完美降级 iOS9.3.2,保留全部数据

    本篇文章由:http://xinpure.com/downgrade-ios10-perfect-ios9-3-2-retention-of-all-data/ iOS 10 Beta版尝鲜 前段时间 ...

  8. java学习之第五章编程题示例(初学篇)

    /* Animal.java */ package animal; public abstract class Animal { public abstract void cry(); public ...

  9. Java反射机制--笔记

    1.认识Class类 任何一个类都是Class类的实例对象,这个实例对象有三种表示方式. /*java 反射机制*/ // 获取类的方法 UserDao userDao = new UserDao() ...

  10. selenium python学习笔记---添加等待时间

    http://selenium-python.readthedocs.io/waits.html 有时候为了保证脚步运行的稳定性,需要在脚本中添加等待时间 添加休眠:需要引入time包,选择一个固定的 ...