1. Add C++ codes for the chapter of
computational complexity, sorting, searching. 2. Corrected some mistakes. 3. Update README.
This commit is contained in:
parent
f85ee36ce1
commit
431a0f6caf
@ -34,7 +34,7 @@
|
||||
本书定期更新中,希望您可以一同参与到本书的创作中来,详情见 [一起参与创作](https://www.hello-algo.com/chapter_preface/contribution/) 。
|
||||
|
||||
- 如果发现笔误、无效链接、内容缺失、文字歧义、解释不清晰、行文结构不合理等问题,烦请您帮忙修正内容,以帮助其他读者获取更优质的学习内容。
|
||||
- 非常欢迎您和我一同来创作本书,包括重写某章节、新增章节等,如果有意请与我联系 WeChat: krahets-jyd , Email: krahets@163.com 。
|
||||
- 非常欢迎您和我一同来创作本书,包括重写章节、新增章节、翻译代码至不同语言等,如果有意请与我联系 WeChat: krahets-jyd , Email: krahets@163.com 。
|
||||
|
||||
如果感觉本书对你有所帮助,请点个 Star 支持一下,谢谢!
|
||||
|
||||
@ -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#, ... 寻求大佬帮助)
|
||||
- [ ] 数据结构:散列表、堆(优先队列)、图
|
||||
- [ ] 算法:搜索与回溯、选择 / 堆排序、动态规划、贪心、分治
|
||||
|
||||
|
8
codes/cpp/.gitignore
vendored
Normal file
8
codes/cpp/.gitignore
vendored
Normal 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"
|
||||
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
@ -8,9 +8,9 @@ comments: true
|
||||
|
||||
纸质书籍的两次印刷的间隔时间往往需要数年,内容更新非常不方便。</br>但在本开源 HTML 书中,内容更迭的时间被缩短至数日甚至几个小时。
|
||||
|
||||
由于作者水平有限,书中内容难免疏漏谬误,请您谅解。此外,希望您可以一同参与到本书的内容创作中来。如果发现笔误、无效链接、内容缺失、文字歧义、解释不清晰、行文结构不合理等问题,烦请您修正内容,以帮助其他读者获取更优质的学习内容。
|
||||
由于作者水平有限,书中内容难免疏漏谬误,请您谅解。此外,期待您可以一同参与本书的创作。如果发现笔误、无效链接、内容缺失、文字歧义、解释不清晰、行文结构不合理等问题,烦请您修正内容,以帮助其他读者获取更优质的学习内容。所有 [撰稿人](https://github.com/krahets/hello-algo/graphs/contributors) 将被展示在仓库主页,以感谢您对开源社区的无私奉献。
|
||||
|
||||
## 修改文字
|
||||
## 修改文字与代码
|
||||
|
||||
每个页面的右上角都有一个「编辑」按钮,你可以按照以下步骤修改文章:
|
||||
|
||||
@ -21,7 +21,7 @@ comments: true
|
||||
|
||||
![edit_markdown](contribution.assets/edit_markdown.png)
|
||||
|
||||
## 修改图片
|
||||
## 修改图片与动画
|
||||
|
||||
书中的配图无法直接修改,需要通过以下途径提出修改意见:
|
||||
|
||||
@ -29,18 +29,16 @@ comments: true
|
||||
2. 描述图片问题,应如何修改;
|
||||
3. 提交 Issue 即可,我会第一时间重新画图并替换图片。
|
||||
|
||||
## 修改代码
|
||||
|
||||
若发现代码源文件有错误,可以本地修改并提交 Pull Request :
|
||||
|
||||
1. 登录 GitHub ,并 Fork [本仓库](https://github.com/krahets/hello-algo) 至个人账号;
|
||||
2. 使用 Git 克隆该 Fork 仓库至本地;
|
||||
3. 在本地修改 `.java` , `.cpp` , `.py` 文件中的代码,并运行测试;测试完成后,请同步更新 Markdown 文章中的对应代码;
|
||||
4. 将本地更新 Commit ,并 Push 至远程仓库;
|
||||
5. 刷新仓库网页,点击 “Create pull request” 按钮发起拉取请求即可;
|
||||
|
||||
(TODO:教学视频)
|
||||
|
||||
## 创作新内容
|
||||
|
||||
「修改代码」中介绍的是完整的 Pull Request 流程,还可以用来 **重写某章节、新增章节、翻译代码至其他编程语言** 等。非常欢迎您和我一同来创作本书!
|
||||
如果您想要创作新内容,例如 **重写章节、新增章节、修改代码、翻译代码至其他编程语言** 等,那么需要实施 Pull Request 工作流程:
|
||||
|
||||
1. 登录 GitHub ,并 Fork [本仓库](https://github.com/krahets/hello-algo) 至个人账号;
|
||||
2. 进入 Fork 仓库网页,使用 `git clone` 克隆该仓库至本地;
|
||||
3. 在本地进行内容创作(建议通过运行测试来验证代码正确性);
|
||||
4. 将本地更改 Commit ,并 Push 至远程仓库;
|
||||
5. 刷新仓库网页,点击 “Create pull request” 按钮发起拉取请求(Pull Request)即可;
|
||||
|
||||
非常欢迎您和我一同来创作本书!
|
||||
|
||||
(TODO:教学视频)
|
||||
|
@ -1,52 +0,0 @@
|
||||
Hello!我是「Krahets」,是一个分享了许多题解、编写了 LeetBook《图解算法数据结构》的扣友。
|
||||
|
||||
当年秋招时,我采用 “扫雷游戏” 式的学习方法,两眼一抹黑刷题,扫到不会的 “雷” 就通过查资料把它 “排掉” ,配合周期性总结,逐渐形成了数据结构与算法的知识图景。我虽然幸运地在秋招斩获了多家大厂的 Offer ,但回想自己当初在 “扫雷式” 刷题中被炸的满头包,很是痛苦。
|
||||
|
||||
开源性是互联网行业最吸引我的地方,只需要一台 PC ,我们可以学习到几乎所有所需知识。学习算法时,我阅读了许多大佬分享的文章,获益匪浅,一直以来我也想再为开源社区做点儿什么。思考良久,我意识到一本 “前期刷题必看” 的读物可以使算法小白少走许多弯路。写作意愿滚滚袭来,那就动笔吧!
|
||||
|
||||
![hello-algo_1280_720.jpg](https://pic.leetcode.cn/1669284263-rbAMcs-hello-algo_1280_720.jpg){:width=500}
|
||||
|
||||
前往仓库 > github.com/krahets/hello-algo
|
||||
前往阅读 > https://www.hello-algo.com
|
||||
{:style="text-align: center;"}
|
||||
|
||||
---
|
||||
|
||||
## 「清晰动画讲解」 {:style="text-align: center;"}
|
||||
|
||||
借助动画介绍重点,提升知识吸收效率
|
||||
支持笔记本、平板、手机全终端阅读
|
||||
{:style="text-align: center;"}
|
||||
|
||||
![animation.gif](https://pic.leetcode.cn/1669280889-QVHgdw-animation.gif)
|
||||
|
||||
---
|
||||
|
||||
## 「代码实践导向」 {:style="text-align: center;"}
|
||||
|
||||
提供经典数据结构与算法的代码实现
|
||||
包含详细注释,皆可一键运行
|
||||
{:style="text-align: center;"}
|
||||
|
||||
![running_code.gif](https://pic.leetcode.cn/1669280958-qiDPFd-running_code.gif)
|
||||
|
||||
---
|
||||
|
||||
## 「可讨论与提问」 {:style="text-align: center;"}
|
||||
|
||||
作者定期(一般 < 72h )回复评论问题
|
||||
在评论区与小伙伴们一起学习进步
|
||||
{:style="text-align: center;"}
|
||||
|
||||
![comment.gif](https://pic.leetcode.cn/1669280943-aiwrOm-comment.gif)
|
||||
|
||||
---
|
||||
|
||||
本书是我利用业余时间创作,完全开源免费,不会收取任何费用。
|
||||
|
||||
本书定期更新中,希望您可以一同参与到本书的创作中来,详情见 [一起参与创作](https://www.hello-algo.com/chapter_preface/contribution/) 。
|
||||
|
||||
- 如果发现笔误、无效链接、内容缺失、文字歧义、解释不清晰、行文结构不合理等问题,烦请您帮忙修正内容,以帮助其他读者获取更优质的学习内容。
|
||||
- 非常欢迎您和我一同来创作本书,包括重写某章节、新增章节、翻译代码至其他编程语言等,如果有意请与我联系或在楼下评论。
|
||||
|
||||
如果感觉本书对你有所帮助,请 [点个 Star](github.com/krahets/hello-algo) 支持一下,谢谢!
|
Loading…
Reference in New Issue
Block a user