-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0002_1.cpp
More file actions
36 lines (35 loc) · 1.19 KB
/
DAY0002_1.cpp
File metadata and controls
36 lines (35 loc) · 1.19 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
// 14. Longest Common Prefix
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
std::sort(strs.begin(), strs.end());
std::string answer;//,str1=strs[0],str2=strs[];
for(int i=0;i<strs[0].length();i++){
if(strs[0][i]==strs[strs.size()-1][i]){
answer+=(strs[0][i]);
}
else{
break;
}
}
return answer;
}
// string longestCommonPrefix(vector<string>& strs) {
// for(int i=1;i<strs.size();i++){
// if(strs[i].length()==0){
// strs[0]="";
// break;
// }
// if(strs[i].size()<strs[0].size()){
// strs[0].erase(strs[i].size(),strs[0].size()-strs[i].size());
// }
// for(int j=0;j<strs[i].size()&&j<strs[0].size();j++){
// if(strs[i][j]!=strs[0][j]){
// strs[0].erase(j);
// break;
// }
// }
// }
// return strs[0];
// }
};