剑指offer34:第一个只出现一次的字符的位置

1 题目描述

  在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写)。

2 思路和方法

 ch[str[i]]++;

if(ch[str[i]]==1)  return i;

3 C++核心代码

 1 class Solution {
 2 public:
 3     int FirstNotRepeatingChar(string str) {
 4         int len = str.length();
 5         if(len==0)
 6             return -1;
 7         char ch[256] = {0};
 8         for(int i=0;i<len;i++)
 9             ch[str[i]]++;
10         for(int i=0;i<len;i++)
11         {
12             if(ch[str[i]]==1)
13                 return i;
14         }
15         return -1;
16     }
17 };
View Code

参考资料

https://blog.csdn.net/u013686654/article/details/76125009

猜你喜欢

转载自www.cnblogs.com/wxwhnu/p/11421105.html