mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-04 12:12:45 +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,4 @@
|
||||
add_executable(array array.cpp)
|
||||
add_executable(linked_list linked_list.cpp)
|
||||
add_executable(list list.cpp)
|
||||
add_executable(my_list my_list.cpp)
|
||||
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* File: array.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 원소에 임의 접근 */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// 구간 [0, size)에서 무작위로 수 하나 선택
|
||||
int randomIndex = rand() % size;
|
||||
// 무작위 원소를 가져와 반환한다
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 배열 길이 확장 */
|
||||
int *extend(int *nums, int size, int enlarge) {
|
||||
// 길이를 확장한 배열을 초기화한다
|
||||
int *res = new int[size + enlarge];
|
||||
// 원래 배열의 모든 원소를 새 배열에 복사한다
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 메모리 해제
|
||||
delete[] nums;
|
||||
// 확장한 새 배열을 반환한다
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 배열의 인덱스 index에 원소 num 삽입 */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// 인덱스 index와 그 뒤에 있는 모든 원소를 한 칸씩 뒤로 이동한다
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index 위치의 원소에 num을 대입한다
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 인덱스 index에 있는 원소 삭제 */
|
||||
void remove(int *nums, int size, int index) {
|
||||
// 인덱스 index 뒤에 있는 모든 원소를 한 칸씩 앞으로 이동한다
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 배열 순회 */
|
||||
void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// 인덱스를 통해 배열을 순회한다
|
||||
for (int i = 0; i < size; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* 배열에서 지정한 원소 탐색 */
|
||||
int find(int *nums, int size, int target) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
/* 배열 초기화 */
|
||||
int size = 5;
|
||||
int *arr = new int[size];
|
||||
cout << "배열 arr = ";
|
||||
printArray(arr, size);
|
||||
|
||||
int *nums = new int[size]{1, 3, 2, 5, 4};
|
||||
cout << "배열 nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 임의 접근 */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
cout << "nums에서 무작위로 가져온 원소 " << randomNum << endl;
|
||||
|
||||
/* 길이 확장 */
|
||||
int enlarge = 3;
|
||||
nums = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
cout << "배열 길이를 8로 확장한 후 nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 원소 삽입 */
|
||||
insert(nums, size, 6, 3);
|
||||
cout << "인덱스 3에 숫자 6을 삽입한 후 nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 원소 삭제 */
|
||||
remove(nums, size, 2);
|
||||
cout << "인덱스 2의 원소를 삭제한 후 nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 배열 순회 */
|
||||
traverse(nums, size);
|
||||
|
||||
/* 원소 탐색 */
|
||||
int index = find(nums, size, 3);
|
||||
cout << "nums에서 원소 3을 탐색하여 얻은 인덱스 = " << index << endl;
|
||||
|
||||
// 메모리 해제
|
||||
delete[] arr;
|
||||
delete[] nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* File: linked_list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 연결 리스트의 노드 n0 뒤에 노드 P 삽입 */
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
|
||||
/* 연결 리스트의 노드 n0 바로 뒤에 있는 노드 삭제 */
|
||||
void remove(ListNode *n0) {
|
||||
if (n0->next == nullptr)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// 메모리 해제
|
||||
delete P;
|
||||
}
|
||||
|
||||
/* 연결 리스트에서 인덱스가 index인 노드에 접근 */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == nullptr)
|
||||
return nullptr;
|
||||
head = head->next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 연결 리스트에서 값이 target인 첫 번째 노드 탐색 */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head != nullptr) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
/* 연결 리스트 초기화 */
|
||||
// 각 노드를 초기화한다
|
||||
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;
|
||||
cout << "초기화한 연결 리스트" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 노드 삽입 */
|
||||
insert(n0, new ListNode(0));
|
||||
cout << "노드를 삽입한 후의 연결 리스트" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 노드 삭제 */
|
||||
remove(n0);
|
||||
cout << "노드를 삭제한 후의 연결 리스트" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 노드 접근 */
|
||||
ListNode *node = access(n0, 3);
|
||||
cout << "연결 리스트에서 인덱스 3에 있는 노드의 값 = " << node->val << endl;
|
||||
|
||||
/* 노드 탐색 */
|
||||
int index = find(n0, 2);
|
||||
cout << "연결 리스트에서 값이 2인 노드의 인덱스 = " << index << endl;
|
||||
|
||||
// 메모리 해제
|
||||
freeMemoryLinkedList(n0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
/* 리스트 초기화 */
|
||||
vector<int> nums = {1, 3, 2, 5, 4};
|
||||
cout << "리스트 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 원소 접근 */
|
||||
int num = nums[1];
|
||||
cout << "인덱스 1의 원소에 접근하여 얻은 num = " << num << endl;
|
||||
|
||||
/* 원소 갱신 */
|
||||
nums[1] = 0;
|
||||
cout << "인덱스 1의 원소를 0으로 갱신한 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 리스트 비우기 */
|
||||
nums.clear();
|
||||
cout << "리스트를 비운 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
nums.push_back(1);
|
||||
nums.push_back(3);
|
||||
nums.push_back(2);
|
||||
nums.push_back(5);
|
||||
nums.push_back(4);
|
||||
cout << "원소를 추가한 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums.insert(nums.begin() + 3, 6);
|
||||
cout << "인덱스 3에 숫자 6을 삽입한 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums.erase(nums.begin() + 3);
|
||||
cout << "인덱스 3의 원소를 삭제한 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 인덱스를 통해 리스트를 순회한다 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* 리스트의 원소를 직접 순회한다 */
|
||||
count = 0;
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 두 리스트 연결 */
|
||||
vector<int> nums1 = {6, 8, 7, 10, 9};
|
||||
nums.insert(nums.end(), nums1.begin(), nums1.end());
|
||||
cout << "리스트 nums1을 nums 뒤에 연결한 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 리스트 정렬 */
|
||||
sort(nums.begin(), nums.end());
|
||||
cout << "리스트를 정렬한 후 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* File: my_list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 리스트 클래스 */
|
||||
class MyList {
|
||||
private:
|
||||
int *arr; // 배열(리스트 원소 저장)
|
||||
int arrCapacity = 10; // 리스트 용량
|
||||
int arrSize = 0; // 리스트 길이(현재 원소 개수)
|
||||
int extendRatio = 2; // 리스트의 용량을 확장할 때마다 적용하는 배수
|
||||
|
||||
public:
|
||||
/* 생성자 */
|
||||
MyList() {
|
||||
arr = new int[arrCapacity];
|
||||
}
|
||||
|
||||
/* 소멸자 */
|
||||
~MyList() {
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
/* 리스트 길이(현재 원소 개수) 반환*/
|
||||
int size() {
|
||||
return arrSize;
|
||||
}
|
||||
|
||||
/* 리스트 용량 반환 */
|
||||
int capacity() {
|
||||
return arrCapacity;
|
||||
}
|
||||
|
||||
/* 원소 접근 */
|
||||
int get(int index) {
|
||||
// 인덱스가 범위를 벗어나면 예외를 발생시킨다. 아래에서도 동일하다
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("인덱스가 범위를 벗어났다");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 원소 갱신 */
|
||||
void set(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("인덱스가 범위를 벗어났다");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
void add(int num) {
|
||||
// 원소 개수가 용량을 초과하면 용량 확장을 수행한다
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
arr[size()] = num;
|
||||
// 원소 개수를 갱신한다
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
void insert(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("인덱스가 범위를 벗어났다");
|
||||
// 원소 개수가 용량을 초과하면 용량 확장을 수행한다
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
// 인덱스 index와 그 뒤의 원소를 모두 한 칸씩 뒤로 이동한다
|
||||
for (int j = size() - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
arr[index] = num;
|
||||
// 원소 개수를 갱신한다
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 원소 삭제 */
|
||||
int remove(int index) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("인덱스가 범위를 벗어났다");
|
||||
int num = arr[index];
|
||||
// 인덱스 index 뒤의 원소를 모두 한 칸씩 앞으로 이동한다
|
||||
for (int j = index; j < size() - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// 원소 개수를 갱신한다
|
||||
arrSize--;
|
||||
// 삭제한 원소를 반환한다
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 리스트 용량 확장 */
|
||||
void extendCapacity() {
|
||||
// 길이가 기존 배열의 extendRatio배인 새 배열 생성
|
||||
int newCapacity = capacity() * extendRatio;
|
||||
int *tmp = arr;
|
||||
arr = new int[newCapacity];
|
||||
// 원래 배열의 모든 원소를 새 배열에 복사한다
|
||||
for (int i = 0; i < size(); i++) {
|
||||
arr[i] = tmp[i];
|
||||
}
|
||||
// 메모리 해제
|
||||
delete[] tmp;
|
||||
arrCapacity = newCapacity;
|
||||
}
|
||||
|
||||
/* 출력을 위해 리스트를 Vector로 변환 */
|
||||
vector<int> toVector() {
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
vector<int> vec(size());
|
||||
for (int i = 0; i < size(); i++) {
|
||||
vec[i] = arr[i];
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
};
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
/* 리스트 초기화 */
|
||||
MyList *nums = new MyList();
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
nums->add(1);
|
||||
nums->add(3);
|
||||
nums->add(2);
|
||||
nums->add(5);
|
||||
nums->add(4);
|
||||
cout << "리스트 nums = ";
|
||||
vector<int> vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "용량 = " << nums->capacity() << " , 길이 = " << nums->size() << endl;
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums->insert(3, 6);
|
||||
cout << "인덱스 3에 숫자 6을 삽입한 후 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums->remove(3);
|
||||
cout << "인덱스 3의 원소를 삭제한 후 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 원소 접근 */
|
||||
int num = nums->get(1);
|
||||
cout << "인덱스 1의 원소에 접근하여 얻은 num = " << num << endl;
|
||||
|
||||
/* 원소 갱신 */
|
||||
nums->set(1, 0);
|
||||
cout << "인덱스 1의 원소를 0으로 갱신한 후 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 용량 확장 동작 테스트 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// i = 5일 때 리스트 길이가 용량을 초과하므로 용량 확장이 수행된다
|
||||
nums->add(i);
|
||||
}
|
||||
cout << "용량을 확장한 후 리스트 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "용량 = " << nums->capacity() << " , 길이 = " << nums->size() << endl;
|
||||
|
||||
// 메모리 해제
|
||||
delete nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user