mycode  24.42%

class Solution:
def firstUniqChar(self, s: str) -> int:
dic = {}
for i in range(len(s)):
dic[s[i]] = dic.get(s[i],0) + 1
for i,val in dic.items():
if val == 1:
return s.index(i)
return -1

参考

class Solution:
def firstUniqChar(self, s: str) -> int:
# # two-pass
# ctr = collections.Counter(s)
# for i,v in enumerate(s):
# if ctr[v] == 1:
# return i
# return -1 letters='abcdefghijklmnopqrstuvwxyz'
index=[s.index(l) for l in letters if s.count(l) == 1]
return min(index) if len(index) > 0 else -1

leetcode-easy-string-387 First Unique Character in a String的更多相关文章

  1. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  2. 【leetcode❤python】387. First Unique Character in a String

    #-*- coding: UTF-8 -*- class Solution(object):    def firstUniqChar(self, s):        s=s.lower()     ...

  3. 【LeetCode】387. First Unique Character in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  5. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  6. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  7. LeetCode 387. First Unique Character in a String

    Problem: Given a string, find the first non-repeating character in it and return it's index. If it d ...

  8. 18. leetcode 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  9. [leetcode]387. First Unique Character in a String第一个不重复字母

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  10. Java [Leetcode 387]First Unique Character in a String

    题目描述: Given a string, find the first non-repeating character in it and return it's index. If it does ...

随机推荐

  1. 09 Python两种创建类的方式

    第一种比较普遍的方式: class Work(): def __init__(self,name): self.name = name w = Work('well woker') 这样就简单创建了一 ...

  2. shell 删除文本中的重复行

    三种常见方法:第一,用sort+uniq,注意,单纯uniq是不行的. shell> sort -k2n file | uniq > a.out 这里我做了个简单的测试,当file中的重复 ...

  3. new和delete用法小结

    在C语言中是利用库函数 malloc 和 free 函数来分配和撤销内存的.C++提供了较简便而功能较强的运算符 new 和 delete 来取代 malloc 和 free 函数. new 和 de ...

  4. 如何在当前目录下打开Windows cmd?

    在当前目录下,按Alt + D (全选当前目录),然后输入 cmd 再按回车 Enter .

  5. p1364 医院设置 题解

    思路:floyd 很普通的思路. 先用floyd求出两个边之间的距离,然后乘以人数. 代码: #include<iostream> #include<cstring> usin ...

  6. C++类模板——博客链接

    https://www.jianshu.com/p/70ca94872418 C++类模板,你看我就够了 值得学习~

  7. 【转】linux中fork()函数详解

    原文链接:http://blog.csdn.net/jason314/article/details/5640969#comments 总结:面宝P268 fork()的意思是进程从这里开始分叉,分成 ...

  8. [洛谷P4072] SDOI2016 征途

    问题描述 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路 ...

  9. 【leetcode】1272. Remove Interval

    题目如下: Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the ...

  10. react -搭建服务-2

    export const DEFAULT_TITLE = "你好"; // export const PRODUCT_SERVER_URL = "http://10.10 ...