最長公共前綴
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
str0 = min(strs)
str1 = max(strs)
for i in range(len(str0)):
if str0[i] != str1[i]:
return str0[:i]
return str0
zip(zipped)的应用
有了压缩,肯定少不了解压缩,在python中,对zip函数的解压缩就是zip函数,解压缩并非时返回原来的两个列表,而是返回一个列表,之前的内容被定义为元组类型
class Solution(object):
def longestCommonPrefix(self, strs):
ans = ''
for i in zip(*strs):
if len(set(i)) == 1:
ans += i[0]
else:
break
return ans
char * longestCommonPrefix(char ** strs, int strsSize)
{
int i,j;
if(strsSize == 0)
{
return "";
}
char * linshi = (char *)calloc(128,sizeof(char));
for(i = 0; i < strlen(*strs);i++)
{
for(j = 0;j < strsSize - 1;j++)
{
if(strs[j][i] != strs[j + 1][i])
{
return linshi;
}
}
linshi[i] = strs[j][i];
}
return linshi;
}