-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path14.cpp
149 lines (142 loc) · 3.88 KB
/
14.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
__________________________________________________________________________________________________
4ms
static auto _=[](){cin.tie(nullptr);ios::sync_with_stdio(false);return 0;}();
class Solution {
public:
string longestCommonPrefix(vector<string> &strs)
{
int length = 0;
int size = strs.empty()? 0: strs.size();
if(size>0)
{
if (size == 1)
{
length = strs[0].size();
}
else
{
sort(strs.begin(), strs.end());
auto fstElem = strs.front();
auto lastElem = strs.back();
for (int i = 0; i <= fstElem.size() || i <= lastElem.size(); ++i)
{
length = i;
if (fstElem[i] != lastElem[i])
{
break;
}
}
}
}
return (length > 0) ? strs[0].substr(0,length):"";
}
};
__________________________________________________________________________________________________
8ms
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) { return ""; }
int minLen = strs[0].size();
for (string str : strs) {
if (str.size() < minLen) {
minLen = str.size();
}
}
int low = 0, high = minLen;
while (low <= high) {
int mid = (low + high) / 2;
if (isCommonPrefix(strs, mid)) { low = mid + 1; }
else { high = mid - 1; }
}
return strs[0].substr(0, (low + high) / 2);
}
bool isCommonPrefix(vector<string>& strs, int mid) {
string prefix = strs[0].substr(0, mid);
for (int i = 1; i < strs.size(); i++) {
if (strs[i].find(prefix)) { return false; }
}
return true;
}
};
__________________________________________________________________________________________________
12ms
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == 0)
return "";
// sort so lowest string first;
sort(strs.begin(), strs.end(), [](string& a, string& b){
return a.size() < b.size();
});
int size = strs[0].size();
int len = 0;
for(int i = 0; i < size; ++i)
{
char c = strs[0][i];
for(int j = 1; j < strs.size(); ++j)
{
if (strs[j][i] != c)
return strs[0].substr(0, len);
}
++len;
}
return strs[0].substr(0, len);
}
};
__________________________________________________________________________________________________
8656 kb
static int pre = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result;
if (strs.empty())
return move(result);
auto size = UINT32_MAX;
for (auto& str : strs) {
if (str.size() < size)
size = str.size();
}
for (int i = 0; i < size; ++i) {
auto temp = strs[0][i];
auto repeat = true;
for (int j = 1; j < strs.size(); ++j) {
if (temp != strs[j][i]) {
repeat = false;
break;
}
}
if (repeat) {
result += temp;
}
else
break;
}
return move(result);
}
};
__________________________________________________________________________________________________
8864 kb
static bool _foo = ios::sync_with_stdio(false);
static ostream* _bar = cin.tie(NULL);
class Solution {
public:
string longestCommonPrefix(vector<string>& strs)
{
if(strs.empty()) return "";
sort(strs.begin(), strs.end());
string a = strs[0], b = strs.back();
int i = 0;
for(; i < a.size(); i++)
if(a[i] != b[i])
break;
return a.substr(0, i);
}
};
__________________________________________________________________________________________________