카테고리 없음
[백준/Baekjoon] 1181번 단어 정렬 (Java)
다콩잉
2023. 12. 3. 18:10
예제 입력1
13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours
예제 출력1
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate
답안
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = Integer.parseInt(scan.nextLine());
List<String> list = new ArrayList<>();
for(int i = 0; i < num; i++){
String x = scan.nextLine();
list.add(x);
}
Collections.sort(list, Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()));
Set<String> hashSet = new LinkedHashSet<>(list);
for (String element : hashSet) {
System.out.println(element);
}
}
}
https://manybean.tistory.com/entry/Java-Comparator
[Java] Comparator
Comparator를 사용하여 문자열 리스트를 다양한 방식으로 정렬할 수 있다. 1. 역순으로 정렬 Collections.sort(list, Comparator.reverseOrder()); 2. 문자열의 길이에 따라 정렬 Collections.sort(list, Comparator.comparingInt(
manybean.tistory.com
728x90