此演算法僅供參考,小菜基本上不懂高深的演算法,只能用最樸實的思想去表達。
//找出字串中第一個不重複的字元
// firstUniqueChar("vdctdvc"); --> t
function firstUniqueChar(str){
var str = str || "",
i = 0,
k = "",
_char = "",
charMap = {},
result = {name: "",index: str.length};
for(i=0;i
_char = str.charAt(i);
if(charMap[_char] != undefined){
charMap[_char] = -1;
}else{
charMap[_char] = i;
}
}
for(k in charMap){
if(charMap[k]
continue;
}
if(result.index>charMap[k]){
result.index = charMap[k];
result.name = k;
}
}
return result.name;
}
小夥伴們如有較好的思路,還請告之一下,不勝感激