m0_59765747 2021-06-28 11:51 采纳率: 100%
浏览 1077
已采纳

编写一个Python程序,在字符串中找出第一个只出现一次的字符,要求时间复杂度不超过O(n)。

1. 编写一个Python程序,在字符串中找出第一个只出现一次的字符,要求时间复杂度不超过O(n)。

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2021-06-28 11:57
    关注

    如有帮助,请采纳。点击我回答右上角【采纳】按钮。

    def methond(str):
        counts = {}
        order = []
        for s in str:
            if s in counts:
                counts[s] +=1
            else:
                counts[s] = 1
                order.append(s)
        for i in order:
            if counts[i] == 1:
                return i
    if _name_=='_main_':
        s=methond('sadfasdfsdwe')
        print(s)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?