본문 바로가기

DataStructure&Algorithm

(52)
[DataStructure&Algorithm] 재귀 함수 section : DFS 풀이 1 public class Main { public void DFS(int n){ if(n==0) return; else { DFS(n - 1); System.out.print(n+" "); } } public static void main(String[] args){ Main m = new Main(); m.DFS(3); } } comment : 재귀 함수에 대해 배웠다. 함수가 호출되면 스택에는 함수의 매개변수, 호출이 끝난 뒤 돌아갈 반환 주소값, 함수에서 선언된 지역 변수 등이 저장된다. 이렇게 스택 영역에 차례대로 저장되는 함수의 호출 정보를 스택 프레임(stack frame)이라고 한다. 출처 : https://www.tcpschool.com/c/c_memory_..
[DataStructure&Algorithm] 응급실 section : queue 풀이 1 import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Person{ int id; int priority; public Person(int id, int priority){ this.id = id; this.priority = priority; } } public class Main { public int solution(int a, int b, int[] arr) { int answer = 0; Queue q = new LinkedList(); for (int i = 0; i < a; i++) { q.offer(new Person(i, arr[i])); } while ..
[DataStructure&Algorithm] 교육과정 설계 section : queue 풀이 1 import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public String solution(String a, String b){ Queue q = new LinkedList(); for(char c : a.toCharArray()) q.offer(c); for(char c : b.toCharArray()){ if(q.contains(c)){ if(q.poll()!=c) return "NO"; } } if(!q.isEmpty()) return "NO"; return "YES"; } public static void main(String[] ar..
[DataStructure&Algorithm] 공주 구하기 section : stack 풀이 1 import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public int solution(int a, int b){ int p1 = 0; Queue q = new LinkedList(); for(int i=0; i
[DataStructure&Algorithm] 쇠막대기 section : stack 풀이 1 import java.util.Scanner; import java.util.Stack; public class Main { public int solution(String str){ int answer = 0; Stack s = new Stack(); for(int i=0; i
[DataStructure&Algorithm] 후위식 연산 section : stack 풀이 1 import java.util.*; public class Main { public int solution(String str){ Stack s = new Stack(); for(char c : str.toCharArray()){ if(Character.isDigit(c)) { s.push(c-48); } else { int a = s.pop(); int b = s.pop(); if(c=='+'){ s.push(b+a); } else if(c=='-') { s.push(b-a); } else if(c=='*') { s.push(b*a); } else { s.push(b/a); } } } return s.pop(); } public static void main(Str..
[DataStructure&Algorithm] 크레인 인형뽑기 풀이 1 import java.util.*; public class Main { public int solution(int n, int[][] board, int m, int[] moves){ int answer = 0; Stack s = new Stack(); int p1=0; for(int i : moves){ while(true){ if(board[n-1][i-1]==0) break; if(!(board[p1][i-1]==0)) { int tmp = board[p1][i - 1]; board[p1][i - 1] = 0; if(!s.isEmpty() && tmp == s.peek()){ answer += 2; s.pop(); } else { s.push(tmp); } break; } p1++; } p..
[DataStructure&Algorithm] 괄호 문자 제거 section : stack 풀이 1 import java.util.*; public class Main { public String solution(String str){ String answer = ""; Stack s = new Stack(); for(char x : str.toCharArray()){ if(x == '(') s.push(x); else if(x == ')') s.pop(); else if(s.isEmpty()) answer += x; } return answer; } public static void main(String[] args){ Main m = new Main(); Scanner scanner = new Scanner(System.in); String str = scann..