Skip to content

Commit

Permalink
1. Add C++ codes for the chapter of
Browse files Browse the repository at this point in the history
computational complexity, sorting, searching.
2. Corrected some mistakes.
3. Update README.
  • Loading branch information
krahets committed Nov 26, 2022
1 parent f85ee36 commit 431a0f6
Show file tree
Hide file tree
Showing 37 changed files with 144 additions and 91 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
本书定期更新中,希望您可以一同参与到本书的创作中来,详情见 [一起参与创作](https://www.hello-algo.com/chapter_preface/contribution/)

- 如果发现笔误、无效链接、内容缺失、文字歧义、解释不清晰、行文结构不合理等问题,烦请您帮忙修正内容,以帮助其他读者获取更优质的学习内容。
- 非常欢迎您和我一同来创作本书,包括重写某章节、新增章节等,如果有意请与我联系 WeChat: krahets-jyd , Email: [email protected]
- 非常欢迎您和我一同来创作本书,包括重写章节、新增章节、翻译代码至不同语言等,如果有意请与我联系 WeChat: krahets-jyd , Email: [email protected]

如果感觉本书对你有所帮助,请点个 Star 支持一下,谢谢!

Expand All @@ -48,7 +48,7 @@

## To-Dos

- [x] [代码翻译](https://github.com/krahets/hello-algo/issues/15)(JavaScript, TypeScript, C, C#, ... 请求大佬帮助
- [x] [代码翻译](https://github.com/krahets/hello-algo/issues/15)(JavaScript, TypeScript, C, C++, C#, ... 寻求大佬帮助
- [ ] 数据结构:散列表、堆(优先队列)、图
- [ ] 算法:搜索与回溯、选择 / 堆排序、动态规划、贪心、分治

Expand Down
8 changes: 8 additions & 0 deletions codes/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/

*.dSYM/
2 changes: 2 additions & 0 deletions codes/cpp/chapter_array_and_linkedlist/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_array_and_linkedlist/linked_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_array_and_linkedlist/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_array_and_linkedlist/my_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

47 changes: 47 additions & 0 deletions codes/cpp/chapter_computational_complexity/leetcode_two_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,50 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

class SolutionBruteForce {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int size = nums.size();
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 {};
}
};

class SolutionHashMap {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int size = nums.size();
unordered_map<int, int> dic;
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 {};
}
};


int main() {
// ======= Test Case =======
vector<int> nums = { 2,7,11,15 };
int target = 9;

// ====== Driver Code ======
// 方法一
SolutionBruteForce* slt1 = new SolutionBruteForce();
vector<int> res = slt1->twoSum(nums, target);
PrintUtil::printVector(res);
// 方法二
SolutionHashMap* slt2 = new SolutionHashMap();
res = slt2->twoSum(nums, target);
PrintUtil::printVector(res);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_searching/binary_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_searching/hashing_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_searching/linear_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_sorting/bubble_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_sorting/insertion_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_sorting/quick_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/array_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/array_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/deque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_stack_and_queue/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_tree/binary_search_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_tree/binary_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_tree/binary_tree_bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

2 changes: 2 additions & 0 deletions codes/cpp/chapter_tree/binary_tree_dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Author: Krahets ([email protected])
*/

#include "../include/include.hpp"

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ func TestTwoSum(t *testing.T) {
// ====== Driver Code ======
// 方法一:暴力解法
res := twoSumBruteForce(nums, target)
t.Log("brute force:", res)

t.Log("方法一 res =", res)
// 方法二:哈希表
res = twoSumHashTable(nums, target)
t.Log("hash table:", res)
t.Log("方法二 res =", res)
}
7 changes: 4 additions & 3 deletions codes/go/chapter_searching/linear_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
package chapter_searching

import (
. "github.com/krahets/hello-algo/pkg"
"testing"

. "github.com/krahets/hello-algo/pkg"
)

func TestLinearSearch(t *testing.T) {
Expand All @@ -15,10 +16,10 @@ func TestLinearSearch(t *testing.T) {

// 在数组中执行线性查找
index := linerSearchArray(nums, target)
t.Log("目标元素 3 的索引 = ", index)
t.Log("目标元素 3 的索引 =", index)

// 在链表中执行线性查找
head := ArrayToLinkedList(nums)
node := linerSearchLinkedList(head, target)
t.Log("目标结点值 3 的对应结点对象为 ", node)
t.Log("目标结点值 3 的对应结点对象为", node)
}
16 changes: 8 additions & 8 deletions codes/go/chapter_tree/binary_search_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import "testing"
func TestBinarySearchTree(t *testing.T) {
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
bst := NewBinarySearchTree(nums)
t.Log("初始化的二叉树为: ")
t.Log("初始化的二叉树为:")
bst.Print()

// 获取根结点
node := bst.GetRoot()
t.Log("二叉树的根结点为: ", node.Val)
t.Log("二叉树的根结点为:", node.Val)
// 获取最小的结点
node = bst.GetMin(bst.GetRoot())
t.Log("二叉树的最小结点为: ", node.Val)
t.Log("二叉树的最小结点为:", node.Val)

// 查找结点
node = bst.Search(5)
t.Log("查找到的结点对象为", node, ",结点值 = ", node.Val)
t.Log("查找到的结点对象为", node, ",结点值 =", node.Val)

// 插入结点
node = bst.Insert(16)
t.Log("插入结点后 16 的二叉树为: ")
t.Log("插入结点后 16 的二叉树为:")
bst.Print()

// 删除结点
bst.Remove(1)
t.Log("删除结点 1 后的二叉树为: ")
t.Log("删除结点 1 后的二叉树为:")
bst.Print()
bst.Remove(2)
t.Log("删除结点 2 后的二叉树为: ")
t.Log("删除结点 2 后的二叉树为:")
bst.Print()
bst.Remove(4)
t.Log("删除结点 4 后的二叉树为: ")
t.Log("删除结点 4 后的二叉树为:")
bst.Print()
}
5 changes: 3 additions & 2 deletions codes/go/chapter_tree/binary_tree_bfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
package chapter_tree

import (
. "github.com/krahets/hello-algo/pkg"
"testing"

. "github.com/krahets/hello-algo/pkg"
)

func TestLevelOrder(t *testing.T) {
Expand All @@ -18,5 +19,5 @@ func TestLevelOrder(t *testing.T) {

// 层序遍历
nums := levelOrder(root)
t.Log("层序遍历的结点打印序列 = ", nums)
t.Log("层序遍历的结点打印序列 =", nums)
}
9 changes: 5 additions & 4 deletions codes/go/chapter_tree/binary_tree_dfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
package chapter_tree

import (
. "github.com/krahets/hello-algo/pkg"
"testing"

. "github.com/krahets/hello-algo/pkg"
)

func TestPreInPostOrderTraversal(t *testing.T) {
Expand All @@ -18,13 +19,13 @@ func TestPreInPostOrderTraversal(t *testing.T) {

// 前序遍历
nums := preOrder(root)
t.Log("前序遍历的结点打印序列 = ", nums)
t.Log("前序遍历的结点打印序列 =", nums)

// 中序遍历
nums = inOrder(root)
t.Log("中序遍历的结点打印序列 = ", nums)
t.Log("中序遍历的结点打印序列 =", nums)

// 后序遍历
nums = postOrder(root)
t.Log("后序遍历的结点打印序列 = ", nums)
t.Log("后序遍历的结点打印序列 =", nums)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public static void main(String[] args) {
// 方法一
SolutionBruteForce slt1 = new SolutionBruteForce();
int[] res = slt1.twoSum(nums, target);
System.out.println(Arrays.toString(res));
System.out.println("方法一 res = " + Arrays.toString(res));
// 方法二
SolutionHashMap slt2 = new SolutionHashMap();
res = slt2.twoSum(nums, target);
System.out.println(Arrays.toString(res));
System.out.println("方法二 res = " + Arrays.toString(res));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
# ====== Driver Code ======
# 方法一
res = SolutionBruteForce().twoSum(nums, target);
print(res)
print("方法一 res =", res)
# 方法二
res = SolutionHashMap().twoSum(nums, target);
print(res)
print("方法二 res =", res)
Loading

0 comments on commit 431a0f6

Please sign in to comment.