java 11

[코딩 인터뷰]지식 기반 문제 - Java 문제

[ private 생성자 ] 상속 관점에서 생성자를 private로 선언하면 어떤 효과가 있나? 생성자가 private으로 선언된 class A는 A의 private 메서드에 접근이 가능해야만 생성자를 호출할 수 있다는 것을 의미합니다. [ finally에서의 반환 ] 자바의 finally 블록은 try-catch-finally의 try 블록 안에 return 문을 넣어도 실행되는가? finally 블록은 try 블록이 종료되는 순간 실행되기 때문에 실행이 됩니다. ⭐ continue, break와 exception을 사용해도 마찬가지로 실행됩니다. 👀 finally 블록이 실행되지 않는 경우 try/catch 수행 중에 가상 머신이 종료됨 try/catch를 수행하고 있던 스레드가 죽음 [ final과..

[코딩 인터뷰]지식 기반 문제 - Java

[ 오버로딩 vs 오버라이딩 ] 👀 오버로딩 : 두 메서드가 같은 이름을 갖고 있으나 인자의 수나 자료형이 다른 경우 public double solve(Circle c){ ... } public double solve(Square s){ ... } 👀 오버라이딩 : 상위 클래스의 메서드와 이름과 용래(signature)가 같은 함수를 하위 클래스에 재정의하는 것 public abstract class Shape{ public void printMe(){ System.out.println("I am a shape."); } public abstract double computeArea(); } public class Circle extends Shape{ private double rad = 5; pub..

[LeetCode/Java]Array and String - Immutable String : Problems & Solutions

나중에 다시 찾아보기 위한 한글 번역 및 요약 Java의 경우 문자열의 중간을 변경하지 못하는 문제와 문자열 추가 시 시간이 오래 걸리는 문제가 있습니다. 이 문제에 대한 해결책입니다. 1. 문자열 수정 작업 문자열을 수정하기 위해서는 문자 배열(char[])을 이용하여 수정할 수 있습니다. public class Main { public static void main(String[] args) { String s = "Hello World"; char[] str = s.toCharArray(); str[5] = ','; System.out.println(str); } } 2. 문자열 추가 작업 public class Main { public static void main(String[] args) { ..

[LeetCode/Java]Array and String - Introduction to String

나중에 다시 찾아보기 위한 한글 번역 및 요약 문자열 소개 문자열은 실제로 유니 코드 문자의 배열입니다. 배열에서 사용한 거의 모든 작업을 수행할 수 있습니다. 그러나 몇 가지 차이점이 있습니다. 문자열을 다룰 때 알아야 할 몇 가지 사항을 살펴보겠습니다. 이러한 기능은 언어마다 많이 다를 수 있습니다. 기능 비교 문자열에는 자체 비교 기능이 있습니다. 하지만 사용하는 언어가 연산자 오버 로딩을 지원하느냐에 따라서 "==" 연산자를 사용하여 비교가 가능하거나 불가능합니다. Java의 경우 연산자 오버 로딩을 지원하지 않기 때문에 "=="를 사용하여 두 문자열을 비교할 수 없습니다. "=="를 사용하면 실제로 이 두 개체가 동일한 개체인지 비교해야 합니다. 코드로 확인해 보시면 이해하시기 편합니다. pub..

[LeetCode/Python/Java]Array and String - Diagonal Traverse

문제 Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. 2차원 배열을 입력받았을 때 아래와 같은 순서로 배열을 출력한다. 코드를 보시려면 더보기를 클릭해 주세요. 더보기 파이썬 코드 class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: if len(matrix) == 0: return [] N, M = len(matrix), len(matrix[0]) result, inter = [], [] for d in range(N ..

[LeetCode/Python/Java]Array and String - Plus One

문제 Given a non-empty array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself. 이 문제는 입력된 배열을 한 개의 숫자로 인식하여 1로 더한 후의 값을 배열로 출력해야 합니다. 예를 들어서 입..

[LeetCode/Python/Java]Array and String - Largest Number At Least Twice of Others

문제 In a given integer array nums, there is always exactly one largest element. Find whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, otherwise return -1. 배열 안의 최댓값이 다른 모든 값보다 2배 이상일 경우 최댓값의 인덱스를 찾아서 리턴하고 아닐 경우는 -1을 리턴합니다. 예를 들어 배열 A가 [3, 6, 1, 0]일 경우 최댓값 6이 다른 값들보다 2배 이상이기 때문에 1을 리턴합니다. 배열 B가..

[LeetCode/Python/Java]Array and String - Find Pivot Index

문제 Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index. If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index. 해석해보면 배열 안..

[LeetCode/Java]Array and String - Introduction to 2D Array

나중에 다시 찾아보기 위한 한글 번역 및 요약 2차원 배열이란? 1차원 배열과 유사하게 2차원 배열도 일련의 원소로 구성됩니다. 그러나 원소는 선이 아닌 1차원 배열로 구성이 됩니다. 원리 일부 언어에서는 다차원 배열이 내부적으로 1차원 배열로 구현되는 반면 다른 언어에서는 실제로 다차원 배열이 전혀 없습니다. Java의 경우 2차원 배열은 실제로 M개의 요소를 포함하는 1차원 배열이며, 각 요소는 N개의 정수 배열입니다. 동적 2차원 배열 1차원 동적 배열과 유사하게 동적 2차원 배열을 정의할 수 있습니다. 다음과 같이 사용이 가능합니다. public class Main { private static void printArray(int[][] a) { // 입력 받은 2차원 배열의 1차원 배열 출력으로..

[LeetCode/Java]Array and String - Introduction to Dynamic Array

나중에 다시 찾아보기 위한 한글 번역 및 요약 동적 배열이란? 배열은 고정된 크기를 가지고 있습니다. 하지만 동적 배열은 배열의 크기를 고정하지 않고 바꿀 수 있는 배열입니다. 다음과 같이 사용이 가능합니다. public class Main { public static void main(String[] args) { // 1. 초기화 List v0 = new ArrayList(); List v1;// v1은 비어있다.(v1 == null) // 2. 배열을 벡터로 변경 Integer[] a = {0, 1, 2, 3, 4}; v1 = new ArrayList(Arrays.asList(a)); // 3. 복사본 만들기 List v2 = v1;// v1에 대한 또 다른 참조 List v3 = new Array..

728x90