/*
* @lc app=leetcode.cn id=387 lang=c
*
* [387] 字符串中的第一个唯一字符
*
* https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/
*
* algorithms
* Easy (36.55%)
* Total Accepted: 23.7K
* Total Submissions: 64.7K
* Testcase Example: '"leetcode"'
*
* 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
*
* 案例:
*
*
* s = "leetcode"
* 返回 0.
*
* s = "loveleetcode",
* 返回 2.
*
*
*
*
* 注意事项:您可以假定该字符串只包含小写字母。
*
*/
int firstUniqChar(char* s) {
int count[];
for(int i=;i<;i++){
count[i]=;
}
for(int i=;i<strlen(s);i++){
count[s[i]-'a']++;
}
for(int i=;i<strlen(s);i++){
if(count[s[i]-'a']==){
return i;
}
}
return -;
}

这里和之前一个题类似,就是设置一个字母表,0代表a,以此类推,初始化都为0。

然后在s中逐一的计数,统计各个字母的次数。

然后再从头循环,如果这个字母的次数为一的话,直接return当前的位置。

------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=387 lang=python3
#
# [387] 字符串中的第一个唯一字符
#
# https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/
#
# algorithms
# Easy (36.55%)
# Total Accepted: 23.7K
# Total Submissions: 64.7K
# Testcase Example: '"leetcode"'
#
# 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
#
# 案例:
#
#
# s = "leetcode"
# 返回 0.
#
# s = "loveleetcode",
# 返回 2.
#
#
#
#
# 注意事项:您可以假定该字符串只包含小写字母。
#
# class Solution(object):
def firstUniqChar(self, s):
list1=[]
list2=[]
if len(s)==1:
return 0
elif len(s)==2:
if s[0]==s[1]:
return -1
else:
return 0
elif len(s)!=0:
s1="".join(list(set(s)))
for i in s1:
if s.count(i)!=1:
continue
else:
list1.append(i)
if len(list1)==s1 or len(list1)==0:
return -1
else:
for j in list1:
list2.append(s.index(j))
a=sorted(list2)
return a[0]
else:
return -1

Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符的更多相关文章

  1. Leecode刷题之旅-C语言/python-344反转字符串

    /* * @lc app=leetcode.cn id=344 lang=c * * [344] 反转字符串 * * https://leetcode-cn.com/problems/reverse- ...

  2. Leecode刷题之旅-C语言/python-26.删除数组中的重复项

    /* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...

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

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

  4. Leecode刷题之旅-C语言/python-14.最长公共前缀

    /* * @lc app=leetcode.cn id=14 lang=c * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-c ...

  5. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  6. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  7. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  8. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  9. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

随机推荐

  1. iOS设计模式 - 桥接

    iOS设计模式 - 桥接 示意图 说明 1. 桥接模式为把抽象层次结构从实现中分离出来,使其可以独立变更,抽象层定义了供客户端使用的上层抽象接口,实现层次结构定义了供抽象层次使用的底层接口,实现类的引 ...

  2. Java并发基础(上)——Thread

    并发编程可以使我们将程序划分为多个分离的,独立运行的任务.通过多线程机制,这些独立任务都将由执行线程来驱动.在使用线程时,CPU将轮流给每个任务分配占用时间,每个任务都觉得自己在占用CPU,但实际上C ...

  3. Python学习---Python安装与基础1205

    1.0. 安装 1.1.1. 下载 官网下载地址:https://www.python.org/downloads/release/python-352/ 1.1.2. 配置环境变量 因为在安装的时候 ...

  4. 如何在Code First、Database First和Model First之间选择

    Code First.Database First和Model First基本图解: 1)Database First: 如果数据库已经存在,可以使用VS自动生成数据模型,已经相关的edmx信息 2) ...

  5. Alpha Scrum2

    Alpha Scrum2 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:督促和监督团队进度.协调组内合作,前端页面编写,博客发布 林书浩.陈远 ...

  6. JQuery的getJSON函数跨域

    由于一开始看到“$”等类似符号就头晕,所以注定与PHP和JQuery无缘了,不过自己用JavaScript可不代表其他人也得用,这不,麻烦到了... 两个网站:A.B A站点提供了一个重要的API,由 ...

  7. PhoneGap检测设备网络连接情况

    一.网络连接状态列表 Phonegap 网络连接通过 navigator.network.connection.type 来获取,一般有一下几种状态 1. Connection.UNKNOWN     ...

  8. ThinkPHP5入门(二)----控制器篇

    一.控制器访问 1.命名空间 命名空间与目录路径对应. 如:路径位置为:application/index/controller/Index.php 其文件的命名空间应为:app\index\cont ...

  9. Linux BLE 基于 树莓派

    1.参考资料:Linux(RaspberryPi)上使用BLE低功耗蓝牙 使用bluez协议栈方法有用 2.Linux下Bluez的编程实现 3.和菜鸟一起学linux之bluez学习记录2 4.BL ...

  10. 开源Webshell利用工具——Altman

    开源Webshell利用工具--Altman keepwn @ 工具 2014-06-04 共 6114 人围观,发现 43 个不明物体收藏该文 Altman,the webshell tool,自己 ...