Skip to content

Commit dffb0ac

Browse files
committed
Update 가장-큰-수.js
1 parent 88bd5a5 commit dffb0ac

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

level-2/가장-큰-수.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,33 @@ function solution(numbers) {
2222
numbers.map((el) => el + '').sort((a,b) => (b+a) - (a+b));
2323

2424
return stringNum[0] === '0' ? '0' : stringNum.join('');
25+
}
26+
27+
//완벽한 정답이 아닙니다.
28+
// 정답 3 - prove-ability
29+
function solution(numbers) {
30+
if(numbers.every(v => v === 0)) return "0";
31+
return numbers.sort((a, b) => {
32+
if(a === b) return 0;
33+
let stringA = a.toString(10), stringB = b.toString(10);
34+
if(stringA[0] === stringB[0]) {
35+
let aIndex = 1, bIndex = 1;
36+
while(true) {
37+
if(!stringA[aIndex]) --aIndex;
38+
if(!stringB[bIndex]) --bIndex;
39+
if(stringA[aIndex] === stringB[bIndex]) {
40+
aIndex++, bIndex++;
41+
continue;
42+
}
43+
if(stringA[aIndex] < stringB[bIndex]) return 1;
44+
else return -1;
45+
}
46+
}
47+
return stringB[0] - stringA[0]
48+
}).join("");
49+
}
50+
51+
// 정답 4 - prove-ability
52+
function solution(numbers) {
53+
return numbers.every(v => v === 0) ? "0" : numbers.map(v => v.toString(10)).sort((a,b) => (b+a) - (a+b)).join("");
2554
}

0 commit comments

Comments
 (0)