在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = "abaccdeff"
返回 "b"
s = ""
返回 " "
题解:
/**
* @param {string} s
* @return {character}
*/
var firstUniqChar = function(s) {
let dict = {}
let i=0
while(s[i]){
if(s[i] in dict){
dict[s[i]] = dict[s[i]]+1
}else{
dict[s[i]] = 1
}
i++
}
i=0
while(dict[s[i]]){
if(dict[s[i]]==1){
return s[i]
}
i++
}
return " "
};
链接:https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
本文介绍了一种算法,用于从字符串中找到第一个仅出现一次的字符。通过遍历字符串并使用哈希表记录每个字符的出现次数,进而确定目标字符。
506

被折叠的 条评论
为什么被折叠?



