题解 | #找出字符串中第一个只出现一次的字符#

找出字符串中第一个只出现一次的字符

http://www.nowcoder.com/questionTerminal/e896d0f82f1246a3aa7b232ce38029d4

str_a=input()
str_a_new=list(set(str_a))
str_a_new.sort(key=lambda x:(str_a.count(x),str_a.index(x)))    #对这个列表排序,先根据出现次数统计排序,若统计次数相同则根据出现的索引排序
# print(str_a_new)
for i in str_a_new:
    if str_a.count(i)==1:                        #若是1则必然是第一个出现的,因为我们根据索引排过序了
        print(i)
        break
    if str_a.count(i)>1:
        print(-1)
        break
全部评论

相关推荐

一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
ZywOo_求职版:谁问你了....
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务