LeetCode--387--字符串中的第一个唯一字符
问题描述:
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -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--字符串中的第一个唯一字符的更多相关文章
- 前端与算法 leetcode 387. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- Java实现 LeetCode 387 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...
- LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...
- LeetCode初级算法之字符串:387 字符串中的第一个唯一字符
字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...
- 【LeetCode】字符串中的第一个唯一字符
[问题]给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. s = "leetcode" 返回 . s = "loveleetcode ...
- 力扣(LeetCode)字符串中的第一个唯一字符 个人题解
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- LeetCode初级算法--字符串02:字符串中的第一个唯一字符
LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...
- leecode刷题(13) -- 字符串中的第一个唯一字符
leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
随机推荐
- rsync: read error: Connection reset by peer (104)
Centos7 rsync守护进程上传文件失败 [root@nfs ~]# rsync -avz /etc rsync_backup@172.16.1.41::backupsending inc ...
- MVC 自定义特性(验证用户登录session是否已经过期)
新建一个类 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] publ ...
- sql 之 INSERT IGNORE
INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以保留 ...
- P3338 [ZJOI2014]力
思路 颓柿子的题目 要求求这样的一个式子 \[ F_j=\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2} ...
- P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)
思路 考虑题目要求求出最小的第k+1大的边权,想到二分答案 然后二分第k+1大的边权wx 把所有边权<=wx的边权变为0,边权>wx的边权变为0,找出最短路之后,如果dis[T]<= ...
- MPI之聚合通信-Scatter,Gather,Allgather
转自:https://blog.csdn.net/sinat_22336563/article/details/70229243 参考:http://mpitutorial.com/tutorials ...
- Redis,Memcache比较
简单比较: Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储.memcache只支持简单的K/V类型数据, 不过memcache可以缓存其他东西如图片, ...
- pgAdmin的数据恢复
DOC 本地添加server 1.设置备份.恢复的exe路径.一般在pgAdmin的安装路径下可以找到 2.恢复restore,备份backup
- tomcat启动出现Preparing launch delegate,一直卡在100%
本地启动项目时,Tomcat一直停留在, Starting Tomcat V8.0 Server at localhost Preparing launch delegate... 百度可得 ...
- 创建react项目
npm搭建React项目 React官网提供最简便的方法是使用create-react-app npx create-react-app my-app cd my-app npm start 也可以自 ...