https://www.acmicpc.net/problem/10988
Answer1
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String inputWord = "";
StringBuilder reversedWord = new StringBuilder();
boolean isPalindrome;
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
inputWord = br.readLine();
} catch(IOException e) {
System.err.println("ERROR : " + e.getMessage());
}
for(int i = inputWord.length() - 1; i >= 0; i--) {
reversedWord.append(inputWord.charAt(i));
}
isPalindrome = inputWord.contentEquals(reversedWord);
System.out.println((isPalindrome) ? 1 : 0);
}
}
Code Review1
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
inputWord = br.readLine();
} catch(IOException e) {
System.err.println("ERROR : " + e.getMessage());
}
`try-with-resources`로 try에 자원 객체를 받아서, try 작업이 끝나면 자동으로 자원을 반환하도록 한다.
`System.in`으로 사용자로부터 입력받은 데이터를 바이트 스트림으로 받는다.
`InpuStreamReader` 클래스로 입력받은 바이트 스트림을 문자 기반 스트림으로 변환한다.
`BufferedReader` 클래스로 문자 스트림을 읽는다.
for(int i = inputWord.length() - 1; i >= 0; i--) {
reversedWord.append(inputWord.charAt(i));
}
입력받은 단어의 길이를 `length()` 메서드로 가져와서, 반복문을 단어 인덱스 역순으로 진행한다.
`charAt(i)` 메서드로 입력받은 단어를 뒤에서부터 하나씩 `StringBuilder` 객체에 `append()` 메서드로 넣어 거꾸로 된 단어를 만든다.
isPalindrome = inputWord.contentEquals(reversedWord);
String 객체인 `inputWord`와 StringBuilder 객체인 `reverseWord`의 값이 같은지 비교하여 boolean 값(true or false)을 얻기 위해 `contentEquals()` 메서드를 사용한다.
System.out.println((isPalindrome) ? 1 : 0);
출력을 true는 1, false는 0으로 해야 하기 때문에 삼항연산자를 사용한다.
Answer2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
String inputWord = "";
StringBuilder reversedWord = new StringBuilder();
boolean isPalindrome;
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
inputWord = br.readLine();
} catch(IOException e) {
System.err.println("ERROR : " + e.getMessage());
}
reversedWord.append(inputWord).reverse();
isPalindrome = inputWord.equals(reversedWord.toString());
System.out.println((isPalindrome) ? 1 : 0);
}
}
Code Review2
중복 코드는 Code Review1 참고
reversedWord.append(inputWord).reverse();
StringBuilder 객체에 입력받은 단어 `inputWord`를 `append()` 메서드로 넣은 후,
`reverse()` 메서드를 사용하여 거꾸로 된 단어를 만들어 객체에 바로 반환한다.
isPalindrome = inputWord.equals(reversedWord.toString());
String 타입인 `inputWord`와 StringBuilder 객체인 `reversedWord`의 값이 같은지 비교하여 boolean 값(true or false)을 얻기 위해 StringBuilder 객체를 `toString()`으로 변환 후 `equals()` 메서드를 사용한다.
Result
두 가지 방법은 단어를 거꾸로 만드는 방법이 다른데 StringBuilder 객체의 `reverse()` 메서드를 보면
public AbstractStringBuilder reverse() {
byte[] val = this.value;
int count = this.count;
int n = count - 1;
if (isLatin1()) {
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
byte cj = val[j];
val[j] = val[k];
val[k] = cj;
}
} else {
StringUTF16.reverse(val, count);
}
return this;
}
결국 반복문으로 문자열을 뒤집기 때문에 성능상 크게 차이가 없을 것 같다.
이후에 사용한다면 가독성이 더 좋아 보이는 `reverse()` 메서드를 사용할 것 같다.
문자 비교 시 `equals()`와 `contentEquals()` 메서드를 사용했는데 차이점을 추후에 정리해야겠다.
2024.04.04 - [Study(JAVA)] - [Study(JAVA)] String equals() vs contentEquals()