Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/*
* @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 字符串中的第一个唯一字符的更多相关文章
- Leecode刷题之旅-C语言/python-344反转字符串
/* * @lc app=leetcode.cn id=344 lang=c * * [344] 反转字符串 * * https://leetcode-cn.com/problems/reverse- ...
- Leecode刷题之旅-C语言/python-26.删除数组中的重复项
/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...
- leecode刷题(13) -- 字符串中的第一个唯一字符
leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...
- Leecode刷题之旅-C语言/python-14.最长公共前缀
/* * @lc app=leetcode.cn id=14 lang=c * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-c ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
随机推荐
- Python 词频统计
利用Python做一个词频统计 GitHub地址:FightingBob [Give me a star , thanks.] 词频统计 对纯英语的文本文件[Eg: 瓦尔登湖(英文版).txt]的英文 ...
- 如何提高Ajax性能
1.适当使用缓存机制 2.使用CDN内容分发来访问Jquery脚本: (1)自己公司架设CDN服务器 (2)使用第三方公司的,比如微软,谷歌等公司的CDN,但有时候不太靠谱 3.JS/CSS文件的打包 ...
- Python初学者第十二天 购物车程序小作业
12day 作业题目: 购物车程序 作业需求: 数据结构: goods = [ {"name": "电脑", "price": 1999}, ...
- Python学习---装饰器的学习1210
装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包 [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前 ...
- 乘风破浪:LeetCode真题_016_3Sum Closest
乘风破浪:LeetCode真题_016_3Sum Closest 一.前言 这一次,问题又升级了,寻找的是三个数之和最靠近的某个数,这是非常让人难以思考的,需要把三个数相加之后和最后给的目标 ...
- 最近编译tolua_runtime安卓编译错误
编译时出现以下错误In file included from lj_ffrecord.c:859:0: lj_recdef.h:224:1: error: ‘recff_rawlen’ undecla ...
- 离散对数&&大步小步算法及扩展
bsgs algorithm ax≡b(mod n) 大步小步算法,这个算法有一定的局限性,只有当gcd(a,m)=1时才可以用 原理 此处讨论n为素数的时候. ax≡b(mod n)(n为素数) 由 ...
- mvvm模型
- 用Web技术开发客户端(一)
http://www.cnblogs.com/lefan/archive/2012/12/27/2836400.html 范怀宇(@duguguiyu)分享了<豌豆荚2.0重构时遇到的坑> ...
- Mac版Mysql Workbench不展示Schema
Mac版的Mysql Workbench会不展示Schema,如下图 操作如下 cd ~/Library/Application\ Support/MySQL/Workbench/ rm wb_sta ...