Code Monkey home page Code Monkey logo

codingtest_java's Introduction

codingtest_java

codingtest_java's People

Contributors

dongur2 avatar

Watchers

Lucian avatar  avatar  avatar

codingtest_java's Issues

[프로그래머스: level 0] 구슬을 나누는 경우의 수 - 120840

서로 다른 구슬 n개를 m개씩 선택할 수 있는 경우의 수를 반환


nCr 공식을 활용하면, n! / ( (n-m)! * m! ) 이 식을 만족하는 결과가 값이 된다. 이 식은(n! / m!) * (n-m)! 와 같으므로,

스크린샷 2024-04-04 오전 12 10 28
스크린샷 2024-04-04 오전 12 11 07
위와 같이 코드로 나타낼 수 있게 된다.

이 과정에서 answerint로 선언한 뒤 사용하게 되면, 수용할 수 있는 최대값을 넘어가게 되어 결국 결과가 0이 되므로 double로 선언해서 결과값을 받아 저장해준 뒤에 return (int)answer로 형변환해 반환해주었다.

큐(Queue)

  • 선형 데이터 구조
  • 데이터가 순차적으로 들어오는 형태
  • FIFO(First In First Out) 선입선출 형태의 자료구조

image

새로운 데이터는 맨 끝/맨 뒤(rear)에 추가되고, 데이터를 꺼낼 때는 다른 끝/맨 앞(head)에서 꺼내어 사용

  • enqueue 큐의 rear에 새로운 데이터 삽입 [INSERT]
  • dequeue 큐의 front에서 데이터를 반환하고 삭제 [DELETE]
  • peek 큐의 front에 있는 데이터 반환
  • isEmpty 큐가 비어 있는지 확인
  • isFull 큐가 가득 찼는지 확인

https://www.geeksforgeeks.org/queue-data-structure/

스택(Stack)

Stack

  • 선형 메모리 공간에 데이터를 저장
  • 후입선출(LIFO; Last In First Out)

image
https://www.tcpschool.com/java/java_collectionFramework_stackQueue

E peek() 해당 스택의 제일 상단에 있는 요소 반환 - 제일 마지막으로 저장된 요소
E pop() 해당 스택의 제일 상단에 있는 요소를 반환하고, 해당 요소를 스택에서 제거
E push(E item) 해당 스택의 제일 상단에 전달된 요소를 삽입

스택 생성
Stack<Integer> st = new Stack<>();

System.arraycopy() & Arrays.copyOf()

System

void arraycopy(Object 원본 배열, int 복사 시작 인덱스, Object 복사받을 배열, int 시작 인덱스, int 복사 길이)

배열을 특정 위치부터 복사하여 다른 배열의 매개변수에서 지정한 특정 위치부터 붙여넣는다. 복사 길이보다 복사해온 배열이 짧을 경우 0으로 채워넣는다.

  • 반환값 없음: void
  • 원본 배열과 복사받을 배열의 인덱스 지정 가능

Arrays

T[] copyOf(T[] 원본 배열, int 복사 길이)

배열을 자르거나 0으로 채워넣어서 배열을 복사한다.

  • 새로운 배열 생성해 반환

Comparable & Comparator

Comparable

같은 타입 객체들 을 비교하여 '오름차순으로 정렬(natural order)'하는 전략을 정의하는 인터페이스

@Override 
public int compareTo(T param) { 
    return Integer.compare(x, y);
}

Integer.compare(x, y) 메서드의 리턴값으로 정렬

  • x < y return -1
  • x == y return 1

Comparator

compare(arg1, arg2) 메서드를 정의하여 다른 타입 객체들 을 비교해 정렬할 수 있는 더 유연한 인터페이스

@Override
public int compare(arg1, arg2) {
    return Integer.compare(x, y);
}

https://www.baeldung.com/java-comparator-comparable

LinkedHashSet

LinkedHashSet

중복을 허용하지 않는 대신 순서를 보장하지 않는 Set에서 데이터 저장 순서를 고정해준다.

LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
https://www.baeldung.com/java-linkedhashset

  • [프로그래머스: level 0] 소인수분해 - 120852

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.