-
[프로그래머스 level.1] 내림차순정렬, 아스키코드 변환알고리즘 2023. 6. 21. 10:36
문자열내림차순 배열하기
function solution(s) { var answer = ''; let arr = [...s]; let newArr = arr.map(e => e.charCodeAt()).sort((a, b) => b - a) let listArr = newArr.map(e => String.fromCharCode(e)).join("") return listArr; }
내림차순 정렬은 오름차순정렬과는 a, b의 위치만 달라지면 된다 전에는 함수선언문? 으로 되어있었나 그래서 쓸 데 없이 길었는데 화살표 함수로 가독성 좀 높여봤음
아스키코드 변환은 계속해서 사용이 자주 되는데 헷갈림 왔다 갔다 다 좀 기억해두자
아니 근데 남의 풀이보니까 굳이 아스키코드 변환안해도 문자열 자체가 정렬했을 때 대문자랑 소문자랑 구분을 해서 정렬을 시키는데? 내가 2점 받은 이유가 있었음..ㄷㄷ
엄청나다
function solution(s) { return s .split("") .sort() .reverse() .join(""); }
'알고리즘' 카테고리의 다른 글
[프로그래머스 level.1] 3중반복문, 소수 판별식 (0) 2023.06.21 [프로그래머스 level.1] join(), split() (0) 2023.06.21 [프로그래머스 level.1] slice 스파르타 예습 (0) 2023.06.20 [프로그래머스 level.1] filter , includes (0) 2023.06.20 [프로그래머스 level.1] 이중반복문, 중복제거, 정렬 (0) 2023.06.20