题目地址:http://ac.jobdu.com/problem.php?pid=1283

 

题目描述:

在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符。

输入:

输入有多组数据
每一组输入一个字符串。

输出:

输出第一个只出现一次的字符下标,没有只出现一次的字符则输出-1。

样例输入:
ABACCDEFF
AA
样例输出:
1
-1
#include <stdio.h>
 
int main(void){
    char str[10001];
    int hash[256];
    int i;
 
    while (scanf ("%s", str) != EOF){
        for (i=0; i<256; ++i)
            hash[i] = 0;
        i=0;
        while (str[i]){
            ++hash[str[i]];
            ++i;
        }
        i = 0;
        while (str[i] && hash[str[i]] != 1)
            ++i;
        if (str[i])
            printf ("%d\n", i);
        else
            printf ("-1\n");
    }
 
    return 0;
}

 


参考资料:何海涛 -- 程序员面试题精选100题(13)-第一个只出现一次的字符[算法]

 

相关文章:

猜你喜欢
  • 2022-12-23
  • 2021-09-22
  • 2022-02-20
  • 2022-12-23
  • 2021-07-26
  • 2022-01-25
相关资源
相似解决方案