Java

    [LeetCode] #14 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 가장 긴 접두사를 구하는 문제다. 이런저런 방법을 써봤는데 도저히 감이 안 잡혀서 솔루션을 참고했다. class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0) { return ""; } String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(pref..

    [LeetCode] #1 Two Sum

    Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. 인자 1: 정수 배열 인자 2(타겟): 정수 두 인자를 파라미터로 넘긴다. 정수 배열 안에 있는 값을 더해 타겟의 값을 만들 수 있는지 확인하고, 만들 수 있다면 해당되는 두 값의 인덱스 번호를 배열의 형태로 리턴한다. 조건 1:..

    Integer, StringBuilder 객체 생성 및 동등성 비교, append 메소드

    들어가기 전에 참고할만한 참조 타입 (Reference Type)에 대한 기본 지식 참조 타입은 생성 시에 객체로 만들어지며 데이터(객체)가 있는 곳의 위치를 저장한다. 데이터는 분리된 메모리 영역에 저장된다. 이 말인즉, 데이터 자체가 아닌 데이터가 저장된 곳의 위치 값을 저장한다는 의미. Integer integerValue1 = 1000; Integer integerValue2 = integerValue1; Integer 타입의 객체 integerValue1을 선언하고 값을 1000으로 초기화했다. Integer 타입의 객체 integerValue2를 선언하고 integerValue1의 값을 참조하도록 했다. Equality comparison (integer, ==): true Equality c..

    [On To Java 2] Chapter 3: How to declare variables

    59. A variable is a chunk of computer memory that contains a value. The name of a variable is an identifier that refers to the variable. A variable's data type determines the size of the chunk and the way that the bits in the chunk are interpreted. If the data type of a variable is int, the variable holds a 32-bit signed integer. If the data type of a variable is double, the variable holds a 64-..

    [On To Java 2] Chapter 2: How to compile and execute a simple program

    package com.practice.doit.chapter1; public class Demonstrate { public static void main(String[] args) { System.out.println("The rating of the movie is: "); System.out.println(6+9+8); } } 46. Said with more precision, print and println are display methods that are defined to work on instances of the PrintStream class. System.out is an expression that produces the particular instance of the PrintS..

    [Database] User table, Post table relation & Cardinality

    고민하는 글! //user가 없는 포스트는 없다 //post가 없는 유저는 있다 //한쪽이 0이 될 수 있는가 //글 주인의 카디널리티(원소의 수)가 0이 될 수 있는가 모든 포스트는 유저를 가지고 있어야 한다. 포스트가 없는 유저는 있다. 원소의 수가 0이 될 수 있는가?....??? 포스트 테이블의 정의가 있어도 거기에 엔터티 튜플 값이 없을 수 있다. 그게 없더라도 어플리케이션의 모든 것이 문제가 없이 작동한다. 유저 없이는 포스트를 넣을 수 없다. 유저 입장에서 봤을 때 포스트 필드가 있고 유저가 쓴 포스트가 0개라고 올수 있음. 유저는 그런 식으로 만들어낼 수 있음 포스트는 유저에 소속된다. 의존성! 특정 유저에 디펜던시가 있는거다. 누가 중심이 되느냐가 핵심! 생각해볼만한 문제 A beginn..

    [연결 리스트]

    포인터로 연결 리스트 만들기 class Node{ E data; //데이터를 참조 Node next; //다음 노드를 참조 } Node는 데이터용 필드인 data와는 별도로 자기 자신과 같은 클래스형의 인스턴스를 참조하는 (가리키는) 참조용 필드 next를 가진다. (이런 클래스 구조를 자기 참조 (self-referential)형이라고 함) Node는 제네릭으로 구현되므로 데이터형 E는 임의의 클래스형이 허용된다. 필드 data의 자료형인 E가 참조형이므로 클래스형 변수 data가 나타내는 것이 데이터 그 자체가 아니라 데이터를 넣어 두는 인스턴스에 대한 '참조'이기 때문이다. 👉 다음 노드를 참조하는 next를 뒤쪽 포인터이다. 뒤쪽 포인터 next에 넣어 두는 것은 다음 노드에 대한 참조이다. 다음..

    [스레드] - sleep, join, wait, interrupt, notify, notifyAll, yield

    일시 정지로 가기 위한 메소드 & 벗어나기 위한 메소드 구분 메서드 실행 일시 정지로 보냄 sleep(long millis) 주어진 시간 동안 스레드를 일시 정지 상태로 만든다. 주어진 시간이 지나면 자동적으로 실행 대기 상태가 된다. join() join() 메서드를 호출한 스레드는 일시 정지 상태가 된다. 실행 대기 상태가 되려면, join() 메서드를 가진 스레드가 종료되어야 한다. wait() 동기화 블록 내에서 스레드를 일시 정지 상태로 만든다. 일시 정지에서 벗어남 interrupt() 일시 정지 상태일 경우, interruptedException을 발생시켜 실행 대기 상태 또는 종료 상태로 만든다. notify() notifyAll() wait() 메서드로 인해 일시 정지 상태인 스레드를 실..

    [스레드] 스레드 동기화, 동시성 제어

    스레드 동기화 멀티 스레드는 하나의 객체를 공유해서 작업할 수도 있다. 이 경우, 다른 스레드에 의해 객체 내부 데이터가 쉽게 변경될 수 있기 때문에 의도했던 것과는 다른 결과가 나올 수 있다. 예시) User1Thread는 Calculator 객체의 memory 필드에 100을 먼저 저장하고 2초간 일시 정지 상태가 된다. 그동안 User2Thread가 memory 필드값을 50으로 변경한다. 2초가 지나 User1Thread가 다시 실행 상태가 되어 memory 필드의 값을 출력하면 User2Thread가 저장한 50이 나온다. 이런 경우, Uers1Thread에 저장되니 데이터가 날아가버린다. 👉 스레드가 사용 중인 객체를 다른 스레드가 변경할 수 없도록 하려면 스레드 작업이 끝날 때까지 객체에 잠..

    JAVA 자바 컬렉션 연습 문제(1~7)

    01. package day12; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.Set; /* HashMap id와 tel(전화번호)로 구성된 Student클래스를 만들고, 이름을 key로하고 Student 객체를 값으로 하는 HashMap을 작성해보세요. 5명 정도 HashMap에 미리 저장해놓고, 이름을 검색하면 id와 전화번호 출력되며, exit를 입력하면 프로그램 종료. */ //class Student{ //String id, tel; //Student(){ //Scanner sc = new Scanner(System.in); //System.out.println("ID와..