/*
* @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. Hypver-V中的快照

    应用快照选项的区别: 获取并应用快照:对虚拟机当前的状态进行快照,然后恢复到所选的快照状态 应用:不保存当前虚拟机的状态,直接将其恢复到所选的快照状态

  2. EF学习之CodeFirst(一)--创建Model

    一.创建Model 创建Model类有两种方式: 1.直接创建model 所有约束条件都以特性的方式写在model的属性上面,映射到数据库的table表名标识在class上,例如: [Table(&q ...

  3. Win10系统创建关机快捷方式和快捷键的方法,实现一键关机

    想不想关机变得更加简单?在Win10中总有些朋友想要快速的操作体验,所以关机这个经常使用的功能也被设置的更简单,下面小编要分享的是“Win10系统创建关机快捷方式和快捷键的方法”. 1.在win10下 ...

  4. 沉淀,再出发:python中的pandas包

    沉淀,再出发:python中的pandas包 一.前言 python中有很多的包,正是因为这些包工具才使得python能够如此强大,无论是在数据处理还是在web开发,python都发挥着重要的作用,下 ...

  5. There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:661) - skipping

    Could not fetch URL https://pypi.python.org/simple/xlrd/: There was a problem confirming the ssl cer ...

  6. HTTP协议图--HTTP 工作过程

                  HTTP请求响应模型 HTTP通信机制是在一次完整的 HTTP 通信过程中,客户端与服务器之间将完成下列7个步骤: 建立 TCP 连接 在HTTP工作开始之前,客户端首先要 ...

  7. 021.2 IO流——字节输出流

    内容:流的分类,文件写入(字节输出流),异常处理,获取一个文件夹下的特定文件集合 字节流的抽象基类:InputStream,OutputStream字符流的抽象基类:Reader,Writer由这四个 ...

  8. [T-ARA][한겨울의 숨바꼭질/숨바꼭질][深冬的捉迷藏/捉迷藏]

    歌词来源: 深冬的捉迷藏:http://music.163.com/#/song?id=28111190 捉迷藏:http://music.163.com/#/song?id=28111189 作曲 ...

  9. .Net Intelligencia.UrlRewriter 重定向参数中文支持配置方法

    在使用.Net 官方 Url重定向组件时,发现若原地址包含中文,如:http://localhost/首页.html 重定向为:http://localhost/index.aspx?id=首页  时 ...

  10. angularJs的工具方法2

    一.angular.isArray     判断是否是数组 var a = []; console.log(angular.isArray(a)); //判断参数里面的是否是数组 二.angular. ...