leetcode-easy-string-387 First Unique Character in a String
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的更多相关文章
- [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 ...
- 【leetcode❤python】387. First Unique Character in a String
#-*- coding: UTF-8 -*- class Solution(object): def firstUniqChar(self, s): s=s.lower() ...
- 【LeetCode】387. First Unique Character in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 4.(基础)tornado应用安全与认证
这一节我们介绍应用安全与认证,其实中间省略了一个数据库.对于tornado来说,读取数据库的数据,性能的瓶颈还是在数据库上面.关于数据库,我在<>中介绍了sqlalchemy,这是一个工业 ...
- CentOS 7 安装 gcc 4.1.2
CentOS 7 安装 gcc 4.1.2 0. 参考 在centOS7.2上编译gcc4.1.2 1. 安装了编译所需 yum groupinstall "Development Tool ...
- 个人小应用服务器安装搭建,HP 360p Gen9 使用winpe安装centos[一]
以前用aws的时候使用的ec2, 里面可选的windows server搭配umbraco的cms做了自己的个人网站,主要是当年项目需要,使用aws,我也办了国际币种卡,在组里各种联系亚马逊开服务,后 ...
- Stopwatch简单时间检测
public ActionResult Index() { Stopwatch sw = new Stopwatch(); //实例化一个对象 sw.Start(); //开始计算 int[] a = ...
- 多项式FFT/NTT模板(含乘法/逆元/log/exp/求导/积分/快速幂)
自己整理出来的模板 存在的问题: 1.多项式求逆常数过大(尤其是浮点数FFT) 2.log只支持f[0]=1的情况,exp只支持f[0]=0的情况 有待进一步修改和完善 FFT: #include&l ...
- HDU - 6589 Sequence (生成函数+NTT)
题目链接 设序列a的生成函数$\large f(x)=\sum\limits_{i=0}^{n-1}a_ix^i$,则操作1,2,3分别对应将$f(x)$乘上$\Large\frac{1}{1-x}, ...
- 关于css阴影和浮动
盒子阴影box-shadow box-shadow:0 0 1px #000 inset; 水平 垂直 模糊 颜色 : [1] inset代表框内阴影,不加inset代表框外阴影 [2]第1个 ...
- Python 模块 Ⅱ
搜索路径 当你导入一个模块,Python 解析器对模块位置的搜索顺序是: 1.当前目录 2.如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录. 3.如果都 ...
- django开发博客01-页面展示数据库中的数据
1.首先在views.py中引入models.py的 Category这个类 然后在函数中(blog)写执行逻辑 categorys 返回的对象是是一个list"<QuerySet [ ...
- 暑假集训 #2 div1 I - Lada Priora 精度处理
I - Lada Priora Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...