티스토리 뷰
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(in.readLine());
String[] array = new String[count];
for(int i=0; i<count; ++i) {
array[i] = in.readLine();
}
Arrays.sort(array, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int lengthOfFirst = o1.length();
int lengthOfSecond = o2.length();
if(lengthOfFirst > lengthOfSecond) {
return 1;
} else {
if(lengthOfFirst == lengthOfSecond) {
return o1.compareTo(o2);
} else {
return -1;
}
}
}
});
System.out.println(array[0]);
for(int i=1; i<count; ++i) {
if(array[i - 1].equals(array[i])) {
continue;
}
System.out.println(array[i]);
}
}
}
재정의한 compare를 어떻게 구현해야 하는지 아직도 잘 모르겠다...
또 생각했던 것과 정반대로 코드를 작성했더니 맞았다.
Java 코드와 똑같이 Swift 코드를 작성했는데 Swift가 100ms 정도 더 느렸다. ???
let count = Int(readLine()!)!
var array = [String]()
for _ in 0..<count {
array.append(readLine()!)
}
array.sort { (first, second) -> Bool in
let lengthOfFirst = first.count
let lengthOfSecond = second.count
if lengthOfFirst == lengthOfSecond {
return first < second
}
return lengthOfFirst < lengthOfSecond
}
print(array.first!)
for i in 1..<count {
if array[i - 1] == array[i] {
continue
}
print(array[i])
}
'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글
1850번 '최대공약수' (0) | 2018.08.10 |
---|---|
13241번 '최소공배수' (0) | 2018.08.10 |
1260번 'DFS와 BFS' (0) | 2018.08.09 |
11004번 'K번째 수' (0) | 2018.08.07 |
11652번 '카드' (0) | 2018.08.07 |
댓글