给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -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'))

最新文章

  1. Greedy:Linear world(POJ 2674)
  2. java基础学习总结——基础语法1
  3. UVA 11983 Weird Advertisement --线段树求矩形问题
  4. 元素查找(codevs 1230)
  5. ZOJ 3603 DP LCS
  6. Java基础知识强化之网络编程笔记19:Android网络通信之 HttpClient和传统Post、Get方式的区别
  7. vijos p1193 扫雷
  8. python面向对象(二)——类成员
  9. mybatis10 实现类代理对象开发
  10. hdu4010-Query on The Trees(lct分裂合并加值查询最大值)
  11. Android+NDK+CDT+eclipse+OPenGL ES编制和native调试
  12. CREELINKS平台_处理器CeAd资源使用说明(CeAd的配置与使用)
  13. tomcat8权限分离
  14. SpringBoot中出现的错误
  15. 学习pthreads,使用互斥量进行同步
  16. 开发过程中,ps要做的事情
  17. CC++语法::数组名退化(array decaying)
  18. JQuery官方学习资料(译):Data方法
  19. FPGA中iic总线上,应答ACK解析
  20. day21 正则表达式

热门文章

  1. docker中ubuntu源更新慢加速 换为国内源 Debian10源
  2. 第14讲 | HTTP协议:看个新闻原来这么麻烦
  3. [翻译] 预览 C# 10 的新东西
  4. Qt开发笔记:OpenSSL库介绍、windows上mingw32版本的OpenSSL编译模块化
  5. Pandas之:Pandas简洁教程
  6. GO语言练习---对切片进行排序
  7. 自动驾驶QNX,Linux,Autosar概述
  8. MindSpore平台系统类
  9. Linux学习笔记:用户与用户组
  10. 「题解」POI2005 AKC-Special Forces Manoeuvres