未验证 提交 38bcd21f 编写于 作者: S ShuoZheLi 提交者: GitHub

算法学习之路全文翻译

算法学习之路全文翻译
上级 cecd9f92
# The way to Algorithm learning
I have published an article about the ideal framework. People from the community have give praised me. I have never thought so many people will be agree with me. I will work harder to write more easily understanding Algorithm articles.
Many friends asked me how should I learn Data Structure and Algorithm. Especially those who are beginner feel exhausted when going through all the LeetCode questions even after reading my article about the ideal framework. So, they hope to explain and tell them the way how I begin.
First of all, I want to congrats my friends who are asking me for it because you have learned that you need external help from experienced people. And, you already start to practice Algorithm problems. Remember, there are not many people made to this step.
For the ideal framework, a beginner may not be easy to understand. But if you are able to understand it, you are not a beginner anymore :D! It just like software engineering. People like me who never lead a group project feel so bored. But, for someone who has led a team. He/she will treat every sentence in software engineering as a treasure. (if you do not understand this, it is fine for now)
Now, I will go through my experience.
# if you have read many articles such as "how to practice LeetCode" and "how to study Algorithm", but you still cannot keep up. Then, this article is for you.
When I begin to learn Data Structure and Algorithm, I am always having knowledge gaps in my mind.
If we summary them into two questions:
1.what is it?
2.what is that for?
For example, if you learned the word stack, your teacher may tell you "first in last out" or "function stack." However, all these are like literature words. They cannot answer your question.
Here is how to answer them:
1.what is it? Go read the textbook
2.what is that for? practice some coding questions
**1.what the hell is it?**
This question is easy to solve. You just need to read a chapter of the book. And then make your own Queue or Stack.
If you can understand the first half of the ideal framework: Data structure is just the combination of array and linked-list. All the operations are just add, remove, search, modify.
For example, Queue is just made by an array or linked-list. For enqueue and dequeue operations are just add and remove function in these data type. You do not even need to writer a new operation for them.
**2.what is that for?**
This problem covers the design of Algorithm. This will take a long time to make through. You need to practice a lot of questions and train yourself to think like a computer.
The previous article has said. Algorithm is just about how to use data structure. The frequent Algorithm question are just a few types. many problems just changed a few conditions. Practice problems just help to see the pattern in questions. With the pattern in your head, you can solve problems with your own framework. Feels like plug in numbers into an equation.
For example, if you need to escape a maze, you need to treat the problem abstractly.
maze -> graph search -> tree search -> binary tree search
then you just apply your own framework
You need to abstract and categorize the problem when you are practicing LeetCode. This will help you to find the pattern.
**3. how to read a book**
Let me just recommend a book to you.
Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne
If you can need 50% of the book, you are at the average level. Do think the thickness of the book because you only need to read two-third of the book.
Now let's talk about how to read it.
Reading the book using recursively: from the top to bottom, step by step to divide the problem.
This is book has a good structure for beginners, so you can read from the first chapter to the end. **Make sure you typed down and run all the code in the book** Because these are really just the basic elements of Algorithm. Do not just think you can understand them without doing it. But, if you know the basics, you can jump the beginning. You can also jump the math proving part and practice part. This shall make the book less thick.
There is a trick to read a book. You should not be stoped by details. You need to build a big knowledge framework first.
**keep move on, do not get stuck by details**
Well, the ending part of the book is very hard. There are some famous Algorithms such as Knuth–Morris–Pratt algorithm (KMP) and Regular expression. These are not useful for doing LeetCode so you do not have to know them.
**4. how to practice problem**
1.there is not a linearly relationship between Algorithm and Math ability and program language does not really matter. Algorithm is just a way to think. To think like a computer is just like riding a bike. You have to think you are walking with two wheels, not two feet.
LeetCode problem does not like the classic Algorithm we talked about before. Leetcode problems are more like brain teasers.
For example, you need to use a queue to make a stack or oppositely make a stack with a queue. Add two numbers without using add sign.
Although these questions are useless, to solve them you still need a solid understanding of data structure and basic knowledge. This is why the companys ask them.
For the beginner, **you should go to the "Explore" in menu and start on "learn"**
This will help you to go through the basic data structure and Algorithm. There are lectures and corresponding practice problems.
Recently, the "learn" part added something new like Ruby and Machine learning. You have no need to worry about that. You just need to finish the basic part. Then, you can just go directly to the "Interview" problem part.
No matter you start with "Explore" or "Problems". You better practice problems by types. For example, you can finish all the problem in the linked-list then jump to binary tree problem. This helps you find the framework and pattern and practice applying them.
**5. I know what you said, but I cannot keep up**
This is all bout what you really want. You need to activate your desire!
!! what I am saying is not a hobby but desire!!
Let me take myself as an example
Half-year ago I start to practice problems for a job after graduation. But, most of the people start it when they almost graduate.
I know I am not smart so I start early. I have a strong desire for a decent job. I have girl who I want to be in love and I have made boasts in front of my friend. So, I have to achieve it to earn the money the fame I want. The desire for money and for fame has made to work harder and harder.
But, I am the kind of person who does not good at doing things quickly. I understand things slowly. Therefore, I decide to wake up early and start everything ahead. In fact, if you keep focusing on something for only just a month. You can see your improvement by your eye.
Also, as a person who likes to share, I find out what I said actually helps others. This gives me recognition too! This is also what I want!! So, I decided to write more about what I experienced and share them on WeChat.
Above, it is not only about Algorithm learning. We, as a human being, are driven by our desires. There must be a thing that is tangible to help us to keep up. We have to benefit directly form it! This should be a simple reason to keep up for what we want to achieve.
**You can find me on Wechat official account labuladong**:
all my friend, labuladong loves you.
![labuladong](../pictures/labuladong.png)
[上一篇:队列实现栈\|栈实现队列](../数据结构系列/队列实现栈栈实现队列.md)
[下一篇:回溯算法详解](../算法思维系列/回溯算法详解修订版.md)
[目录](../README.md#目录)
# 算法学习之路
之前发的那篇关于框架性思维的文章,我也发到了不少其他圈子,受到了大家的普遍好评,这一点我真的没想到,首先感谢大家的认可,我会更加努力,写出通俗易懂的算法文章。
有很多朋友问我数据结构和算法到底该怎么学,尤其是很多朋友说自己是「小白」,感觉这些东西好难啊,就算看了之前的「框架思维」,也感觉自己刷题乏力,希望我能聊聊我从一个非科班小白一路是怎么学过来的。
首先要给怀有这样疑问的朋友鼓掌,因为你现在已经「知道自己不知道」,而且开始尝试学习、刷题、寻求帮助,能做到这一点本身就是及其困难的。
关于「框架性思维」,对于一个小白来说,可能暂时无法完全理解(如果你能理解,说明你水平已经不错啦,不是小白啦)。就像软件工程,对于我这种没带过项目的人来说,感觉其内容枯燥乏味,全是废话,但是对于一个带过团队的人,他就会觉得软件工程里的每一句话都是精华。暂时不太理解没关系,留个印象,功夫到了很快就明白了。
下面写一写我一路过来的一些经验。如果你已经看过很多「如何高效刷题」「如何学习算法」的文章,却还是没有开始行动并坚持下去,本文的第五点就是写给你的。
我觉得之所以有时候认为自己是「小白」,是由于知识某些方面的空白造成的。具体到数据结构的学习,无非就是两个问题搞得不太清楚:**这是啥?有啥用?**
举个例子,比如说你看到了「栈」这个名词,老师可能会讲这些关键词:先进后出、函数堆栈等等。但是,对于初学者,这些描述属于文学词汇,没有实际价值,没有解决最基本的两个问题。如何回答这两个基本问题呢?回答「这是啥」需要看教科书,回答「有啥用」需要刷算法题。
**一、这是啥?**
这个问题最容易解决,就像一层窗户纸,你只要随便找本书看两天,自己动手实现一个「队列」「栈」之类的数据结构,就能捅破这层窗户纸。
这时候你就能理解「框架思维」文章中的前半部分了:数据结构无非就是数组、链表为骨架的一些特定操作而已;每个数据结构实现的功能无非增删查改罢了。
比如说「列队」这个数据结构,无非就是基于数组或者链表,实现 enqueue 和 dequeue 两个方法。这两个方法就是增和删呀,连查和改的方法都不需要。
**二、有啥用?**
解决这个问题,就涉及算法的设计了,是个持久战,需要经常进行抽象思考,刷算法题,培养「计算机思维」。
之前的文章讲了,算法就是对数据结构准确而巧妙的运用。常用算法问题也就那几大类,算法题无非就是不断变换场景,给那几个算法框架套上不同的皮。刷题,就是在锻炼你的眼力,看你能不能看穿问题表象揪出相应的解法框架。
比如说,让你求解一个迷宫,你要把这个问题层层抽象:迷宫 -> 图的遍历 -> N 叉树的遍历 -> 二叉树的遍历。然后让框架指导你写具体的解法。
抽象问题,直击本质,是刷题中你需要刻意培养的能力。
**三、如何看书**
直接推荐一本公认的好书,《算法第 4 版》,我一般简写成《算法4》。不要蜻蜓点水,这本书你能选择性的看上 50%,基本上就达到平均水平了。别怕这本书厚,因为起码有三分之一不用看,下面讲讲怎么看这本书。
看书仍然遵循递归的思想:自顶向下,逐步求精。
这本书知识结构合理,讲解也清楚,所以可以按顺序学习。**书中正文的算法代码一定要亲自敲一遍**,因为这些真的是扎实的基础,要认真理解。不要以为自己看一遍就看懂了,不动手的话理解不了的。但是,开头部分的基础可以酌情跳过;书中的数学证明,如不影响对算法本身的理解,完全可以跳过;章节最后的练习题,也可以全部跳过。这样一来,这本书就薄了很多。
相信读者现在已经认可了「框架性思维」的重要性,这种看书方式也是一种框架性策略,抓大放小,着重理解整体的知识架构,而忽略证明、练习题这种细节问题,即**保持自己对新知识的好奇心,避免陷入无限的细节被劝退。**
当然,《算法4》到后面的内容也比较难了,比如那几个著名的串算法,以及正则表达式算法。这些属于「经典算法」,看个人接受能力吧,单说刷 LeetCode 的话,基本用不上,量力而行即可。
**四、如何刷题**
首先声明一下,**算法和数学水平没关系,和编程语言也没关系**,你爱用什么语言用什么。算法,主要是培养一种新的思维方式。所谓「计算机思维」,就跟你考驾照一样,你以前骑自行车,有一套自行车的规则和技巧,现在你开汽车,就需要适应并练习开汽车的规则和技巧。
LeetCode 上的算法题和前面说的「经典算法」不一样,我们权且称为「解闷算法」吧,因为很多题目都比较有趣,有种在做奥数题或者脑筋急转弯的感觉。比如说,让你用队列实现一个栈,或者用栈实现一个队列,以及不用加号做加法,开脑洞吧?
当然,这些问题虽然看起来无厘头,实际生活中也用不到,但是想解决这些问题依然要靠数据结构以及对基础知识的理解,也许这就是很多公司面试都喜欢出这种「智力题」的原因。下面说几点技巧吧。
**尽量刷英文版的 LeetCode**,中文版的“力扣”是阉割版,不仅很多题目没有答案,而且连个讨论区都没有。英文版的是真的很良心了,很多问题都有官方解答,详细易懂。而且讨论区(Discuss)也沉淀了大量优质内容,甚至好过官方解答。真正能打开你思路的,很可能是讨论区各路大神的思路荟萃。
PS:**如果有的英文题目实在看不懂,有个小技巧**,你在题目页面的 url 里加一个 -cn,即 https://leetcode.com/xxx 改成 https://leetcode-cn.com/xxx,这样就能切换到相应的中文版页面查看。
对于初学者,**强烈建议从 Explore 菜单里最下面的 Learn 开始刷**,这个专题就是专门教你学习数据结构和基本算法的,教学篇和相应的练习题结合,不要太良心。
最近 Learn 专题里新增了一些内容,我们挑数据结构相关的内容刷就行了,像 Ruby,Machine Learning 就没必要刷了。刷完 Learn 专题的基础内容,基本就有能力去 Explore 菜单的 Interview 专题刷面试题,或者去 Problem 菜单,在真正的题海里遨游了。
无论刷 Explore 还是 Problems 菜单,**最好一个分类一个分类的刷,不要蜻蜓点水**。比如说这几天就刷链表,刷完链表再去连刷几天二叉树。这样做是为了帮助你提取「框架」。一旦总结出针对一类问题的框架,解决同类问题可谓是手到擒来。
**五、道理我都懂,还是不能坚持下去**
这其实无关算法了,还是老生常谈的执行力的问题。不说什么破鸡汤了,我觉得**解决办法就是「激起欲望」**,注意我说的是欲望,而不是常说的兴趣,拿我自己说说吧。
半年前我开始刷题,目的和大部分人都一样的,就是为毕业找工作做准备。只不过,大部分人是等到临近毕业了才开始刷,而我离毕业还有一阵子。这不是炫耀我多有觉悟,而是我承认自己的极度平凡。
首先,我真的想找到一份不错的工作(谁都想吧?),我想要高薪呀!否则我在朋友面前,女神面前放下的骚话,最终都会反过来啪啪地打我的脸。我也是要恰饭,要面子,要虚荣心的嘛。赚钱,虚荣心,足以激起我的欲望了。
但是,我不擅长 deadline 突击,我理解东西真的慢,所以干脆笨鸟先飞了。智商不够,拿时间来补,我没能力两个月突击,干脆拉长战线,打他个两年游击战,我还不信耗不死算法这个强敌。事实证明,你如果认真学习一个月,就能够取得肉眼可见的进步了。
现在,我依然在坚持刷题,而且为了另外一个原因,这个公众号。我没想到自己的文字竟然能够帮助到他人,甚至能得到认可。这也是虚荣心啊,我不能让读者失望啊,我想让更多的人认可(夸)我呀!
以上,不光是坚持刷算法题吧,很多场景都适用。执行力是要靠「欲望」支撑的,我也是一凡人,只有那些看得见摸得着的东西才能使我快乐呀。读者不妨也尝试把刷题学习和自己的切身利益联系起来,这恐怕是坚持下去最简单直白的理由了。
**致力于把算法讲清楚!欢迎关注我的微信公众号 labuladong,查看更多通俗易懂的文章**
![labuladong](../pictures/labuladong.png)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册