본문 바로가기

전체 글148

동적 프로그래밍(Dynamic Programming) static int fibonacci(int n){ if(n==1) return 1; if(n==2) return 1; return fibonacci(n-1) + fibonacci(n-2); } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); System.out.println(fibonacci(N)); } 피보나치 수열을 다음과 같은 로직으로 짠다면, 시간 복잡도는 2^n 이 나오게 된다. 예를 들어 Index 6의 값을 구한다고 가정해보자. .. 2020. 5. 9.
Stack and Queue Stack Stack stack = new Stack(); stack.add(1); stack.add(2); stack.add(3); stack.pop(); System.out.println(stack.peek()); if(!stack.empty()){ for(int i:stack){ System.out.println(i); } } ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ output: 2 // peek 1 2 add, pop , peek( Top의 값을 보는 method ) Queue Queue queue= new LinkedList(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.remove(); System.out.println(q.. 2020. 5. 8.
Kruskal algorithm Union-find algorithm #include int getParent(int parent[], int x) { if (parent[x] == x) return x; parent[x] = getParent(parent, parent[x]); return parent[x]; } void unionParent(int parent[], int a, int b){ a= getParent(parent,a); b= getParent(parent,b); if(a>b) parent[a]=b; else parent[b]=a; } int findParent(int parent[],int a, int b){ a= getParent(parent,a); b= getParent(parent,b); if(a==b) retu.. 2020. 4. 24.
String.split() 을 이용하여 , 값 받기 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); ArrayList al = new ArrayList(); int number = Integer.parseInt(br.readLine()); for(int i=0;i 2020. 4. 16.
인터페이스의 요소들 다음과 같은 Interface를 만들었다. public interface InterfaceTest { static void printStatic(){ System.out.println("interface static method"); printPrivateStatc(); } default void print(){ System.out.println("interface default method"); printPrivate(); printPrivateStatc(); } private void printPrivate(){ System.out.println("private interface method"); } private static void printPrivateStatc(){ System.out.prin.. 2020. 4. 13.
Naming Conventions 2. Classes naming conventions In Java, class names generally should be nouns, in title-case with the first letter of each separate word capitalized. e.g. public class ArrayList {} public class Employee {} public class Record {} public class Identity {} 클래스의 이름은 명사여야 한다. 3. Interfaces naming conventions In Java, interfaces names, generally, should be adjectives. Interfaces should be in titlecase .. 2020. 4. 6.
인터페이스 모두 Abstract method로 이루어져있다. 인터페이스에 선언한 변수는 모두 상수가 되고, 메서드는 전부 추상 메서드가 된다. public interface Calc{ int a = 3; // public static final int a = 3; int add(int num1,int num2) // abstract int add() } 이 때, int add(int num1,int num2) 을 보면 num1 , num2 를 받아서 두 수를 add 하고 int형으로 돌려줄 것이라는 것을 유추할 수 있다. Interface Vs Abstract class 결론부터 말하자면 인터페이스는 설계도로서의 계약서, 규약('명세(specification' 라고 흔히 말한다)이라고 생각하면 되고, 추상 클래스는.. 2020. 3. 30.
추상클래스 & 템플릿 메서드 추상클래스(abstract class) public abstract class Tv { public abstract void show(); } - 선언은 상위클래스에 하고, 구현의 책임을 하위클래스에 위임하는 형태. - 추상 메소드를 하나라도 포함하는 class는 추상 클래스이다. 추상 클래스는 인스턴스 생성이 불가능하다.( 추상 메소드 = body가 없는 메소드 ) - 메소드가 전부 구현돼있더라도, abstract가 포함돼있다면 추상클래스이다. : 상속을 하기 위한, 기반이 되는 클래스에 abstract 를 붙인다. abstract class Computer{ public abstract void message(); public void turnOn(){ System.out.println("켠다"); .. 2020. 3. 30.