Add cpp codes for the chapter
computational complexity, sorting, searching.
This commit is contained in:
@@ -56,7 +56,26 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
/* 结构体 */
|
||||
struct Node {
|
||||
int val;
|
||||
Node *next;
|
||||
Node(int x) : val(x), next(nullptr) {}
|
||||
};
|
||||
|
||||
/* 函数(或称方法) */
|
||||
int func() {
|
||||
// do something...
|
||||
return 0;
|
||||
}
|
||||
|
||||
int algorithm(int n) { // 输入数据
|
||||
const int a = 0; // 暂存数据(常量)
|
||||
int b = 0; // 暂存数据(变量)
|
||||
Node* node = new Node(0); // 暂存数据(对象)
|
||||
int c = func(); // 栈帧空间(调用函数)
|
||||
return a + b + c; // 输出数据
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -74,7 +93,6 @@ comments: true
|
||||
return 0
|
||||
|
||||
def algorithm(n): # 输入数据
|
||||
a = 0 # 暂存数据(常量)
|
||||
b = 0 # 暂存数据(变量)
|
||||
node = Node(0) # 暂存数据(对象)
|
||||
c = function() # 栈帧空间(调用函数)
|
||||
@@ -104,7 +122,12 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
vector<int> b(10000); // O(1)
|
||||
if (n > 10)
|
||||
vector<int> nums(n); // O(n)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -142,7 +165,21 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
int func() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
/* 循环 O(1) */
|
||||
void loop(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
/* 递归 O(n) */
|
||||
void recur(int n) {
|
||||
if (n == 1) return;
|
||||
return recur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -212,7 +249,22 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 常数阶 */
|
||||
void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode* node = new ListNode(0);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -259,7 +311,21 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
vector<int> nums(n);
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
vector<ListNode*> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(new ListNode(i));
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
unordered_map<int, string> map;
|
||||
for (int i = 0; i < n; i++) {
|
||||
map[i] = to_string(i);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -291,7 +357,12 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -318,7 +389,7 @@ $$
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
int numMatrix[][] = new int[n][n];
|
||||
int [][]numMatrix = new int[n][n];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -334,7 +405,18 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.push_back(0);
|
||||
}
|
||||
numMatrix.push_back(tmp);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -363,7 +445,13 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -401,7 +489,14 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode* buildTree(int n) {
|
||||
if (n == 0) return nullptr;
|
||||
TreeNode* root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
@@ -39,7 +39,20 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="leetcode_two_sum.cpp"
|
||||
|
||||
class SolutionBruteForce {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return { i, j };
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -101,7 +114,22 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="leetcode_two_sum.cpp"
|
||||
|
||||
class SolutionHashMap {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return { dic[target - nums[i]], i };
|
||||
}
|
||||
dic.emplace(nums[i], i);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
@@ -36,7 +36,16 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
// 在某运行平台下
|
||||
void algorithm(int n) {
|
||||
int a = 2; // 1 ns
|
||||
a = a + 1; // 1 ns
|
||||
a = a * 2; // 10 ns
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++
|
||||
cout << 0 << endl; // 5 ns
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -88,7 +97,22 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
// 算法 A 时间复杂度:常数阶
|
||||
void algorithm_A(int n) {
|
||||
cout << 0 << endl;
|
||||
}
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void algorithm_B(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << 0 << endl;
|
||||
}
|
||||
}
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void algorithm_C(int n) {
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
cout << 0 << endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -144,7 +168,15 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 1; // +1
|
||||
a = a + 1; // +1
|
||||
a = a * 2; // +1
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
|
||||
cout << 0 << endl; // +1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -233,7 +265,20 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++) {
|
||||
cout << 0 << endl;
|
||||
}
|
||||
// +n*n(技巧 3)
|
||||
for (int i = 0; i < 2 * n; i++) {
|
||||
for (int j = 0; j < n + 1; j++) {
|
||||
cout << 0 << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -310,7 +355,14 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -344,7 +396,13 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -381,7 +439,15 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(vector<int>& nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -419,7 +485,17 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -471,7 +547,24 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(vector<int>& nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -522,7 +615,19 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -559,7 +664,11 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -596,7 +705,15 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -630,7 +747,11 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -666,7 +787,16 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -714,7 +844,16 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -789,7 +928,42 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="worst_best_time_complexity.cpp"
|
||||
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(n);
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 使用系统时间生成随机种子
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
// 随机打乱数组元素
|
||||
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(vector<int>& nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int n = 100;
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\n数组 [ 1, 2, ..., n ] 被打乱后 = ";
|
||||
PrintUtil::printVector(nums);
|
||||
cout << "数字 1 的索引为 " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
Reference in New Issue
Block a user