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

剑指Offer - 第一个只出现一次的字符位置

时间:2018-02-12 20:12:08      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:tmp   ice   tno   str   inter   this   代码   题目   tps   

https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

 

题目描述

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

代码

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        int tms[256];
        // firstly, it‘s wrong without this sentence
        memset(tms, 0, sizeof(tms));
        
        for (int i=0; i<str.size(); ++i) {
            int tmp = int(str[i]);
            tms[tmp] = tms[tmp]+ 1;
        }
        for (int i=0; i<str.size(); ++i) {
            int tmp = int(str[i]);
            if (tms[tmp] == 1) return i;
        }
        return -1;
    }
};

 

剑指Offer - 第一个只出现一次的字符位置

标签:tmp   ice   tno   str   inter   this   代码   题目   tps   

原文地址:https://www.cnblogs.com/charlesblc/p/8445323.html

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