반응형


here is my answer
var firstUniqChar = function(s) {
//for getting the count of element, I think Map is necessary.
//so I declared the Map and used for loop for checking the count.
let a = new Map()
for(let i = 0; i < s.length; i++){
if(a.has(s[i])){
a.set(s[i], a.get(s[i])+1)
}else{
a.set(s[i], 1)
}
}
//after saving the count on Map,
//i need to find out the first factor which is on the first.
// so I use indexOf method, it is find the factor in first index.
//and also there is a possibility that doesn't have any 1 value,
//so I also use else statment, and block to occure the issue.
let result = 0
for(let i = 0; i< s.length; i++){
if(a.get(s[i]) === 1){
result = s.indexOf(s[i])
break
}else result = -1
}
return result
};
I know this code is not perfect, so I'm going to check others' code. Anyway, I solved one more algorithm by myself. That point really means a lot to me. I always struggle to improve myself. I think speed is not the most important thing; the most important thing is the direction.
+ I also change some code like this.
let result = 0
for(let i = 0; i< s.length; i++){
if(a.get(s[i]) === 1){
result = i
break
}else result= -1
}
return result
so there are some upgraded on memory and beats.
but not enough
반응형
'열정가득한 개발자의 이야기 > Javascript Algorithm' 카테고리의 다른 글
500. Keyboard Row (javascript) (0) | 2025.01.26 |
---|---|
leet code 409. Longest Palindrome (0) | 2025.01.18 |
345. Reverse Vowels of a String (0) | 2025.01.14 |
geeksforgeeks (Sum of elements in a matrix / basic) (2) | 2024.11.11 |
Palindrome Array (geeksforgeeks_Basic) (3) | 2024.11.07 |