leetcode笔记11 First Unique Character in a String

题目描述:

Given a string, find the first non-repeating character in it and return it‘s index. If it doesn‘t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

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

my answer:

主要思路:

首先把字符串变成字母列表,然后遍历列表字母建立一个计数字典和索引字典,再次遍历字母列表查找第一个出现的计数为1的字母的索引

出错点:

1 得到计数字典uni和索引字典ind之后,直接在uni里查找值为1的字母,这样出错的原因是,形成计数字典之后,字母顺序已经改变,所得字母并不是第一个出现的

2 忽略了不存在的情况,对于不存在的情况,即所有计数均大于1,这里使用的判断方法是对大于1的计数,如果最后得数等于list的长度则说明不存在

3 做逻辑运算时总是写成=,应该是双等号==

知识点:

for index,t in enumerate(list) 可同时遍历list 的值和索引

for k,v in uni.items()             可同时遍历字典uni的key 与 value

时间: 2025-01-14 14:39:14

leetcode笔记11 First Unique Character in a String的相关文章

[LeetCode] NO. 387 First Unique Character in a String

[题目] Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowe

【LeetCode】387. First Unique Character in a String

Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-character-in-a-string/ Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s =

LeetCode之387. First Unique Character in a String

-------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solution { public int firstUniqChar(String s) { Count c[]=new Count[26]; for(int i=0;i<s.length();i++){ int index=s.charAt(i)-'a'; if(c[index]==null) c[in

LeetCode(387)First Unique Character in a String

题目 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowerc

Leetcode-387 First Unique Character in a String

#387.   First Unique Character in a String Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You m

LeetCode_387. First Unique Character in a String

387. First Unique Character in a String Easy Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You

209 First Unique Character in a String

原题网址:https://www.lintcode.com/problem/first-unique-character-in-a-string/description 描述 给出一个字符串,找出第一个只出现一次的字符. 您在真实的面试中是否遇到过这个题?  是 样例 对于 "abaccdeff", 'b'为第一个只出现一次的字符. 标签 字符串处理 思路:用两个哈希表,一个统计每个字符出现次数,另一个统计字符的下标.遍历第一个哈希表,找到字符出现次数为1并且下标最小的return出去

Leetcode: First Unique Character in a String

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

[leetcode]387. First Unique Character in a String第一个不重复字母

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase