码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-08-27 22:44:29      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:none   nbsp   opened   off   esc   first   csdn   event   ring   

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

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

标签:none   nbsp   opened   off   esc   first   csdn   event   ring   

原文地址:https://www.cnblogs.com/wxwhnu/p/11421105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!