"Disclaimer: All rights reserved, please indicate the source of the reprint, do not use for commercial purposes. Contact mailbox: [Email protected] "
Topic Link:http://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?rp=2&ru=/ta/coding-interviews& Qru=/ta/coding-interviews/question-ranking
Title Description
The position of the first occurrence of a character in a string (1<= string length <=10000, all composed of letters) is found. If the string is empty, return-1. Position index starting from 0
Ideas
Use a hash table to store the number of occurrences of each letter, and then one more loop to determine
Class Solution{public:int Firstnotrepeatingchar (String str) {if (str== "") Return-1;int len = Str.length (); int hsh[300]{0 };for (int i = 0; i<len; ++i) hsh[str[i]]++;for (int i = 0; i<len; ++i) if (hsh[str[i]]==1) return i;return-1;};
Copyright NOTICE: This article is the original blogger article, if reproduced, please indicate the source
The first character position that appears only once for the "sword Point"