mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* File: array.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_array_and_linkedlist;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class array {
|
||||
/* 원소에 임의 접근 */
|
||||
static int randomAccess(int[] nums) {
|
||||
// 구간 [0, nums.length)에서 숫자 하나를 무작위로 뽑는다
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// 무작위 원소를 가져와 반환한다
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 배열 길이 확장 */
|
||||
static int[] extend(int[] nums, int enlarge) {
|
||||
// 길이를 확장한 배열을 초기화한다
|
||||
int[] res = new int[nums.length + enlarge];
|
||||
// 원래 배열의 모든 원소를 새 배열에 복사한다
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 확장한 새 배열을 반환한다
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 배열의 인덱스 index에 원소 num 삽입 */
|
||||
static void insert(int[] nums, int num, int index) {
|
||||
// 인덱스 index와 그 뒤에 있는 모든 원소를 한 칸씩 뒤로 이동한다
|
||||
for (int i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index 위치의 원소에 num을 대입한다
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 인덱스 index에 있는 원소 삭제 */
|
||||
static void remove(int[] nums, int index) {
|
||||
// 인덱스 index 뒤에 있는 모든 원소를 한 칸씩 앞으로 이동한다
|
||||
for (int i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 배열 순회 */
|
||||
static void traverse(int[] nums) {
|
||||
int count = 0;
|
||||
// 인덱스를 통해 배열을 순회한다
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 배열의 원소를 직접 순회한다
|
||||
for (int num : nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 배열에서 지정한 원소 탐색 */
|
||||
static int find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
/* 배열 초기화 */
|
||||
int[] arr = new int[5];
|
||||
System.out.println("배열 arr = " + Arrays.toString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
System.out.println("배열 nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 임의 접근 */
|
||||
int randomNum = randomAccess(nums);
|
||||
System.out.println("nums에서 무작위로 가져온 원소 " + randomNum);
|
||||
|
||||
/* 길이 확장 */
|
||||
nums = extend(nums, 3);
|
||||
System.out.println("배열 길이를 8로 확장한 후 nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 원소 삽입 */
|
||||
insert(nums, 6, 3);
|
||||
System.out.println("인덱스 3에 숫자 6을 삽입한 후 nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 원소 삭제 */
|
||||
remove(nums, 2);
|
||||
System.out.println("인덱스 2의 원소를 삭제한 후 nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 배열 순회 */
|
||||
traverse(nums);
|
||||
|
||||
/* 원소 탐색 */
|
||||
int index = find(nums, 3);
|
||||
System.out.println("nums에서 원소 3을 탐색하여 얻은 인덱스 = " + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* File: linked_list.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_array_and_linkedlist;
|
||||
|
||||
import utils.*;
|
||||
|
||||
public class linked_list {
|
||||
/* 연결 리스트의 노드 n0 뒤에 노드 P 삽입 */
|
||||
static void insert(ListNode n0, ListNode P) {
|
||||
ListNode n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 연결 리스트의 노드 n0 바로 뒤에 있는 노드 삭제 */
|
||||
static void remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 연결 리스트에서 인덱스가 index인 노드에 접근 */
|
||||
static ListNode access(ListNode head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 연결 리스트에서 값이 target인 첫 번째 노드 탐색 */
|
||||
static int find(ListNode head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
/* 연결 리스트 초기화 */
|
||||
// 각 노드를 초기화한다
|
||||
ListNode n0 = new ListNode(1);
|
||||
ListNode n1 = new ListNode(3);
|
||||
ListNode n2 = new ListNode(2);
|
||||
ListNode n3 = new ListNode(5);
|
||||
ListNode n4 = new ListNode(4);
|
||||
// 노드 사이의 참조를 설정한다
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
System.out.println("초기화한 연결 리스트");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* 노드 삽입 */
|
||||
insert(n0, new ListNode(0));
|
||||
System.out.println("노드를 삽입한 후의 연결 리스트");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* 노드 삭제 */
|
||||
remove(n0);
|
||||
System.out.println("노드를 삭제한 후의 연결 리스트");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* 노드 접근 */
|
||||
ListNode node = access(n0, 3);
|
||||
System.out.println("연결 리스트에서 인덱스 3에 있는 노드의 값 = " + node.val);
|
||||
|
||||
/* 노드 탐색 */
|
||||
int index = find(n0, 2);
|
||||
System.out.println("연결 리스트에서 값이 2인 노드의 인덱스 = " + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: list.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_array_and_linkedlist;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class list {
|
||||
public static void main(String[] args) {
|
||||
/* 리스트 초기화 */
|
||||
// 배열의 원소 형식에는 int[]의 래퍼 형식인 Integer[]를 사용한다는 점에 주의한다
|
||||
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
||||
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
|
||||
System.out.println("리스트 nums = " + nums);
|
||||
|
||||
/* 원소 접근 */
|
||||
int num = nums.get(1);
|
||||
System.out.println("인덱스 1의 원소에 접근하여 얻은 num = " + num);
|
||||
|
||||
/* 원소 갱신 */
|
||||
nums.set(1, 0);
|
||||
System.out.println("인덱스 1의 원소를 0으로 갱신한 후 nums = " + nums);
|
||||
|
||||
/* 리스트 비우기 */
|
||||
nums.clear();
|
||||
System.out.println("리스트를 비운 후 nums = " + nums);
|
||||
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
System.out.println("원소를 추가한 후 nums = " + nums);
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums.add(3, 6);
|
||||
System.out.println("인덱스 3에 숫자 6을 삽입한 후 nums = " + nums);
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums.remove(3);
|
||||
System.out.println("인덱스 3의 원소를 삭제한 후 nums = " + nums);
|
||||
|
||||
/* 인덱스를 통해 리스트를 순회한다 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums.get(i);
|
||||
}
|
||||
/* 리스트의 원소를 직접 순회한다 */
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 두 리스트 연결 */
|
||||
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
|
||||
nums.addAll(nums1);
|
||||
System.out.println("리스트 nums1을 nums 뒤에 연결한 후 nums = " + nums);
|
||||
|
||||
/* 리스트 정렬 */
|
||||
Collections.sort(nums);
|
||||
System.out.println("리스트를 정렬한 후 nums = " + nums);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* File: my_list.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_array_and_linkedlist;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 리스트 클래스 */
|
||||
class MyList {
|
||||
private int[] arr; // 배열(리스트 원소 저장)
|
||||
private int capacity = 10; // 리스트 용량
|
||||
private int size = 0; // 리스트 길이(현재 원소 개수)
|
||||
private int extendRatio = 2; // 리스트의 용량을 확장할 때마다 적용하는 배수
|
||||
|
||||
/* 생성자 */
|
||||
public MyList() {
|
||||
arr = new int[capacity];
|
||||
}
|
||||
|
||||
/* 리스트 길이(현재 원소 개수) 반환 */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/* 리스트 용량 반환 */
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* 원소 접근 */
|
||||
public int get(int index) {
|
||||
// 인덱스가 범위를 벗어나면 예외를 발생시킨다. 아래에서도 동일하다
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("인덱스가 범위를 벗어났다");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 원소 갱신 */
|
||||
public void set(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("인덱스가 범위를 벗어났다");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
public void add(int num) {
|
||||
// 원소 개수가 용량을 초과하면 용량 확장을 수행한다
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
arr[size] = num;
|
||||
// 원소 개수를 갱신한다
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("인덱스가 범위를 벗어났다");
|
||||
// 원소 개수가 용량을 초과하면 용량 확장을 수행한다
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
// 인덱스 index와 그 뒤의 원소를 모두 한 칸씩 뒤로 이동한다
|
||||
for (int j = size - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
arr[index] = num;
|
||||
// 원소 개수를 갱신한다
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 원소 삭제 */
|
||||
public int remove(int index) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("인덱스가 범위를 벗어났다");
|
||||
int num = arr[index];
|
||||
// 인덱스 index 뒤의 원소를 모두 한 칸씩 앞으로 이동한다
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// 원소 개수를 갱신한다
|
||||
size--;
|
||||
// 삭제한 원소를 반환한다
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 리스트 용량 확장 */
|
||||
public void extendCapacity() {
|
||||
// 길이가 원래 배열의 extendRatio배인 새 배열을 생성하고, 원래 배열을 새 배열에 복사한다
|
||||
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
||||
// 리스트 용량을 갱신한다
|
||||
capacity = arr.length;
|
||||
}
|
||||
|
||||
/* 리스트를 배열로 변환 */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
public class my_list {
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
/* 리스트 초기화 */
|
||||
MyList nums = new MyList();
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
System.out.println("리스트 nums = " + Arrays.toString(nums.toArray()) +
|
||||
" , 용량 = " + nums.capacity() + " , 길이 = " + nums.size());
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums.insert(3, 6);
|
||||
System.out.println("인덱스 3에 숫자 6을 삽입한 후 nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums.remove(3);
|
||||
System.out.println("인덱스 3의 원소를 삭제한 후 nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 원소 접근 */
|
||||
int num = nums.get(1);
|
||||
System.out.println("인덱스 1의 원소에 접근하여 얻은 num = " + num);
|
||||
|
||||
/* 원소 갱신 */
|
||||
nums.set(1, 0);
|
||||
System.out.println("인덱스 1의 원소를 0으로 갱신한 후 nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 용량 확장 동작 테스트 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// i = 5일 때 리스트 길이가 용량을 초과하므로 용량 확장이 수행된다
|
||||
nums.add(i);
|
||||
}
|
||||
System.out.println("용량을 확장한 후 리스트 nums = " + Arrays.toString(nums.toArray()) +
|
||||
" , 용량 = " + nums.capacity() + " , 길이 = " + nums.size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user