728x90
StringBuilder클래스의 compareTo메소드 활용
이것의 존재를 몰라서 밑에 방식처럼 StringBuilder 전체 index를 for문으로 돌려가면서 해결함
근데 만약 문자의 길이가 크다면 밑에 방식이 더 빠를 것
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Solution {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
StringBuilder firstString = new StringBuilder(br.readLine().toLowerCase());
StringBuilder secondString= new StringBuilder(br.readLine().toLowerCase());
int compareToResult = firstString.compareTo(secondString);
int result = 0;
if(compareToResult<0) result=-1;
else if(compareToResult>0) result=1;
bw.write(String.valueOf(result));
bw.flush();
}
}
|
cs |
Character클래스의 compare(A,B) 메소드 활용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Solution {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
StringBuilder firstString = new StringBuilder(br.readLine().toLowerCase());
StringBuilder secondString= new StringBuilder(br.readLine().toLowerCase());
for(int i=0; i<firstString.length(); i++) {
char firstChar= firstString.charAt(i);
char secondChar = secondString.charAt(i);
if(Character.compare(firstChar, secondChar)>0) {
System.out.println("1");
return;
}
else if(Character.compare(firstChar, secondChar)<0) {
System.out.println("-1");
return;
}
}
System.out.println(0);
}
}
|
cs |
'알고리즘 풀이 > CodeForces' 카테고리의 다른 글
[CodeForces][JAVA] 50A - Domino piling (0) | 2021.06.21 |
---|---|
[CodeForces][JAVA] 263A - Beautiful Matrix (0) | 2021.06.21 |
[CodeForces][JAVA] 118A - String Task (0) | 2021.06.20 |
[CodeForces][JAVA] 282A. Bit++ (0) | 2021.06.20 |
[CodeForces][JAVA] 158A.Next Round (0) | 2021.06.20 |