/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return int整型
*/
int FirstNotRepeatingChar(char* str ) {
// write code here
int a = 0;
int i = 0;
char* pst = str;
while (*pst)
{
pst++;
a++;
}
pst = str;
int count = 0;
for (int m = 0; m < a; m++)
{
for (i = 0; i < a; i++)
{
if (*pst == *(str + i) && count != i)
break;
}
if (i == a)
{
return count;
}
pst++;
count++;
}
return -1;
}
思路是既然要找第一次出现的字符并返回下标,这可以利用数组用C语言来实现,很容易想到的是把每个字母保存然后遍历数组中所有元素,循环结束时判断是否遇到相同的字符即可,具体方法用COUNT和I的判断相等来进行。如果所有的循环结束还是未返回,那么就返回-1即可。
缺点是时间复杂度是N方,优点是容易想到。
初学者的思路