전체 글
-
백준 1874 - 스택수열자료구조와 알고리즘/문제풀기 2023. 4. 4. 21:22
스택에 오름차순으로 숫자를 하나씩 집어넣고, 입력된 숫자들을 만든다. 가령 4,3,6,8 이라면 스택에 1,2,3,4까지 넣고 4 pop(), 다음 3 pop(), 다시 5,6 push()하고 6 pop(), 7,8 push() ,8 pop() ++++--++-++-로 4,3,6,8을 만드는 것이다. 다음의 경우에는 만들 수 없는 수열이다. 1,2,5,3,4 스택에 1 push -> 1pop() -> 2push() ->2 pop() , 3,4,5 push() -> 5 pop() 이때 3을 꺼내려면 4를 먼저 pop()해야하므로 만들 수 없는 수열이다. 입력: 첫 줄에 n (1 ≤ n ≤ 100,000)이 주어진다. 둘째 줄부터 n개의 줄에는 수열을 이루는 1이상 n이하의 정수가 하나씩 순서대로 주어진다...
-
백준 9012 - 괄호자료구조와 알고리즘/문제풀기 2023. 4. 4. 20:19
괄호가 짝이 맞는지 찾는 문제이다. -해결방법 1. 괄호는 ()형태만 가능하다. 2. (만 저장했다가 )가 나오면 삭제 이때 닫는괄호)가 먼저나오면 ->불완전한 괄호 3 입력받은 모든 문자열이 검사가 끝났을 때, 저장된 것이 없다면 -> 완벽한 괄호 (가 남아 있다면 -> 불완전한 괄호 풀이1 public class Main{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t ..
-
백준 9093 - 단어뒤집기자료구조와 알고리즘/문제풀기 2023. 4. 4. 20:01
문장이 주어지면, 단어를 뒤집어야한다. 입력:첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 공백이 하나 있다. 출력:각 테스트 케이스에 대해서, 입력으로 주어진 문장의 단어를 모두 뒤집어 출력한다. 예제입력 2 I am happy today We want to win the first prize 예제출력 I ma yppah yadot eW tnaw ot niw eht tsrif ezirp -해결 방법 1. 테스트케이스 입력받기 & 문자열 입력받기 2. 공백으로 단어가 구분되어 있으므로, 공백까지 문자를 저장했다가 공백을 만나면 뒤집어서 출력 *개행문자도..
-
백준 10828 - 스택 구현자료구조와 알고리즘/문제풀기 2023. 4. 4. 19:37
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception{ BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); MSt ms = new MSt(); int input = Integer.valueOf(s.readLine()); String[] temp; int result=0; while(input-- > 0){ temp = s.readLine().split(" "); switch (temp[0]){ case "push": ms.push(..
-
프코전-자바편 1~2장자료구조와 알고리즘/알고리즘 2023. 3. 30. 21:31
1장 -코드를 짤 때 흔히하는 실수 코드를 짤 때 가독성과 역할을 충분히 고려하지 못하고 쌓아올리기 시작하면, 어느순간 코드를 수정하는 것 보다 갈아 엎는 것이 나을 때가 생긴다. 이러한 상황을 방지하기 위해서 코드를 작성할 때 가독성과 역할을 신경 쓰며 문제를 단계별로 나누어 해결해보자 -디버깅과 시행착오 줄이기 > 코드를 단계별로 작성하라 > 하나의 단계를 작성하고 검증하라 > 단계 검증이 실패하면, 실패한 단계에서 더 자세히 로그를 찍어보자 - 실습 -전화번호부 만들기 사용자가 입력한 전화번호를 가진 사람들을 전화번호부에서 검색하는 기능을 구현해보자 처음에는 입력과 검색 모든 과정을 한 번에 생각하면서 코드를 작성하려 할 것이다. 이는 가독성이 떨어지는 코드 작성의 시발점이다. 전화번호를 검색하는 ..
-
자바 - 열거형(enums)언어/JAVA 2023. 3. 26. 16:51
1. 열거형이란? - 열거형은 서로 관련된 상수를 묶어놓은 것이다. java의 열거형은 타입도 관리하여 타입에 안전한 열거형이다. - 마치 인스턴스가 미리 정의된 클래스로 생각해 볼 수 있다. 1.2 열거형의 정의와 사용 - enum 열거체명 {상수명1,상수명2..} 등으로 정의할 수 있다. - 선언 위치는 클래스와 동일하다. 클래스 내부, 외부, 혹은 하나의 클래스파일에 정의할 수 있다. - 사용법은 열거체 참조변수에 열거체.상수명 혹은 열거형체.valueOf(상수명) - 모든 열거형의 조상 Enum에 Enum에 기본적으로 사용가능한 메서드들이 정의되어 있다. enum Direction {EAST,SOUTH,WEST,NORTH} Direction dir = Direction.EAST; Direction..
-
자바 - Arrays 클래스와 Comparator언어/JAVA 2023. 3. 23. 13:30
-Objects,Colletions 등 배열과 관련된 static메서드 제공 배열의 복사 - copyOf(), copyOfRange(): 배열 전체 혹은 배열의 일부 복사 배열 채우기 - fill(),setAll() : fill()은 배열의 모든 요소를 지정된 값으로 채우고 setAll()은 배열을 채우는데 함수형 인테페이스를 매개변수로 받는다 배열의 정렬과 검색 - sort(), binarySearch(): sort()는 정렬, binarySearch() 배열에 지정된 값이 저장된 index리턴 (단,배열은반드시 정렬되어 있어야함) 배열의 비교와 출력 equals(),toString() - 다차원 배열은 deepEquals() deeptoString()사용 배열을 List로 변환 asList(Objec..
-
자바 - Thread(4) 동기화언어/JAVA 2023. 3. 21. 19:43
1.쓰레드의 동기화란 - 멀티쓰레드 프로세스의 경우 여러 쓰레드가 같은 프로세스 내의 자원을 공유해서 작업하기 때문에 서로 작업에 영향을 주게됨 - 따라서, 한 쓰레드가 특정 작업을 마무리하기 전까지 다른 쓰레드에 의해 방해받지 않도록 하는 것이 필요. - 이러한 작업을 위해 도입된 개념이 임계영역(critical section)과 잠금(lock)이다. - 공유 데이터를 사용하는 코드 영역을 임계 영역으로 지정 -> 쓰레드가 데이터를 사용할 때 lock을 걸고 사용 후 unlock - lock이 걸려있는 상태에서는 다른 쓰레드는 해당 데이터에 접근할 수 없게 함 정리하자면, 동기화란 한 쓰레드가 진행 중인 작업을 다른 쓰레드가 간섭하기 못하도록 막는 것. 그리고 이를 위해 임계영역 지정과 잠금이 필요 -..