내 정답
import java.util.*;
class Main{
public static String solution(String input, int N){
String result ="";
for(int j=input.length()-1;j>=0;j--){
result+=input.charAt(j);
}
return result;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
int N = kb.nextInt();
String[] arr = new String[N];
for(int i=0;i<N;i++){
String input = kb.next();
arr[i] = input;
}
for(int i=0;i<N;i++){
System.out.println(solution(arr[i],N));
}
}
}
강의 정답 1
import java.util.*;
class Main{
pulic ArrayList<String> solution (int n, String[] str){
ArrayLsit<String> answer = new ArrayList<>();
for(String x: str){
String tmp = new StringBuilder(x).reverse().toString();
answer.add(tmp);
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
String[] str = new Strint[n];
for(String x : T.solution(n,str)){
System.out.println(x);
}
}
}
StringBuilder이해하기 추가
강의 정답 2
import java.util.*;
public class p4 {
public static ArrayList<String> solution(int n, String[] str){
ArrayList<String> answer = new ArrayList<>();
for(String x : str) {
char[] s = x.toCharArray();
int lt =0, rt=x.length()-1;
while(lt<rt) {
char tmp=s[lt];
s[lt]=s[rt];
s[rt]=tmp;
lt++;
rt--;
}
String tmp = String.valueOf(s);
answer.add(tmp);
}
return answer;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
String[] str = new String[n];
for(int i=0;i<n;i++) {
String in = kb.next();
str[i]=in;
}
for(String x : solution(n,str)) {
System.out.println(x);
}
}
}