Add cpp codes for the chapter
computational complexity, sorting, searching.
This commit is contained in:
@@ -10,6 +10,7 @@ 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)
|
||||
@@ -24,7 +25,9 @@ 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 };
|
||||
@@ -45,9 +48,13 @@ int main() {
|
||||
// 方法一
|
||||
SolutionBruteForce* slt1 = new SolutionBruteForce();
|
||||
vector<int> res = slt1->twoSum(nums, target);
|
||||
cout << "方法一 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
// 方法二
|
||||
SolutionHashMap* slt2 = new SolutionHashMap();
|
||||
res = slt2->twoSum(nums, target);
|
||||
cout << "方法二 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,97 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 函数 */
|
||||
int func() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
// 线性阶
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 平方阶
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数阶
|
||||
TreeNode* root = buildTree(n);
|
||||
PrintUtil::printTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,161 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(vector<int>& nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
int n = 8;
|
||||
cout << "输入数据大小 n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "常数阶的计算操作数量 = " << count << endl;
|
||||
|
||||
count = linear(n);
|
||||
cout << "线性阶的计算操作数量 = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "线性阶(遍历数组)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "平方阶的计算操作数量 = " << count << endl;
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
cout << "平方阶(冒泡排序)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "指数阶(循环实现)的计算操作数量 = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "指数阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = logarithmic((float) n);
|
||||
cout << "对数阶(循环实现)的计算操作数量 = " << count << endl;
|
||||
count = logRecur((float) n);
|
||||
cout << "对数阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = linearLogRecur((float) n);
|
||||
cout << "线性对数阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "阶乘阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,39 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 生成一个数组,元素为 { 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user