求大佬帮忙看看我这段代码错在哪?

weixin_45906870 2020-08-03 02:25:56
题目:
实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

代码如下:

int *getNext(char *p, int *next)
{
int k=-1, j=0;
next[0] = -1;
while (j<strlen(p))
{
if (k=-1 || p[k]==p[j])
{
next[++j] = ++k;
}
else
{
k = next[k];
}
}
return next;
}

int strStr(char * haystack, char * needle){
if (needle==NULL) return 0;

int x = strlen(needle);
int i=0, j=0;
int *next = (int *)malloc(x*sizeof(int));

getNext(needle, next);

while (i<strlen(haystack) && j<strlen(needle))
{
if (j=-1 || haystack[i]==needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j==strlen(needle))
return i-j;
else
return -1;
}
这是一道LeetCode上的题,我用的是KMP算法,写完代码后看了半天,完全没搞懂错在哪,求大佬们指正。
...全文
111 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dreamer.L 2020-08-03
  • 打赏
  • 举报
回复
if (j=-1 || haystack[i]==needle[j]);
你这行写错了吧?j = -1
自信男孩 2020-08-03
  • 打赏
  • 举报
回复
int *getNext(char *p, int *next)
{
int k=-1, j=0;
next[0] = -1;
while (j<strlen(p))
{
//if (k=-1 || p[k]==p[j])
if (k==-1 || p[k]==p[j])
{
next[++j] = ++k;
}
else
{
k = next[k];
}
}
return next;
}

int strStr(char * haystack, char * needle){
if (needle==NULL) return 0;

int x = strlen(needle);
int i=0, j=0;
int *next = (int *)malloc(x*sizeof(int));

getNext(needle, next);

while (i<strlen(haystack) && j<strlen(needle))
{
//if (j=-1 || haystack[i]==needle[j])
if (j==-1 || haystack[i]==needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j==strlen(needle))
return i-j;
else
return -1;
}

供参考~

有这两处的问题~
weixin_45906870 2020-08-03
  • 打赏
  • 举报
回复
我全部搞懂了,谢谢各位大佬们
weixin_45906870 2020-08-03
  • 打赏
  • 举报
回复
int *getNext(char *needle, int *next) { int k=-1, j=0, n=strlen(needle); next[0] = -1; while (j<n) { if (k==-1 || needle[k]==needle[j]) { next[++j] = ++k; } else { k = next[k]; } } return next; } int strStr(char * haystack, char * needle){ int len1=strlen(haystack); int len2=strlen(needle); if (len2==0) return 0; int i=0, j=0; int next[strlen(needle)+1]; //这里为什么要加1?我看其他KMP算法这里并没有加1 getNext(needle, next); while (i<len1 && j<len2) { if (j==-1 || haystack[i]==needle[j]) { i++; j++; } else { j = next[j]; } } if (j>=len2) return i-j; else return -1; } 大佬们,我已经写出正确的代码了,但是我不知道上面的next数组后面为什么要加1?
qybao 2020-08-03
  • 打赏
  • 举报
回复
if (needle==NULL) return 0 改成 if (needle==NULL) return -1 并追加 if (strlen(needle)==0) return 0 空字符串不是NULL,是””
weixin_45906870 2020-08-03
  • 打赏
  • 举报
回复
错误一: if (needle==NULL) return 0 改成 if (needle==NULL) return -1 并追加 if (strlen(needle)==0) return 0 错误二: //if (k=-1 || p[k]==p[j]) if (k==-1 || p[k]==p[j]) 错误三: //if (j=-1 || haystack[i]==needle[j]) if (j==-1 || haystack[i]==needle[j]) 改完之后还是不行哭了,究竟还错在哪里

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧