본문 바로가기

Program

(128)
DFS, BFS DFS(깊이 우선 탐색) - 모든 경우의 수를 찾을 때 구현 package org.example.chap12; import org.example.chap11.Vertex; import java.util.LinkedList; import java.util.List; import java.util.Stack; //인접 리스트 방식 public class GraphList { // 정점들을 저장할 리스트 private List vertices; // 그래프들의 연결관계를 저장할 인접 리스트 private List adjList; public GraphList() { vertices = new LinkedList(); // 정점들이 저장될 1차원리스트 adjList = new LinkedList(); // 정점..
그래프 공용 Vertex package org.example.chap11; // 정점 노드 public class Vertex { private int id;// 정점 고유 번호 = Key 값 private String data; //정점에 저장할 데이터 private boolean visitFlag; // 정점 방문 여부 public Vertex(String data){ this.data=data; this.id = -1; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getData() { return data; } public void setData(String data) { this.d..
Lambda 22.12.15-Lambda 람다(Lambda)를 이용한 DB 내용 받기 Domain User package org.example.lambda.advance; public class User { private String id; private String email; private String password; private String username; public User(String id, String email, String password, String username) { this.id = id; this.email = email; this.password = password; this.username = username; } public String getId() { return id; } ..
이진 트리 이진 트리 구현 package org.example.chap10; import java.util.Stack; class Node { private int key; // 트리의 키 (데이터) private Node leftChild; // 왼쪽 자식 private Node rightChild; // 오른쪽 자식 public Node(int key) { this.key = key; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public Node getLeftChild() { return leftChild; } public void setLeftChild(Node leftChild) { this.le..
Lambda 22.12.14-Lambda 람다(Lambda) 여러가지 람다 표현식 (String s) -> s.length() : String형식의 파라미터 하나를 가지며 int를 반환한다. 람다표현식에는 return이 함축되어 있다. (Apple a) -> a.getWeight() > 150 : Apple형식의 파라미터 하나를 가지며 boolean을 반환한다. int형식의 2개의 파라미터를 가지며 리턴값이 없다. (int x, int y) -> { System.out.println("Result:"); System.out.println(x + y); } 이러한 형식을 '컨슈머'라고 한다. () -> 42 : 파라미터가 없으며 42를 반환한다. 제공되는 함수형 인터페이스 함수형 인터페이스 함수 디스크립..
그리디 알고리즘 문제 백준 11047 11047번: 동전 0 package org.example.chap09; import java.util.Scanner; import java.util.Stack; //https://www.acmicpc.net/problem/11047 public class 그리디01 { public static void main(String[] args) { //적절한 해 : 도달액수 보다 액면가가 작은 동전부터 최대치로 개수를 만들면서 액면가를 내려가면서 개수를 만든다. /* int N = 10; //동전 개수 int K = 4790; //목표 액수 //동전을 담을 자료구조 Stack A = new Stack(); A.push(1); A.push(10); A.push(50); A.push(100)..
탐색 탐색 구현 package org.example.chap08; import java.util.Arrays; public class Search { // 선형 탐색 public static int linearSearch(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (target == arr[i]) { return i; } } return -1; } // 이진 탐색 public static int binarySearch(int[] arr, int target) { // 왼쪽포인터, 오른쪽 포인터, 중앙포인터 선언 int left = 0; int right = arr.length - 1; int mid; while (left
기본 정렬, 개선된 정렬 기본 정렬 버블 정렬 구현 package org.example.chap06.bubble; import java.util.Arrays; public class BubbleSort { public static void sort(int[] arr){ for (int i=arr.length-1; i > 0; i--) { boolean flag = false; for (int j =0; j arr[j+1]){ //인접 자료를 비교해서 왼쪽이 더 크면 swap //swap int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag=true; } } if(!flag) break;//swap이 한번도 일어나지 않았으면 정렬이 끝난것 } } public static voi..