字符串中的第一个惟一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。若是不存在,则返回 -1。数组
案例:code
s = "leetcode" 返回 0. s = "loveleetcode", 返回 2.
注意事项:您能够假定该字符串只包含小写字母。索引
用count数组记录每一个字符出现次数。ip
/** * @param {string} s * @return {number} */ var firstUniqChar = function(s) { let index = 0; let count = []; for(let i = 0; i < s.length; i++){ count[i] = 1; } for(let i = 0; i < s.length; i++){ if(count[i] == 1){ for(let j = i + 1; j < s.length; j++){ if(s[i] == s[j]){ count[i]++; count[j]++; } } } if(count[i] == 1){ return i; } } return -1; };