문자열 리스트 str_list에는 "u", "d", "l", "r" 네 개의 문자열이 여러 개 저장되어 있습니다. str_list에서 "l"과 "r" 중 먼저 나오는 문자열이 "l"이라면 해당 문자열을 기준으로 왼쪽에 있는 문자열들을 순서대로 담은 리스트를, 먼저 나오는 문자열이 "r"이라면 해당 문자열을 기준으로 오른쪽에 있는 문자열들을 순서대로 담은 리스트를 return하도록 solution 함수를 완성해주세요. "l"이나 "r"이 없다면 빈 리스트를 return합니다.
import java.util.*;
class Solution {
public ArrayList<String> solution(String[] str_list) {
ArrayList<String> answer = new ArrayList<>();
int index = 0;
String dic = " ";
while(index < str_list.length){
if(str_list[index].equals("r") || str_list[index].equals("l")){
dic = str_list[index];
break;
}
index++;
}
if(dic.equals("r")){
for(int i = index; i < str_list.length; i++){
answer.add(str_list[i]);
}
}else if(dic.equals("l")){
for(int i = 0; i < index; i++){
answer.add(str_list[i]);
}
}else{
return answer;
}
return answer;
}
}
import java.util.*;
class Solution {
public ArrayList<String> solution(String[] str_list) {
ArrayList<String> answer = new ArrayList<>();
int index = 0;
String dic = " ";
while(index < str_list.length){
if(str_list[index].equals("r") || str_list[index].equals("l")){
dic = str_list[index];
break;
}
index++;
}
if(dic.equals("r")){
for(int i = index+1; i < str_list.length; i++){
answer.add(str_list[i]);
}
}else if(dic.equals("l")){
for(int i = 0; i < index; i++){
answer.add(str_list[i]);
}
}
return answer;
}
}
import java.util.Arrays;
class Solution {
public String[] solution(String[] str_list) {
for (int i = 0; i < str_list.length; i++) {
if ("l".equals(str_list[i])) {
return Arrays.copyOfRange(str_list, 0, i);
} else if ("r".equals(str_list[i])) {
return Arrays.copyOfRange(str_list, i + 1, str_list.length);
}
}
return new String[0];
}
}
Arrays.copyOfRange(original, from, to)
배열을 시작 인덱스부터 마지막 인덱스까지 잘라서 복사
마지막 인덱스는 포함되지 않음