-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path014_CommonPrefix.c
80 lines (70 loc) · 1.77 KB
/
014_CommonPrefix.c
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
//1.49%
char* longestCommonPrefix(char** strs, int strsSize) {
if(!strs || 0 >= strsSize)
return "";
if(1 == strsSize)
return strs[0];
//Track position of longest common prefix
int pos = 0;
bool flag = true; //'false' while need to break out the circular
char charFirst = strs[0][pos];
while(charFirst != '\0')
{
for(int index = 1; index < strsSize; index++)
if(strs[index][pos] != charFirst || strs[index][pos] == '\0')
{
flag = false;
break;
}
if(!flag)
break;
pos++;
charFirst = strs[0][pos];
}
//Now we know the common prefix's length as 'pos'
if(pos <= 0)
return "";
char *rstStr = (char *)malloc(pos + 1);
for(int index = 0; index < pos; index++)
rstStr[index] = strs[0][index];
rstStr[pos] = '\0';
return rstStr;
}
//1.49%
char* longestCommonPrefix001(char** strs, int strsSize) {
if(!strs || 0 >= strsSize)
return "";
if(1 == strsSize)
return strs[0];
//Track position of longest common prefix
int pos = 0;
bool flag = true; //'false' while need to break out the circular
//Check if there exist 'empty' pointer
for(int index = 0; index < strsSize; index++)
if(*strs[index] == '\0')
return "";
while(true)
{
char charFirst = strs[0][pos];
if(charFirst == '\0')
break;
for(int index = 1; index < strsSize; index++)
if(strs[index][pos] != charFirst || strs[index][pos] == '\0')
{
//pos--;
flag = false;
break;
}
if(!flag)
break;
pos++;
}
//Now we know the common prefix's length as 'pos'
char *rstStr = (pos <= 0 ? "" : (char *)malloc(pos + 1));
if(pos <= 0)
return "";
for(int index = 0; index < pos; index++)
rstStr[index] = strs[0][index];
rstStr[pos] = '\0';
return rstStr;
}