第一個只出現一次字元的位置 牛客網 劍指Offer
阿新 • • 發佈:2018-12-09
第一個只出現一次字元的位置 牛客網 劍指Offer
- 題目描述
- 在一個字串(0<=字串長度<=10000,全部由字母組成)中找到第一個只出現一次的字元,並返回它的位置, 如果沒有則返回 -1(需要區分大小寫)
class Solution: #run:28ms memory:5732k def FirstNotRepeatingChar(self, s): if len(s) <= 0: return -1 hash_dict = {} for i in s: if i in hash_dict: hash_dict[i] += 1 else: hash_dict[i] = 1 for j in s: if hash_dict[j] == 1: return s.index(j)