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

[leetcode]First Unique Character in a String

时间:2020-02-05 21:51:17      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:pre   tco   rac   ring   charindex   leetcode   ==   etc   dict   

用了一个字典

class Solution:
    def firstUniqChar(self, s: str) -> int:
        charIndexDict = {}
        for i in range(len(s)):
            if s[i] in charIndexDict:
                charIndexDict[s[i]] = -1
            else:
                charIndexDict[s[i]] = i
        
        minIdx = float(‘inf‘)
        for char in charIndexDict:
            if charIndexDict[char] != -1:
                idx = charIndexDict[char]
                if idx < minIdx:
                    minIdx = idx
                    
        if minIdx == float(‘inf‘):
            return -1
        
        return minIdx

  

[leetcode]First Unique Character in a String

标签:pre   tco   rac   ring   charindex   leetcode   ==   etc   dict   

原文地址:https://www.cnblogs.com/lautsie/p/12266776.html

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