[剑指offer] 34. 第一个只出现一次的字符

it2025-11-14  5

题目描述

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
一次遍历存储到哈希表 一次遍历取Value为1的第一个字符的索引 class Solution { public: int FirstNotRepeatingChar(string str) { int len = str.size(); if (len == 0) return -1; map<char, int> mapping; for (auto c : str) mapping[c]++; for (int i = 0; i < len; i++) { if (mapping[str[i]] == 1) return i; } return -1; } };

 

转载于:https://www.cnblogs.com/ruoh3kou/p/10129089.html

最新回复(0)