티스토리 뷰
728x90
SMALL
풀이
잘못된풀이1
function solution(s, n) {
let asc=[];
for(let i=0;i<s.length;i++){
asc.push(s.charCodeAt(i));
if(asc[i]===90||asc[i]===122){
asc[i]=asc[i]-26;
}
}
asc = asc.map(i=>{
if(i===32){
return i;
}
return i+n;
});
let answer="";
for(let i=0;i<asc.length;i++){
answer=answer+String.fromCharCode(asc[i]);
}
return answer;
}
잘못된풀이2
function solution(s, n) {
let result = "";
let num;
for(let i=0;i<s.length;i++){
if(s[i]===" "){
result+=" ";
}else{
if(s.charCodeAt(i)===90 || s.charCodeAt(i)===122){
num=-26;
}else{
num=0;
}
num+=s.charCodeAt(i)+n
result+=String.fromCharCode(num)
}
}
return result;
}
푸는데 시간이 오래걸렸다... 위에있는 풀이들이 정확도 테스트를 통과를 못했다...
function solution(s, n) {
let result = "";
for (let i=0;i<s.length;i++) {
if (s[i]==" "){
result+=" ";
}
else{
result += String.fromCharCode( (s.charCodeAt(i)>=97)
? (s.charCodeAt(i)+n-97)%26+97
: (s.charCodeAt(i)+n-65)%26+65 )
}
}
return result;
}
728x90
LIST
'코딩테스트' 카테고리의 다른 글
코딩테스트연습 > 연습문제 > 약수의 합 (0) | 2020.07.08 |
---|---|
코딩테스트연습 > 연습문제 > 소수찾기 (0) | 2020.07.06 |
코딩테스트연습 > 연습문제 > 문자열을 정수로 바꾸기 (0) | 2020.07.04 |
코딩테스트연습 > 연습문제 > 수박수박수박수박수박수? (0) | 2020.07.04 |
코딩테스트연습 > 연습문제 > 서울에서 김서방 찾기 (0) | 2020.07.03 |
댓글
공지사항