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 ...
随机推荐
- TFS2012强制解除迁出(数据库操作方式)
同事离职,有个文件被迁出,查了好多资料终于解决. 在数据库中找到对应项目名字的数据库,例如Tfs_Project,记得先备份,保证删错了也能恢复~ 然后施行下列sql语句: --找到项目所在库 use ...
- TP自动生成模块目录
TP自动生成模块目录 例如我想在项目中增加一个AdminI模块 只需要在入口文件index.php中添加: define('BIND_MODULE','Admin'); 再访问127.0.0.1项目就 ...
- idea 修改编辑区字体样式、大小
idea 修改编辑区字体样式.大小 CreateTime--2018年4月26日10:36:59 Author:Marydon 设置-->Editor-->Font-->修改Fo ...
- Asp.net Mvc (Filter及其执行顺序)
应用于Action的Filter 在Asp.netMvc中当你有以下及类似以下需求时你可以使用Filter功能判断登录与否或用户权限,决策输出缓存,防盗链,防蜘蛛,本地化设置,实现动态Actionfi ...
- try/except/else语句
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python学习手册 868 #try/except/else语句 #try语句分句形式 except: # ...
- MySQL Desc指令相关
MySQL Desc指令相关 2011-08-09 11:25:50| 分类: my基本命令 |举报 |字号 订阅 1.desc tablename; 例如 :mysql> desc jo ...
- HTTP头返回码分析
http协议通讯时,在客户端发送请求后(request),服务器端返回的状态码解释(response) http状态码 1**:请求收到,继续处理 2**:操作成功收到,分析.接受 ...
- esp8266烧录Html文件,实现内置网页控制设备!
代码地址如下:http://www.demodashi.com/demo/14321.html 一.前言: 这个月也快结束了,时间真快,我服务器知识自学依然在路途中,这几天听到热点网页配置esp826 ...
- Docker镜像仓库Harbor之搭建及配置
目录 Harbor介绍环境.软件准备Harbor服务搭建Harbor跨数据复制配置FAQ1.Harbor 介绍 Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的 ...
- HDUOJ------2398Savings Account
Savings Account Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...