1. Add C++ codes for the chapter of

computational complexity, sorting, searching.
2. Corrected some mistakes.
3. Update README.
This commit is contained in:
Yudong Jin
2022-11-27 04:19:16 +08:00
parent f85ee36ce1
commit 431a0f6caf
37 changed files with 147 additions and 94 deletions
+8
View File
@@ -0,0 +1,8 @@
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
*.dSYM/
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,50 @@
* Author: Krahets (krahets@163.com)
*/
#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);
}
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
+2
View File
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
+2
View File
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
+2
View File
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -4,3 +4,5 @@
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
@@ -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)
}
@@ -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) {
@@ -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)
}
@@ -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,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) {
@@ -18,5 +19,5 @@ func TestLevelOrder(t *testing.T) {
// 层序遍历
nums := levelOrder(root)
t.Log("层序遍历的结点打印序列 = ", nums)
t.Log("层序遍历的结点打印序列 =", nums)
}
@@ -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) {
@@ -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)
}
@@ -48,10 +48,10 @@ public class leetcode_two_sum {
// 方法一
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));
}
}
@@ -35,7 +35,7 @@ if __name__ == '__main__':
# ====== Driver Code ======
# 方法一
res = SolutionBruteForce().twoSum(nums, target);
print(res)
print("方法一 res =", res)
# 方法二
res = SolutionHashMap().twoSum(nums, target);
print(res)
print("方法二 res =", res)