문제는 백준 1181번 단어정렬
조건은 길이가 짧은것부터 정렬 길이가 같으면 사전 순으로 정렬
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.io.*;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
try {
int num = Integer.parseInt(br.readLine());
String[] strings = new String[num];
for (int i = 0; i < num; i++) {
strings[i] = br.readLine();
}
Arrays.stream(strings).distinct()
.sorted(
new Comparator<String>() {
@Override
public int compare(String string1, String string2) {
if(string1.length() != string2.length()) {
int num = string1.length() - string2.length();
return num;
}
else return string1.compareTo(string2);
}
}
)
.forEach(System.out::println);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
'내가 겪은 정보' 카테고리의 다른 글
1859. Sorting the Sentence 자바 코드 (0) | 2023.05.02 |
---|---|
1365. How Many Numbers Are Smaller Than the Current Number 자바 (0) | 2023.04.28 |
org.springframework.test.web.servlet.ResultActions 숫자 앞 소문자 리턴 현상 (0) | 2023.03.02 |
mybatis java.sql.SQLException: 부적합한 열 유형: 1111 에러 (0) | 2023.01.26 |
인텔리제이 단축키명, 역할 2023.01.11 추가 (0) | 2023.01.11 |