LeetCode-First Unique Character in a String

来源:互联网 发布:360数据恢复免费版 编辑:程序博客网 时间:2024/04/28 05:10
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist,

return -1.


Examples:


Note: You may assume the string contain only lowercase letters.

题目含义:给一个字符串,找出字符串中第一个没有重复的字符,并返回他的索引,如果不存在则返回-1。可以假设字符串中只包含小写字母。


解题思路:由于题设字符串中只包含小写字母,所以可以建立一个26个元素的数组,统计字符串中各个字符出现的次数,然后再次遍历字符串,找到第一个出现次数只有1的元素,返回下标,若遍历完后未发现只出现1次的结果则返回-1.


1 0
原创粉丝点击