Git Product home page Git Product logo

leetcode_101's Introduction

LeetCode 101:和你一起你轻松刷题(C++)

一个面向有C++编程基础,但缺乏刷题经验的读者的教科书和工具书(不适合编程小白喔)。

永久免费,禁止任何盈利性利用,欢迎传阅和指正。为防止不被告知的二次修改,本书暂不放出Latex源代码。

作者:高畅 Chang Gao

最新版本:1.22

思维导图

overview

打赏链接

如果您觉得这本书对您有帮助,不妨打赏一下作者哟~~~以下是微信打赏二维码

版本更新历史

欢迎在Github Issue中发布勘误、建议、以及对算法边界情况的讨论,请尽量避免请求帮忙debug。

另:由于我早已开始全职工作,并没有太多闲余时间,所有涉及题目更换、难度变化的更新请求不再回复,但仍会就书中可能存在的问题和不清楚的地方进行修改。

  • ✅ Dec 24 2019:发布预览版 0.01
    • ✅ 序
    • ✅ 目录
    • ✅ 101道题目和题解
    • ✅ 练习题题目挑选
  • ✅ Mar 1 2020:发布预览版 0.02
    • ✅ 大部分题目的中文题解讲解
  • ✅ Mar 12 2020:发布预览版 0.03
    • ✅ 所有题目的中文题解讲解
    • ✅ 大部分的文字解释部分完成
  • ✅ Mar 12 2020:发布预览版 0.04
    • ✅ 格式和文字调整(关键词加下划线,加图)
  • ✅ Mar 13 2020:发布正式版 1.00
    • ✅ 正式版第一次校对,优化文笔
  • ✅ Apr 29 2021:最新版本 1.11
    • ✅ 持续校对,收集意见
  • ✅ May 28 2021:最新版本 1.12
    • ✅ 持续校对,收集意见
  • ✅ Dec 27 2021:最新版本 1.14
    • ✅ 持续校对,收集意见
  • ✅ Apr 8 2022:最新版本 1.19
    • ✅ 持续校对,收集意见
  • ✅ Apr 27 2022:最新版本 1.19
    • ✅ 持续校对,收集意见
  • ✅ Jun 17 2022:最新版本 1.21
    • ✅ 持续校对,收集意见
  • ✅ Aug 25 2022:最新版本 1.22
    • ✅ 持续校对,收集意见

leetcode_101's People

Contributors

changgyhub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leetcode_101's Issues

二分查找 P15 第一种方法

作者你好, 首先非常感谢您分享的letcode101,让我获益匪浅.
但是其中有一个解让我不是很明白,希望指点一下,
关于二分查找 P15 第一种方法, 我有些疑问
实际上如果a = 8的时候, while是个死循环,
因为此时mid = 4, 无论 right-1或者left-1 都是3, 然鹅

  mid = left + (right - 1) / 2;

让下一个mid又是4...如此循环往复.....

次数 right left mid sqrt
1 1 8 4 0
2 1 3 2 2
3 3 3 4 4
4 3 3 4 2
5 3 3 4 2
...

我的代码如下

class Solution {
    public int mySqrt(int a) {
        if(a == 0) return a;
        int left = 1, right = a, mid, sqrt;
        while(left <= right){
            mid = left + (right - 1) / 2;
            sqrt = a / mid;
            if(sqrt == mid){
                return mid;
            }else if(mid > sqrt){
                right = mid - 1;
            }else{
                left = mid + 1;
            }
        }
        return right;
    } 

希望大佬指点一下我哪里做错了...非常感谢!!!

There may be overflow in the binary search function at page16

First I want to say this is an excellent job! Thank you for sharing this!
At page 16, for question 34. Find First and Last Position of Element in Sorted Array (Medium), the function lower_bound below
may have a bug:

int lower_bound(vector<int> &nums, int target) {
int l = 0, r = nums.size(), mid;
while (l < r) {
mid = (l + r) / 2; // Here, may be overflow when r = MAX_INT and l= MAX_INT/2
if (nums[mid] >= target) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}

mid = (l + r) / 2; This code may be overflow when r = MAX_INT and l= MAX_INT/2, you should use mid = (r-l)/2+l instead.

Also the same for the upper_bound function.

【勘误】p55页代码存在问题

也就是题目416中的非压缩算法存在问题,j循环时未考虑比nums[i-1]小的问题。
for (int i = 1; i <= n; ++i) {
for (int j = nums[i-1]; j <= target; ++j) {
dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i-1]];
}
}
测试用例:[1,5,10,6],应该是true,使用此代码是false;
1616469943
压缩空间的算法是正确的。

[作者留言] 意见征集

单独开一贴征集一些意见吧。

=================

笔者自Waymo入职已一年有余,闲来重看此书,忽觉代码风格实在不够标准严谨。如果有空会按照Google Code Style重写一遍代码。

近来实在没有太多空闲时间,有时想写点别的书却抽不出时间。工作后确实很难在完成工作内容、锻炼身体、玩游戏之外再有余力。如果有长假期,可能考虑稍微写一些关于无人车的内容吧,在不泄露任何公司机密的情况下给各位读者介绍一下行业的现状。

另外如果你在读完此书后顺利通过面试而入职,又恰好在硅谷,请备注私信我,我请你吃炸鸡。

关于中文书名的小疑问

这个问题可能不值得开issue,它就是一个关于书名的小疑问:
为什么不叫”....和你一起轻松刷题“,而是”和你一起你轻松刷题“(有点别扭)呢?
刚下载打开看的时候,还以为下到了”二次排版“的盗版PDF。

感谢作者!

感谢作者!非常赞的题解!不知道作者有没有Python Version的计划?

P34 N皇后问题

您好,我有一点疑问想请教一下。
在N皇后问题的题解中,您表示有对角线的方法为rdiag[row+i+1], row范围为[0,n-1] i范围为[0,n-1]。
但是rdiag初始化为2*n-1大小的vector。假设n=3,那么row+i+1最大可能为5,而rdiag大小为5,最大下标只有4。
所以是否存在数组下标越界的可能呢?(但是我复制您的代码到leetcode却也能AC)
按我的理解应该是rdiag[row+i]。

[勘误] P17的题目描述

“判断这个值是否存在于这个为旋转数组中。”

是不是应该把 "为" 字去掉?

(虽然问题不大

书下载问题

能麻烦作者整一个网盘下载么,github中的下载网址总是加载错误,谢谢了

关于二进制文件放在release页面更好这件事

感谢作者。尤其是使用了

ElegantBook提供的精美LATEX模版

几年前我看到这个模板,我就一直想用这个模板排版点内容啥的,做做笔记之类的,不过一直没有合适的内容。
现在看到作者的成品,就像cuda程序员还顺手写了opencl实现一样,让我觉得非常高兴。

但是把pdf这种二进制文件用git管理确实不太方便啊。正好tex用法也可以理解为编译,pdf是编译出的二进制文件,那么放在release环节更加自然。

勘误,p34,51题,N-Queens,存在越界的问题

代码再C++中不会报错,但我在java中发现,会存在数组越界问题。
vector column(n, false), ldiag(2n-1, false), rdiag(2n-1, false);
当n为8时,在下面的for循环中rdiag[row+i+1],使其会存在取到rdiag[15]的情况,改为rdiag[row+i]后不再出错。
据说c++在[]时不会进行数组越界判断。
1618276123

小小的问题

作者您好 我是今天刚看到这本书 觉得写的很好 就是一开始第一题的时候 讲满足条件的饼干和孩子 我觉得[1,1] [2,2]这两个组和是不是也应该加进去 因为题目说的不是大于等于

意见反馈

个人觉得14.3中对二叉查找树的文字定义存在错误

【勘误】455. Assign Cookies (Easy)

原题的题意应该包含孩子的饥饿度等于饼干的大小也符合题中要求,并且在你的代码中:
if (children[child] <= cookies[cookie]) ++child;
体现出了两者等于的情况。
但是在你的题目描述中没有包含该情况。
Snipaste_2021-02-14_02-38-19

纠错

image在BTS中查找一个值的时间复杂度应该是O(log n)

可能的问题

在背包问题中的注意段落 最后一句写 “完全背包对物品的迭代放在里层,外层的体积或价值正向遍历。” 作者的code写的也是外层是物品的贴袋 内层是体积或者价值, 不知道是我理解错了还是这里没写清楚,请指正。

谢谢分享这本书给大家。

P50 子序列问题

在P50给出的更新例子中,对于每轮的更新查找情况:
当num为5时,dp数组是否应该更新为[2,5],而不是[2],抑或是我理解错误了?谢谢。

error: use of class template 'hash' requires template arguments, page 94-95.

When I compiled the code using -std=c++11, I got the following error:
error: use of class template 'hash' requires template arguments
return hash(obj, hash_table.size());
I'm thinking there would be an implicit type casting involved in the myhash's call to hash. Would that be the problem?

Btw, I really admire your work and it has helped me a ton!

P58 2KeysKeyboard

第二重循环的轮数如果是sqrt(i)而不是h=sqrt(n)应该会快一些,觉得取h=sqrt(n)有点奇怪,当然也是可以AC的。或者第二重循环判断条件改为j*j<=i也行。

P56 322、Coin Change

您好,在P56的322题中,我觉得结果的最大可取值应该为amount,也就是全部都用面额为1的硬币,所以dp的初始值应该可以为amount+1,(当然amount+2也可以~)。或者说这里还有其他需要考虑的地方,导致只能取amount+2?谢谢。

关于6.2深度优先搜索 695.Max Area of Island 第一种递归解法

在您的书 P26/143的上半页写到: "一种是先判定是否越界,只有在合法的情况下才进行下一步搜索(即判断放在调用 递归函数前)", 对应的代码为:

// 主函数
int maxAreaOfIsland(vector<vector<int>>& grid) {
    if (grid.empty() || grid[0].empty()) return 0;
    int max_area = 0;
    for (int i = 0; i < grid.size(); ++i) {
       for (int j = 0; j < grid[0].size(); ++j) {
           if (grid[i][j] == 1) {
              max_area = max(max_area, dfs(grid, i, j));
           }
} }
    return max_area;
}
// 辅函数
int dfs(vector<vector<int>>& grid, int r, int c) {
    if (grid[r][c] == 0) return 0;
    grid[r][c] = 0;
    int x, y, area = 1;
    for (int i = 0; i < 4; ++i) {
       x = r + direction[i], y = c + direction[i+1];
       if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) {
           area += dfs(grid, x, y);
       }
}
    return area;
}

由于在主函数中已经进行了 if (grid[i][j] == 1)判断才能进入递归,所以我之前想,在helper函数中第一行if (grid[r][c] == 0) return 0;是否多余,删除之后提交出现StackOverflow的问题。

这里就不是很理解了,如果是主函数中先判断,通过条件之后才递归,为什么在helper函数中仍然需要判断?(如果是不管三七二十一搜索,那么helper中肯定需要判断,这个好理解)

希望您这里能够做出详细一点的解释。我想应该不止我一个有这个问题

p100页,题目304题解描述有问题

此处intergral矩阵描述的其实有些问题:
如果按代码中定义的是intergral是m+1和n+1,intergral[i][j]应该表示的是以位置 (0, 0) 为左上角、位置 (i-1, j-1) 为右下角的长方形中所有数字的和,否则和下面的递推公式不好对应。
1619507947(1)

纠错

page17
4.4旋转数组查找数字
81. Search in Rotated Sorted Array (Medium)给出的解析中,mid取值方法 int mid = (start + end) /2;可能会造成溢出,可以更改为int mid = left + ((right-left)>>1);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.