78. Longest Common Prefix【medium】
Given k strings, find the longest common prefix (LCP).
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】的更多相关文章
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- leetcode:Longest Common Prefix【Python版】
1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) class Solution: # @retur ...
- 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 ...
- 【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 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
随机推荐
- 栈的应用实例——平衡符号
检查().[].{}是否配对. /* stack_balance_symbol */ #include "stack.h" #include <stdio.h> #in ...
- STS 控制台 中文乱码(maven 中文乱码)
用uriEncoding标签设置中文字符集就行了 <plugin> <groupId>org.apache.tomcat.maven</groupId> <a ...
- spring-tool-suite(STS) 创建 spring boot项目
1.创建一个Spring Starter Project工程(new --> Spring Starter Project) 2.选择自己需要的依赖,因为想要通过REST方式来验证是否成功创建, ...
- word2vec模型cbow与skip-gram的比较
cbow和skip-gram都是在word2vec中用于将文本进行向量表示的实现方法,具体的算法实现细节可以去看word2vec的原理介绍文章.我们这里大体讲下两者的区别,尤其注意在使用当中的不同特点 ...
- IOS版微信小视频导出方法
1.在电脑上连接手机,打开iTools 选择 应用-应用-文件共享. 2.依次打开/Library/WechatPrivate/6e2809aac61608de6a6cc55d9570d25b/Sig ...
- SET GLOBAL FOREIGN_KEY_CHECKS取消外键约束
今天在工作中遇到的问题,在删除一个表时报错,发现有外键约束,所以不能删除,查了下发现需要取消外键约束. SET GLOBAL FOREIGN_KEY_CHECKS=0;全局取消外键约束 SET SES ...
- SXi5.5不识别硬件驱动的光盘定制
SXi5.5不识别硬件驱动的光盘定制~~RealTek8111E,Intel217V,Z97 AHCI [复制链接] gmx168 电梯直达 1# 发表于 2014-9-23 17:06 ...
- Linux生成高强度密码
在撰写,自动化脚本.往往需要添加账户及密码.如何自动化填写随机密码,有点意思.... 01.openssl生成密码 [root@mvp ~]# openssl rand -base 14Usage: ...
- Windwos在cmd如何复制文本
生活的琐事,总是要解决. 01.Win+R打开运行窗口 cmd--回车 02. 勾选快速编辑模式 注意: 快速编辑模式就是可以Ctrl+c(复制).Ctrl+v(粘贴)
- eclipse cdt Program "make" not found in PATH
eclipse cdt插件,开发c/c++程序,编译时报错Program "make" not found in PATH经查C:\MinGW\bin下确实无make.exe,有m ...