问题描述:

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

案例:

s = "leetcode"
返回 0. s = "loveleetcode",
返回 2.

方法:

 class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
x = "abcdefghijklmnopqrstuvwxyz"
res = [] #res=[s.index[i] for i in x if s.count(i) == 1]
for i in x:
if i in s and s.count(i) == 1:
res.append(s.index(i))
if len(res):
return min(res)
return -1

官方:

 class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
""" if s == '':
return -1 l = len(s)
tmp = l
for i in 'abcdefghijklmnopqrstuvwxyz':
start = s.find(i)
end = s.rfind(i)
if start != -1 and start == end:
tmp = min(tmp, start)
if tmp < l:
return tmp
else:
return -1

2018-09-28 16:27:05

LeetCode--387--字符串中的第一个唯一字符的更多相关文章

  1. 前端与算法 leetcode 387. 字符串中的第一个唯一字符

    目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  2. Java实现 LeetCode 387 字符串中的第一个唯一字符

    387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...

  3. LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String

    题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...

  4. LeetCode初级算法之字符串:387 字符串中的第一个唯一字符

    字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...

  5. 【LeetCode】字符串中的第一个唯一字符

    [问题]给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. s = "leetcode" 返回 . s = "loveleetcode ...

  6. 力扣(LeetCode)字符串中的第一个唯一字符 个人题解

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...

  7. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  8. LeetCode初级算法--字符串02:字符串中的第一个唯一字符

    LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...

  9. leecode刷题(13) -- 字符串中的第一个唯一字符

    leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...

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

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

随机推荐

  1. shell脚本之 给PNG图片添加后缀@3x

    1,给png图片加上后缀@3x #!/bin/sh #root_src=$(dirname $(PWD)) #echo ${root_src} image_path=${root_src}/image ...

  2. 从Git上导入Maven 项目到Eclipse

    Note: 经验之谈,操作过程中有不懂的地方可以留言问. Step: Open the Eclipse: --1.File>>Import>>Git:Project from ...

  3. Junit中AssertTrue的使用

    assertTrue public static void assertTrue(String message, boolean condition) Asserts that a condition ...

  4. linux内核中侧async_tx是什么?

    答: 异步内存传输api(asynchronous memory transfer/transform API),这是一种api,用来为应用提供操作DMA的API. 下图是async_tx在架构中所处 ...

  5. 再谈 iptables 防火墙的 指令配置

    手机上使用localhost为什么不能访问? 电脑上使用localhost 访问主页的原理是 电脑上有网站资源和服务器相关程序apache等的支持, 同时在 电脑的hosts文件中 有 127.0.0 ...

  6. 【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑

    题意 给出一张无向图,求出恰巧经过n条边的最短路. 题解 考虑先离散化,那么点的个数只会有202个最多.于是复杂度里面就可以有一个\(n^3\).考虑构造矩阵\(d^1\)表示经过一条边的最短路,那么 ...

  7. 题解——Codeforces Round #508 (Div. 2) T2 (构造)

    按照题意构造集合即可 注意无解情况的判断 #include <cstdio> #include <algorithm> #include <cstring> #in ...

  8. HDU 4656 Evaluation(MTT)

    题意 \(x_k=bc^{2k}+d\) \(\displaystyle F(x)=\sum_{i=0}^{n-1}a_ix^i\) 给定 \(\{a\},b,c,d,n\) ,求 \(F(x_0), ...

  9. 第一章(欢迎进入node.js世界)

    本章内容 1:Node.js是什么 2:服务器端javascript 3:node的异步和事件触发本质 4:node为谁而生 5:node程序示例 1.1 node.js他的首次亮相是在2009年,非 ...

  10. HDU 5445 Food Problem(多重背包+二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5445 题意:现在你要为运动会提供食物,总共需要提供P能量的食物,现在有n种食物,每种食物能提供 t 能量,体积为 ...