博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - 14. Longest Common Prefix
阅读量:7010 次
发布时间:2019-06-28

本文共 1435 字,大约阅读时间需要 4 分钟。

 14. Longest Common Prefix

 ----------------------------------------------------------------------------

Mean: 

给定一个字符串集合,找出这个字符串集合的最长公共前缀.

analyse:

直接暴力.

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-16-12.27
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using
namespace
std;
typedef
long
long(
LL);
typedef
unsigned
long
long(
ULL);
const
double
eps(
1e-8);
class
Solution
{
public
:
   
string
longestCommonPrefix(
vector
<
string
>&
strs)
   
{
       
string
ret;
       
int
si
=
strs
.
size();
       
if(
si
<=
0)
return
ret;
       
bool
flag
=
0;
       
for(
int
i
=
0;
i
<
strs
[
0
].
length();
++
i)
       
{
           
for(
int
j
=
1;
j
<
si;
++
j)
           
{
               
if(
strs
[
j
].
length()
<
i ||
strs
[
j
][
i
]
!=
strs
[
0
][
i
])
               
{
                   
flag
=
1;
                   
break;
               
}
           
}
           
if(
flag)
break;
           
ret
.
push_back(
strs
[
0
][
i
]);
       
}
       
return
ret;
   
}
};
int
main()
{
   
Solution
solution;
   
int n;
   
while(
cin
>>n)
   
{
       
vector
<
string
>
strs;
       
while(n
--)
       
{
           
string
tmp;
           
cin
>>
tmp;
           
strs
.
push_back(
tmp);
       
}
       
cout
<<
solution
.
longestCommonPrefix(
strs)
<<
endl;
   
}
   
return
0;
}

 

转载于:https://www.cnblogs.com/crazyacking/p/5038576.html

你可能感兴趣的文章
重定向
查看>>
CentOS 安装详解
查看>>
heartbeat+DRBD+NFS
查看>>
JavaSE_03面向对象
查看>>
subversion部署安装 (安装篇)
查看>>
spring boot 整合mysql
查看>>
ASP.NET Core 中间件的几种实现方式
查看>>
Windows XP权限整合应用全解
查看>>
我的友情链接
查看>>
sum-git-w.sh
查看>>
Eclipse与github整合完整版
查看>>
yii 数据访问
查看>>
laravel的MVC
查看>>
js 对象方法、类方法、原型方法的区别;私有属性、公有属性、公有静态属性的区别...
查看>>
MySQL - ERROR 1062 (23000)
查看>>
100万并发连接服务器笔记之Java Netty处理1M连接会怎么样
查看>>
CentOS的free命令
查看>>
浅谈java内部类
查看>>
解决安卓SDK Build Tools版本低问题
查看>>
PyQt5 Opencv拍照
查看>>