Skip to content

Commit c6c42c2

Browse files
committed
[PGS] 문자열 내 마음대로 정렬하기 / Level 1 / 30분 / 성공
1 parent 5335f32 commit c6c42c2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
/*
4+
1. n번쨰 글자를 기준으로 오름차순 정렬.
5+
2. 만약 같은 경우 사전순으로 앞선 문자열이 앞으로 와야 한다.
6+
풀이 시간: 30분
7+
*/
8+
class Solution {
9+
public String[] solution(String[] strings, int n) {
10+
// n번쨰 기준으로 정렬하기 전에 전체 정렬
11+
Arrays.sort(strings);
12+
13+
// n번째 글자를 기준으로 오름차순 정렬
14+
Arrays.sort(strings, new Comparator<String>() {
15+
@Override
16+
public int compare(String s1, String s2) {
17+
int index = n;
18+
char c1 = s1.charAt(index);
19+
char c2 = s2.charAt(index);
20+
return Character.compare(c1, c2);
21+
}
22+
});
23+
return strings;
24+
}
25+
}

0 commit comments

Comments
 (0)