-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14 Longest Common Prefix.cpp
More file actions
55 lines (43 loc) · 1.08 KB
/
14 Longest Common Prefix.cpp
File metadata and controls
55 lines (43 loc) · 1.08 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
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int n=strs.size();
string ans="";
if(n==0)
return ans;
if(n==1)
return strs[0];
int i=0;
int j=0;
while(true){
// cout<<i<<" "<<j<<endl;
if(j<strs[i].size() && j<strs[i+1].size()){
char pre=strs[i][j];
char curr=strs[i+1][j];
if(curr==pre){
++i;
}
else{
return ans;
// break;
}
if(i==n-1){
i=0;
ans.push_back(strs[0][j]);
++j;
}
}
else{
return ans;
// break;
}
}
return ans;
}
};