当前位置: 移动技术网 > IT编程>脚本编程>Python > 【leetcode 简单】 第九十题 字符串中的第一个唯一字符

【leetcode 简单】 第九十题 字符串中的第一个唯一字符

2018年08月28日  | 移动技术网IT编程  | 我要评论

dhl广告歌,免费seo培训,焦恩俊老婆黄忆轩

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

案例:

s = "leetcode"
返回 0.

s = "loveleetcode",
返回 2.


class solution(object):
    def firstuniqchar(self, s):
        """
        :type s: str
        :rtype: int
        """
        s_len=len(s)
        for i in "abcdefghijklmnopqrstuvwxyz":
            start_f = s.find(i)
            if start_f != -1 and start_f == s.rfind(i):
                s_len = min(start_f,s_len)
        return s_len if s_len != len(s) else -1

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网