Algorithm/List
-
배열과 List를 활용한 수 찾기Algorithm/List 2021. 12. 31. 11:05
배열과 List를 활용해서 문제를 하나 더 풀어보았다. # 백준 1920 수 찾기 백준 1920번 수 찾기 문제이다. 문제를 정리해 보자면 입력받은 수만큼 정수를 입력받고, 다시 입력받은 수만큼 정수를 입력 받아, 그 두번의 입력과 같은 수는 1, 다른 수는 0을 출력하면 된다. 지난번 포스팅 때 배운 BufferedReader와 StringTokenizer를 이용하여 최대한 시간 단축을 할 수 있는 입력 방식을 적극적으로 사용해 보기로 했다. 처음엔 List를 활용하려고 했으나, ArrayList의 구현 size는 10이기 때문에 길이 5만큼만 입력 받는 이러한 문제에서는 배열이 훨씬 메모리 소모가 적을 것으로 생각하고 배열로 구현했다. 1. 배열을 선언하고 for문과 StringTokenizer를 활..
-
List 예제 #2Algorithm/List 2021. 12. 25. 14:47
List 예제 #1에 이어서 baekjoon에서 다른 문제도 풀어 보았다. #2. 2605번 줄 세우기 문제 문제를 보자마자 긴 문제에 어려운 건 아닌가 살짝 긴장하고 문제를 읽기 시작했다. 문제를 정리해보면 0을 뽑으면 그 자리에 그대로 있으면 되고, 다른 숫자를 뽑으면 뒤에서 부터 뽑은 숫자만큼 앞으로 가면된다고 한다. 단순히 ArrayList를 구현하고 뒤에서 부터 세어준다는 것만 염두하면 될 것 같았다. list.size() : list의 길이 get(index) : 제거는 하지 않고 해당 index의 수를 뽑아옴 add(num, index) : index에 num을 추가 처음에 푼 방식은 if else 구문을 사용해서 0을 뽑으면 그 자리에 그대로 추가(add), 다른 수를 뽑으면 총 list의..
-
List 개념 및 예제#1Algorithm/List 2021. 12. 25. 14:28
List 인터페이스는 컬렉션 프레임워크의 한 종류로 배열이라고 생각하면 간단하다. 주로 순서가 있는 자료 관리에 유용하게 사용되고 중복을 허용한다. 크게 arrayList, LinkedList, Vector로 구분되고 Stack과 Queue도 List 인터페이스에 속한다. ArrayList : 객체 배열 클래스, List = new ArrayList();와 같은 문법으로 주로 사용 Vector : 멀티 스레드에서 주로 사용하고, 동기화를 제공한다. 동기화 구현시 작업이 이루어지는 자원에 대해 잠금 을 수행하므로 ArrayList보다 수행속도가 느리다. LinkedList : ArrayList보다 중간에 자료를 추가, 제거하는데 시간이 적게 걸린다. LinkedList는 자료의 추가, 제거 시 각 요소가 ..