LeetCode 387 字符串中的第一个唯一字符【简单】
2018-05-30 20:26
393 查看
387. 字符串中的第一个唯一字符
题目描述:
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
案例:s = "leetcode" 返回 0. s = "loveleetcode", 返回 2.注意事项:您可以假定该字符串只包含小写字母。个人解答如下,没有写注释,看起来有点乱...
104 / 104 个通过测试用例 | 状态:通过 |
执行用时:76 ms |
class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ list1=[] list2=[] if len(s)==1: return 0 elif len(s)==2: if s[0]==s[1]: return -1 else: return 0 elif len(s)!=0: s1="".join(list(set(s))) for i in s1: if s.count(i)!=1: continue else: list1.append(i) if len(list1)==s1 or len(list1)==0: return -1 else: for j in list1: list2.append(s.index(j)) a=sorted(list2) return a[0] else: return -1
阅读更多
相关文章推荐
- 【leetcode】 字符串中的第一个唯一字符
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
- [LeetCode] 387. First Unique Character in a String 字符串的第一个唯一字符
- leetcode-字符串中的第一个唯一字符
- LeetCode number387 字符串第一个没有相同的字符
- Leetcode 387. First Unique Character in a String 第一个唯一字符 解题报告
- First Unique Character in a String(字符串中的第一个唯一字符)
- LeetCode:找出字符串中第一个只出现一次的字符
- 【初级算法】14. 字符串中的第一个唯一字符
- [LeetCode] First Unique Character in a String 字符串第一个不同字符
- 从第一个字符串中删除第二个字符串中的所有字符
- JavaScript实现找出字符串中第一个不重复的字符
- 在字符串中找出第一个只出现一次的字符。经典C语言例题
- 找出字符串中第一个只出现一次的字符
- 在字符串中找出第一个只出现一次的字符
- 算法学习七----在一个字符串中找到第一个只出现一次的字符
- [字符串]第一个不重复的字符
- Leetcode712. 计算两个字符串删除任意字符后使二者相等的最小删除字符和
- leetcode_345. Reverse Vowels of a String 转换字符串中的元音字符,list类型数据转换成str类型,join()函数
- 统计字符串中第一个出现次数为1的字符