문제 출처
가운데 글자 가져오기
내가 푼 풀이
class Solution {
public String solution(String s) {
String answer = "";
int count = s.length();
if (count % 2 == 0) {
answer = s.substring(count/2 - 1, count/2 + 1);
} else {
answer = Character.toString(s.charAt(count/2));
}
return answer;
}
}
다른 사람들의 풀이
class StringExercise{
String getMiddle(String word){
return word.substring((word.length()-1) / 2, word.length()/2 + 1);
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args){
StringExercise se = new StringExercise();
System.out.println(se.getMiddle("power"));
}
}