字符串中的第一个唯一字符 python
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
s = "leetcode"
返回 0. s = "loveleetcode",
返回 2.
class solution:
def getChar(self,s):
setS = set(s)
dict = {}
for i in setS:
dict[i] = s.count(i) #该方法返回子字符串在字符串中出现的次数。
for index,value in enumerate(s):
print('j'+str(index)+' , k'+str(value))
if dict.get(value) == 1:
return index
return -1
def getChar2(self,s):
return min([s.find(c) for c in 'abcdefghijklmnopqrstuvwxyz' if s.count(c)==1] or [-1]) if __name__ == "__main__":
solution = solution()
index = solution.getChar('leetcode')
index2 = solution.getChar('loveleetcode')
print(index)
print(index2)
print(solution.getChar2('leetcode'))
print(solution.getChar2('loveleetcode'))
字符串中的第一个唯一字符 python的更多相关文章
- 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. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- Java实现 LeetCode 387 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...
- LeetCode初级算法之字符串:387 字符串中的第一个唯一字符
字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...
- 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
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...
- [Swift]LeetCode387. 字符串中的第一个唯一字符 | 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 ...
随机推荐
- python split方法
split方法说明: split方法通过指定分隔符对字符串进行切片,如果参数num有指定值,则分隔num+1个子字符串 split()方法语法: 1 str.split(str="&qu ...
- Locust入门
Locust入门 Locust是一款Python技术栈的开源的性能测试工具.Locust直译为蝗虫,寓意着它能产生蝗虫般成千上万的并发用户: Locust并不小众,从它Github的Star数量就 ...
- Docker学习(4) 守护式容器
守护式容器 stop - 等待信号 kill - 直接干死
- Go语言流程控制02--选择结构之switch
package main import "fmt" /* @星座诊所2(switch) 根据用户输入的出生月份猜测其星座: ·白羊(4) 金牛(5) 双子(6) 巨蟹(7) 狮子( ...
- MegEngine推理性能优化
MegEngine推理性能优化 MegEngine「训练推理一体化」的独特范式,通过静态图优化保证模型精度与训练时一致,无缝导入推理侧,再借助工业验证的高效卷积优化技术,打造深度学习推理侧极致加速方案 ...
- 智能物联网(AIoT,2020年)(下)
智能物联网(AIoT,2020年)(下) 12工业物联网是AIoT在工业领域第一战场 工业物联网分为感知.决策.执行,OS与软件是大脑+神经 13工业场景下一步如何使用AIoT 不止工业物联网:用人工 ...
- ARM Cortex-M嵌入式C基础编程(上)
ARM Cortex-M嵌入式C基础编程(上) ARM Cortex-M Embedded C Fundamentals/Tutorial -Aviral Mittal 此技术是关于从编写简单的嵌入式 ...
- 现代传感器的接口:中断驱动的ADC驱动程序
现代传感器的接口:中断驱动的ADC驱动程序 Interfacing with modern sensors: Interrupt driven ADC drivers 研究了如何编写一个阻塞的模数转换 ...
- JUC 并发编程--08,线程池,三大方法,七大参数,4种拒绝策略,代码演示
三大方法: //线程池核心线程数为n, 最大线程数为 n ExecutorService fixedThreadPool = Executors.newFixedThreadPool(n); 源码: ...
- .Net RabbitMQ实战指南——进阶(二)
持久化 持久化可以提高RabbitMQ的可靠性,防止异常情况下的数据丢失.RabbitMQ的持久化分为三个部分:交换器的持久化.队列的持久化和消息的持久化. 交换器的持久化通过声明队列时将durabl ...