完整代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int firstUniqChar(char* s){
int i = 0;
char* ab = s+1;
char* cd = s-1;
while (*s)
{
while (*ab&&*s != *ab)//从前往后找
{
ab++;
}
while (*cd&&*s != *cd)//从后往前找
{
cd--;
}
if (*ab||*cd)//该元素有相同的元素,进行下一个元素
{
i++;
s++;
ab = s + 1;
cd = s - 1;
}
else
{
return i;//找到了,返回索引
}
}
return -1;//没找到,返回-1
}
int main()
{
char* a = "ghjdyu";
printf("%d",firstUniqChar(a));
}
结果实例:
事项:
第一个完整通过线上测试的程序,之前特别懒散,只写算法。