题解 | #第一个只出现一次的字符#
第一个只出现一次的字符
https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return int整型
*/
func FirstNotRepeatingChar( str string ) int {
// write code here
if len(str) == 0 {
return -1
}
// 用切片来存储每个字符出现的次数, 字符的ascii码就是字符在切中的索引
repeatTimes := [256]int{0}
for i := 0; i < len(str); i++ {
repeatTimes[str[i]]++
}
for j := 0; j < len(str); j++ {
if repeatTimes[str[j]] == 1 {
return j
}
}
return -1
}