题目描述

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

【思路】当一个字符第一次出现的位置和它最后一次出现的位置相同,那么它就是只出现一次的数

 1 class Solution
 2 {
 3 public:
 4     int GetLastIndex(char c,string str)
 5     {
 6         for(int i = str.length() - 1; i >= 0; i --)
 7         {
 8             if(str[i] == c)
 9             {
10                 return i;
11             }
12         }
13         return -1;
14     }
15     int FirstNotRepeatingChar(string str)
16     {
17         if(str == "")    return -1;
18         int id = -1;
19         bool judge[str.length()];
20         for(int i = 0; i < str.length(); i ++)
21             judge[i] = false;
22         for(int i = 0; i < str.length(); i ++)
23         {
24             id = GetLastIndex(str[i],str);
25             if(id == i && !judge[i])
26                 return i;
27             judge[i] = judge[id] = true;
28         }
29         return -1;
30     }
31 };

 

相关文章:

  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-01-05
  • 2021-09-20
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2021-07-04
相关资源
相似解决方案