我是leetcode平台的新手。当我提交解决方案时,我得到Compile Error
错误是关于我的自定义函数,但我已成功运行代码块中的代码。谁能告诉我为什么?
int lengthOfLongestSubstring(char* s) {
int longestSub = 1;
for(int startIndex = 0, step = 1; startIndex + step < strlen(s); ){
if(myNoRepeating(s+startIndex, step, s[startIndex+step])){
step++;
longestSub = step;
} else {
startIndex++;
}
}
return longestSub;
}
int myNoRepeating(char *substring, int length, char next){
for(int i = 0; i < length-1; i++){
if(substring[length-1] == substring[i])
return 0;
}
for(int i = 0; i < length; i++){
if(next == substring[i])
return 0;
}
return 1;
}
运行代码状态:编译错误
运行代码结果:第19行:冲突 'myNoRepeating'的类型
答案 0 :(得分:0)
你需要在调用之前声明函数原型。
int myNoRepeating(char *substring, int length, char next);
在lengthOfLongestSubstring
方法之前放置此语句以解决编译错误。
另一个解决方案是重新排序代码,如:
int myNoRepeating(char *substring, int length, char next){
for(int i = 0; i < length-1; i++){
if(substring[length-1] == substring[i])
return 0;
}
for(int i = 0; i < length; i++){
if(next == substring[i])
return 0;
}
return 1;
}
int lengthOfLongestSubstring(char* s) {
int longestSub = 1;
for(int startIndex = 0, step = 1; startIndex + step < strlen(s); ){
if(myNoRepeating(s+startIndex, step, s[startIndex+step])){
step++;
longestSub = step;
} else {
startIndex++;
}
}
return longestSub;
}