-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlongestCommonPrefix.h
More file actions
79 lines (70 loc) · 2.35 KB
/
longestCommonPrefix.h
File metadata and controls
79 lines (70 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// longestCommonPrefix.h
// string
//
// Created by junlongj on 2019/8/12.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef longestCommonPrefix_hpp
#define longestCommonPrefix_hpp
#include <stdio.h>
#include <string>
#include <vector>
/*
14. 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-common-prefix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
namespace leetcode {
/*
LCP(s1,s2......sn) = LCP(LCP(LCP(s1,s2),s3),.....)
水平扫描:先计算第一个字符串和第二个字符串的最长前缀,然后结果和第三个字符串继续计算前缀。。。。
*/
std::string longestCommonPrefix2(std::vector<std::string>& strs) {
//最长公共前缀,找的是前缀
if (strs.empty()) return "";
std::string match = strs[0];
for(int i=1;i<strs.size();i++){
int j=0;
for(;j<strs[i].length();j++){
if(match[j] != strs[i][j])
break;
}
if (j == 0){
return "";
}
match=match.substr(0,j);
}
return match;
}
/*
纵向扫描,先判断每一列是否相等,然后进行下一列判断。
上面解法:复杂度为O(S*len),S为S数组大小,len为每项字符串长度
当前解法:复杂度为O(S*minlen),S为S数组大小,minlen为最短字符串长度.
*/
std::string longestCommonPrefix(std::vector<std::string>& strs) {
//纵向扫描
if (strs.empty()) return "";
int len = strs[0].length();
for (int i=0;i<len;i++){
for(int j=1;j<strs.size();j++){
//如果字符不匹配,或者当前字符串没有该项,直接返回结果。
if( i>strs[j].length() || strs[j][i]!= strs[0][i]){
return strs[0].substr(0,i);
}
}
}
return strs[0];
}
}
#endif /* longestCommonPrefix_hpp */