/*
* @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. LED相关

    P10 模组   分辨率32*16   尺寸320*160      间距 10mm P8 模组   分辨率32*16   尺寸256*128        间距 8mm P7.62 模组   分辨率 ...

  2. [翻译] DXPopover

    DXPopover A Popover mimic Facebook app popover using UIKit. 使用UIKit框架写了一个类似于Facebook的pop效果的动画. The c ...

  3. [EffectiveC++]item17:以独立语句将newed对象置入智能指针

    Store newed objects in smart pointers in standalone statements

  4. apt 安装 tomcat

    apt 安装 tomcat 直接使用 agt-get 安装 apt-get install tomcat7 # or apt-get install tomcat8 需要一段时间后就安装完成了. 安装 ...

  5. NSLayout​Constraint

    NSLayout​Constraint:定义了UI属性有参照关系的两个UI元素,一个元素的frame变化时,另一个元素变化的规则:依赖依附于最近的共同父视图,包含参照元素本身. 首要:确定变化参考物: ...

  6. Odoo中的模型在哪里

    Odoo 模型存在 Python 的模块之外, 在中间注册表那里. 对于这个注册表,可以通过self.env[<model name>]进入,例如, 通过res.partner 模型获取对 ...

  7. ionic和angularjs的区别?

    a.ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化HTML.css和js的性能,构建高效的应用程序,而且还可以用于构建sass和angularJS的优化 b.AngularJS ...

  8. 【webpack】config/index.js

    // see http://vuejs-templates.github.io/webpack for documentation. var path = require('path') module ...

  9. 13.56Mhz SI522兼容MFRC522的资料以及对比性能

    (13.56Mhz芯片) SI522是一颗专门替代MFRC522/FM17522,PIN对PIN 完全软硬件兼容.相对于MFRC522,SI522完全替换,不需要做任何更改,同时接受模式下功耗低10m ...

  10. CentOS7.2安装RabbitMQ笔记

    身为.NET程序员,用着宇宙级IDE,干什么事都变得越来越懒了,Windows操作系统在手,能通过桌面点点点的方式何必找其他罪受呢..于是RabbitMQ自然而然也就跑在Windows上了,说实话Wi ...