Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
Examples:
s = “leetcode”
return 0.
s = “loveleetcode”,
return 2.
Note: You may assume the string contain only lowercase letters.
解法1:HashTable
思路是将字符串建立hashtable,其中存的是每一个字符出现的位置,如果出现超过1次则位置设置为-1,然后遍历hashtable找出最小的非-1的即可。
C++
Java
解法2:HashTable
上面的思路有点繁复,如果hashtable中存入每一个字符出现的次数,那么只需要重新扫描一遍字符串,找到第一个次数为1的就是所求的答案。
C++
|
|