Add ru version (#1865)

* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
This commit is contained in:
Yudong Jin
2026-03-28 04:24:07 +08:00
committed by GitHub
parent 2ca570cc33
commit 772183705e
1958 changed files with 108186 additions and 0 deletions
@@ -0,0 +1,60 @@
/**
* File: binary_search.js
* Created Time: 2022-12-22
* Author: JoseHung (szhong@link.cuhk.edu.hk)
*/
/* Бинарный поиск (двусторонне замкнутый интервал) */
function binarySearch(nums, target) {
// Инициализировать двусторонне замкнутый интервал [0, n-1], то есть i и j указывают на первый и последний элементы массива соответственно
let i = 0,
j = nums.length - 1;
// Цикл завершается, когда диапазон поиска пуст (при i > j диапазон пуст)
while (i <= j) {
// Вычислить индекс середины m, используя parseInt() для округления вниз
const m = parseInt(i + (j - i) / 2);
if (nums[m] < target)
// Это означает, что target находится в интервале [m+1, j]
i = m + 1;
else if (nums[m] > target)
// Это означает, что target находится в интервале [i, m-1]
j = m - 1;
else return m; // Целевой элемент найден, вернуть его индекс
}
// Целевой элемент не найден, вернуть -1
return -1;
}
/* Бинарный поиск (лево замкнутый, право открытый интервал) */
function binarySearchLCRO(nums, target) {
// Инициализировать лево замкнутый, право открытый интервал [0, n), то есть i и j указывают на первый элемент массива и позицию сразу за последним элементом соответственно
let i = 0,
j = nums.length;
// Цикл завершается, когда диапазон поиска пуст (при i = j диапазон пуст)
while (i < j) {
// Вычислить индекс середины m, используя parseInt() для округления вниз
const m = parseInt(i + (j - i) / 2);
if (nums[m] < target)
// Это означает, что target находится в интервале [m+1, j)
i = m + 1;
else if (nums[m] > target)
// Это означает, что target находится в интервале [i, m)
j = m;
// Целевой элемент найден, вернуть его индекс
else return m;
}
// Целевой элемент не найден, вернуть -1
return -1;
}
/* Driver Code */
const target = 6;
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* Бинарный поиск (двусторонне замкнутый интервал) */
let index = binarySearch(nums, target);
console.log('Индекс целевого элемента 6 = ' + index);
/* Бинарный поиск (лево замкнутый, право открытый интервал) */
index = binarySearchLCRO(nums, target);
console.log('Индекс целевого элемента 6 = ' + index);
@@ -0,0 +1,45 @@
/**
* File: binary_search_edge.js
* Created Time: 2023-08-22
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
const { binarySearchInsertion } = require('./binary_search_insertion.js');
/* Бинарный поиск самого левого target */
function binarySearchLeftEdge(nums, target) {
// Эквивалентно поиску точки вставки target
const i = binarySearchInsertion(nums, target);
// target не найден, вернуть -1
if (i === nums.length || nums[i] !== target) {
return -1;
}
// Найти target и вернуть индекс i
return i;
}
/* Бинарный поиск самого правого target */
function binarySearchRightEdge(nums, target) {
// Преобразовать задачу в поиск самого левого target + 1
const i = binarySearchInsertion(nums, target + 1);
// j указывает на самый правый target, а i — на первый элемент больше target
const j = i - 1;
// target не найден, вернуть -1
if (j === -1 || nums[j] !== target) {
return -1;
}
// Найти target и вернуть индекс j
return j;
}
/* Driver Code */
// Массив с повторяющимися элементами
const nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nМассив nums = ' + nums);
// Бинарный поиск левой и правой границы
for (const target of [6, 7]) {
let index = binarySearchLeftEdge(nums, target);
console.log('Индекс самого левого элемента ' + target + ' равен ' + index);
index = binarySearchRightEdge(nums, target);
console.log('Индекс самого правого элемента ' + target + ' равен ' + index);
}
@@ -0,0 +1,64 @@
/**
* File: binary_search_insertion.js
* Created Time: 2023-08-22
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
/* Бинарный поиск точки вставки (без повторяющихся элементов) */
function binarySearchInsertionSimple(nums, target) {
let i = 0,
j = nums.length - 1; // Инициализировать двусторонне замкнутый интервал [0, n-1]
while (i <= j) {
const m = Math.floor(i + (j - i) / 2); // Вычислить индекс середины m, используя Math.floor() для округления вниз
if (nums[m] < target) {
i = m + 1; // target находится в интервале [m+1, j]
} else if (nums[m] > target) {
j = m - 1; // target находится в интервале [i, m-1]
} else {
return m; // Найти target и вернуть точку вставки m
}
}
// target не найден, вернуть точку вставки i
return i;
}
/* Бинарный поиск точки вставки (с повторяющимися элементами) */
function binarySearchInsertion(nums, target) {
let i = 0,
j = nums.length - 1; // Инициализировать двусторонне замкнутый интервал [0, n-1]
while (i <= j) {
const m = Math.floor(i + (j - i) / 2); // Вычислить индекс середины m, используя Math.floor() для округления вниз
if (nums[m] < target) {
i = m + 1; // target находится в интервале [m+1, j]
} else if (nums[m] > target) {
j = m - 1; // target находится в интервале [i, m-1]
} else {
j = m - 1; // Первый элемент меньше target находится в интервале [i, m-1]
}
}
// Вернуть точку вставки i
return i;
}
/* Driver Code */
// Массив без повторяющихся элементов
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
console.log('\nМассив nums = ' + nums);
// Бинарный поиск точки вставки
for (const target of [6, 9]) {
const index = binarySearchInsertionSimple(nums, target);
console.log('Индекс позиции вставки элемента ' + target + ' равен ' + index);
}
// Массив с повторяющимися элементами
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nМассив nums = ' + nums);
// Бинарный поиск точки вставки
for (const target of [2, 6, 20]) {
const index = binarySearchInsertion(nums, target);
console.log('Индекс позиции вставки элемента ' + target + ' равен ' + index);
}
module.exports = {
binarySearchInsertion,
};
@@ -0,0 +1,45 @@
/**
* File: hashing_search.js
* Created Time: 2022-12-29
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
const { arrToLinkedList } = require('../modules/ListNode');
/* Хеш-поиск (массив) */
function hashingSearchArray(map, target) {
// key хеш-таблицы: целевой элемент, value: индекс
// Если такого key нет в хеш-таблице, вернуть -1
return map.has(target) ? map.get(target) : -1;
}
/* Хеш-поиск (связный список) */
function hashingSearchLinkedList(map, target) {
// key хеш-таблицы: значение целевого узла, value: объект узла
// Если такого key нет в хеш-таблице, вернуть null
return map.has(target) ? map.get(target) : null;
}
/* Driver Code */
const target = 3;
/* Хеш-поиск (массив) */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// Инициализация хеш-таблицы
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: элемент, value: индекс
}
const index = hashingSearchArray(map, target);
console.log('Индекс целевого элемента 3 = ' + index);
/* Хеш-поиск (связный список) */
let head = arrToLinkedList(nums);
// Инициализация хеш-таблицы
const map1 = new Map();
while (head != null) {
map1.set(head.val, head); // key: значение узла, value: узел
head = head.next;
}
const node = hashingSearchLinkedList(map1, target);
console.log('Объект узла со значением 3 =', node);
@@ -0,0 +1,47 @@
/**
* File: linear_search.js
* Created Time: 2022-12-22
* Author: JoseHung (szhong@link.cuhk.edu.hk)
*/
const { ListNode, arrToLinkedList } = require('../modules/ListNode');
/* Линейный поиск (массив) */
function linearSearchArray(nums, target) {
// Обход массива
for (let i = 0; i < nums.length; i++) {
// Целевой элемент найден, вернуть его индекс
if (nums[i] === target) {
return i;
}
}
// Целевой элемент не найден, вернуть -1
return -1;
}
/* Линейный поиск (связный список) */
function linearSearchLinkedList(head, target) {
// Обойти связный список
while (head) {
// Найти целевой узел и вернуть его
if (head.val === target) {
return head;
}
head = head.next;
}
// Целевой узел не найден, вернуть null
return null;
}
/* Driver Code */
const target = 3;
/* Выполнить линейный поиск в массиве */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
const index = linearSearchArray(nums, target);
console.log('Индекс целевого элемента 3 = ' + index);
/* Выполнить линейный поиск в связном списке */
const head = arrToLinkedList(nums);
const node = linearSearchLinkedList(head, target);
console.log('Объект узла со значением 3 = ', node);
@@ -0,0 +1,46 @@
/**
* File: two_sum.js
* Created Time: 2022-12-15
* Author: gyt95 (gytkwan@gmail.com)
*/
/* Метод 1: полный перебор */
function twoSumBruteForce(nums, target) {
const n = nums.length;
// Два вложенных цикла, временная сложность O(n^2)
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
return [];
}
/* Метод 2: вспомогательная хеш-таблица */
function twoSumHashTable(nums, target) {
// Вспомогательная хеш-таблица, пространственная сложность O(n)
let m = {};
// Один цикл, временная сложность O(n)
for (let i = 0; i < nums.length; i++) {
if (m[target - nums[i]] !== undefined) {
return [m[target - nums[i]], i];
} else {
m[nums[i]] = i;
}
}
return [];
}
/* Driver Code */
// Метод 1
const nums = [2, 7, 11, 15],
target = 13;
let res = twoSumBruteForce(nums, target);
console.log('Результат метода 1 res = ', res);
// Метод 2
res = twoSumHashTable(nums, target);
console.log('Результат метода 2 res = ', res);