728x90
300x250
ArrayList를 이용해서 할수있을까 보다가.. LinkedList이 답이였다...
ArrayList는.. set()함수가 있지만 이거는 그냥 원소를 바꿔치기 하는거여서
LinkedList는 특정 index에 add를 하면 뒤에것들은 자동으로 밀린다.
그리고 LinkedList는 삽입,삭제가 빈번할 때 사용하면 좋고,
ArrayList는 배열의 인덱스를 기반으로하기에 조회가 빈번할 때 사용하면 좋다.
이 문제의 포인트는 delete할 때 인듯!
import java.util.*;
public class Solution {
public static int N,M; //암호문의 길이, 명령어의 갯수
public static LinkedList<Integer> list;
public static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
for (int tc = 1; tc <= 10; tc++) {
list = new LinkedList<>();
list.add(999); // index 1부터 맞춰주기 위함
N = sc.nextInt();
for (int i = 0; i < N; i++) {
list.add(sc.nextInt());
}
M = sc.nextInt();
for (int i = 0; i < M; i++) {
String str = sc.next();
switch(str) {
case "I": InsertPw(); break;
case "D": DeletePw(); break;
}
}
System.out.print("#"+tc+" ");
for (int i = 1; i <= 10; i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
}
}
public static void InsertPw() {
int start = sc.nextInt();
int cnt = sc.nextInt();
for (int i = 1; i <= cnt; i++) {
list.add(start+i, sc.nextInt());
}
}
public static void DeletePw() {
int start = sc.nextInt();
int cnt = sc.nextInt();
for (int i = 1; i <= cnt; i++) {
list.remove(start+1); //지워지면 .. 앞으로 땡겨지기때문에 항상 start 뒤에걸 지워야함 지울갯수만큼!
}
}
}
728x90
'알고리즘 > 그 외' 카테고리의 다른 글
프로그래머스[Level2] - 스킬 트리 [Summer/Winter Coding(~2018)] (0) | 2020.08.28 |
---|---|
[JAVA] 알고리즘 달팽이 배열 - 직사각형 (0) | 2020.07.23 |
SWEA - 8741. 두문자어 (0) | 2020.05.10 |
SWEA - 7193. 승현이의 수학공부 (0) | 2020.03.29 |
백준 2167번: 2차원 배열의 합 (0) | 2020.03.14 |