387. 字符串中的第一个唯一字符.py-----leetcode刷题(python解题)
  xaeiTka4h8LY 18天前 17 0

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

案例:

s = "leetcode"

返回 0.

s = "loveleetcode",

返回 2.

注意事项:您可以假定该字符串只包含小写字母。

解答

leetcode解题

 

 

import collections

class Solution1(object):  # 方法一
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        for a,i in enumerate(s):
            aa = s.replace(i,"",1)
            if i not in aa:
                return a


class Solution2(object):  # 方法二
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        index=0
        count = collections.Counter(s)
        for i in s:
            if count[i]==1:
                return index
            else:
                index+=1
        return -1
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 18天前 0

暂无评论

推荐阅读
  xaeiTka4h8LY   2024年05月17日   34   0   0 字符串
xaeiTka4h8LY