/*
* @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. F5 IIS Log获取客户端源IP

    1.配置F5启用X-Forwarded-For方法: 1:Local Traffic-Profiles-Http-改"Insert XForwarded For"为Enable 2 ...

  2. Storm 实现滑动窗口计数和TopN排序

    计算top N words的topology, 用于比如trending topics or trending images on Twitter. 实现了滑动窗口计数和TopN排序, 比较有意思,  ...

  3. java获取中文拼音首字母

    import net.sourceforge.pinyin4j.PinyinHelper; public class PinyinHelperUtil { /** * 得到中文首字母(中国 -> ...

  4. git操作:

    error: Your local changes to the following files would be overwritten by merge: **/**/**.php Please, ...

  5. pat 5—1 求该日是该年的第几天

    哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 前面还是欠了一堆,慢慢补吧. 看了别人的博客才知道前面有一次圆面积的题,我上课是在干啥......... 打是打出来了,但是还是有几个蒙蔽的地方: ...

  6. poi导出excel出现本工作薄不能再使用其他新字体的解决方法

    最近使用POI处理EXCEL,当处理的单元格太多时,就会出现,本工作薄使用字体过多,不能再使用其他新的字体的是提示. 网上很多方法告诉我,要怎么修改excel文件,但是这个解决不了问题啊,难道让客户去 ...

  7. Java虚拟机10:Client模式和Server模式的区别

    部分商用虚拟机中,Java程序最初是通过解释器对.class文件进行解释执行的,当虚拟机发现某个方法或代码块运行地特别频繁的时候,就会把这些代码认定为热点代码Hot Spot Code(这也是我们使用 ...

  8. BZOJ4987:Tree(树形DP)

    Description 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一行两个正整数n,k,表示数的顶点数和需要 ...

  9. 【[USACO08JAN]haybale猜测Haybale Guessing】

    抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...

  10. nowcoder模拟赛

    R1 D1 普及组... T1/T2 咕 T3 链接:C 小A有一个只包含左右括号的字符串S.但他觉得这个字符串不够美观,因为它不是一个合法的括号串.一个合法的括号串是这样定义的: ()是合法的括号串 ...