From 5a79e97714627b5f232367a2bae09c5feb57eed0 Mon Sep 17 00:00:00 2001 From: MYP Date: Mon, 21 Sep 2020 14:55:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E5=86=85=E5=AE=B9=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../07 Leetcode同步练习(二).md | 12 + DataStructureAndAlgorithm/09 栈与递归.md | 677 ++++++++++++------ 2 files changed, 465 insertions(+), 224 deletions(-) diff --git a/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md b/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md index 7c887f8..91b09a6 100644 --- a/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md +++ b/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md @@ -30,6 +30,18 @@ : , Ϊ 01 һ ``` +**д** + +```c +public class Solution +{ + public bool IsPalindrome(int x) + { + + } +} +``` + : ܲתΪַ diff --git a/DataStructureAndAlgorithm/09 栈与递归.md b/DataStructureAndAlgorithm/09 栈与递归.md index 12df63c..d4f21d5 100644 --- a/DataStructureAndAlgorithm/09 栈与递归.md +++ b/DataStructureAndAlgorithm/09 栈与递归.md @@ -1,44 +1,38 @@ +# 09 ջݹ -# Task03ջݹ飨2죩 +**֪ʶṹ** -ջǾʹõһݽṹͼʾǹӵ˳ӵѹ뵯е˳෴ѹ뵯еӵȷ - -![](https://img-blog.csdnimg.cn/20191222213300699.png) - -ʹõWordExcelPhotoshopϵͳеijҲջľӦãIJһȳġǾϸܡջݽṹ +![ͼ1 ֪ʶṹ](https://img-blog.csdnimg.cn/20200920184422236.png) +ջǾʹõһݽṹ磬ǹӵ˳ӵѹ뵯е˳෴ѹ뵯еӵȷֱ磬ʹõWordExcelPhotoshopϵͳеijҲջľӦãIJһȳġ + +Ǿϸܡջݽṹ + +--- ## 1. ջĶ - -**1.1 ջĶ** +### 1.1 ջĶ 루ջɾջֻһˣջеԱȽFirst In Last OutԱ -1 Ա`(a0,a1,...,an)` ջջʾ +1 Ա`$(a_0,a_1,?,a_{n-1)}$`ջջʾ -![˳ģջջ](https://img-blog.csdnimg.cn/20191222213645860.png) +![ͼ2 ˳ģջջ](https://img-blog.csdnimg.cn/20191222213645860.png) -![ģջջ](https://img-blog.csdnimg.cn/20191222213749217.png) +![ͼ3 ģջջ](https://img-blog.csdnimg.cn/20191222213749217.png) -ʾջʵһ˳ջһջʵַʽʲôأʵ˳һģ -- ˳ջǾ̬ĵջǶ̬ģԱȽջڿռʸߡΪ˳ջ˽ϴĿռ䵫Dzûȫ洢Ԫء -- ˳ջȻô洢ָȽջ˵Ϊʡڴռ䣬ջȴԽڴռ -- Ҷڴ洢δ֪£ջʺϣΪջͨջ -- ˳˵ڲɾЧʸߣ˳ڲЧʸߡǶջ˵ֻջв޷Чʸߡ -**1.2 ջIJ** +### 1.2 ջIJ -- ջԪֵջ +- ջԪزջ - ջƳջԪء - ǷΪգжջǷԪء - õջȡջʵʰԪصĸ - ղƳջеԪء - ȡջԪء -![ջӿ](https://img-blog.csdnimg.cn/20191222214544845.png) - -´Ϊ`C#`汾 +![ͼ4 ջӿ](https://img-blog.csdnimg.cn/20191222214544845.png) ```c using System; @@ -86,22 +80,15 @@ namespace LinearStruct } ``` - - - - - -## 2. ջĴ洢ʵ - -**2.1 ˳洢˳ջ** +--- +## 2. ջ˳洢˳ջ ˳ջ˳ʵֵջ ʵ֣ -![˳ջ](https://img-blog.csdnimg.cn/20191222214937292.png) +![ͼ5 ˳ջ](https://img-blog.csdnimg.cn/20200920163058627.png) -´Ϊ`C#`汾 ```c using System; @@ -196,16 +183,185 @@ namespace LinearStruct } ``` +Ӧã -**2.2 ʽ洢ջ** +```c +using System; +using LinearStruct; + +namespace ExampleStack +{ + class Program + { + static void Main(string[] args) + { + StackTest(new SeqStack(20)); + } + + private static void StackTest(IStack stack) + { + stack.Push("a1"); + stack.Push("a2"); + stack.Push("a3"); + while (stack.IsEmpty() == false) + { + Console.WriteLine(stack.StackTop); + stack.Pop(); + } + // a3 + // a2 + // a1 + } + } +} +``` + +`SeqStack`Ȼʵ`IStack`ӿڣջͳջʱƶԪأЧʱȽϵ͡ʰջͳջŵ˳βУЧʡ + +![ͼ6 ˳ջ](https://img-blog.csdnimg.cn/20200920170606804.png) + +- ջ`Insert(Length, data)` +- ջ`Remove(Length - 1)` +- ջԪأ`SeqList[Length - 1]` + + + +```c +using System; + +namespace LinearStruct +{ + /// + /// ˳洢ṹʵֵջ + /// + /// ˳ջԪص + public class SeqStack_1 : IStack where T : IComparable + { + private readonly SeqList _lst; + + /// + /// ʼSeqStackʵ + /// + /// SeqStackԪصĸ + public SeqStack_1(int max) + { + if (max <= 0) + throw new ArgumentOutOfRangeException(); + _lst = new SeqList(max); + } + + /// + /// ȡSeqStackʵʰԪصĸ + /// + public int Length + { + get { return _lst.Length; } + } + + /// + /// ȡSeqStackԪصĸ + /// + public int MaxSize + { + get { return _lst.MaxSize; } + } + + /// + /// ȡSeqStackеջԪ + /// + public T StackTop + { + get + { + if (_lst.IsEmpty()) + throw new Exception("ջΪ."); + return _lst[Length - 1]; + } + } + + /// + /// Ԫջ + /// + /// ҪջԪ + public void Push(T data) + { + if (_lst.Length == _lst.MaxSize) + throw new Exception("ջѴﵽ."); + _lst.Insert(Length, data); + } + + /// + /// Ԫسջ + /// + public void Pop() + { + if (_lst.IsEmpty()) + throw new Exception("ջΪ."); + _lst.Remove(Length - 1); + } + + /// + /// жSeqStackǷԪ + /// + /// Ԫطfalse,򷵻true. + public bool IsEmpty() + { + return _lst.IsEmpty(); + } + + /// + /// SeqStackƳԪ + /// + public void Clear() + { + _lst.Clear(); + } + } +} +``` + +Ӧã + +```c +using System; +using LinearStruct; + +namespace ExampleStack +{ + class Program + { + static void Main(string[] args) + { + StackTest(new SeqStack_1(20)); + } + + private static void StackTest(IStack stack) + { + stack.Push("a1"); + stack.Push("a2"); + stack.Push("a3"); + while (stack.IsEmpty() == false) + { + Console.WriteLine(stack.StackTop); + stack.Pop(); + } + // a3 + // a2 + // a1 + } + } +} +``` + +--- +## 3. ջʽ洢ջ ջõʵֵջ ʵ֣ -![ջ](https://img-blog.csdnimg.cn/20191222215336715.png) +![ͼ7 ջ](https://img-blog.csdnimg.cn/20200920171743150.png) -´Ϊ`C#`汾 ```c using System; @@ -287,204 +443,77 @@ namespace LinearStruct } ``` -## 3. ݹ - -һڲǵݹ麯 - -Sample01nĽ׳ - -`n! = 1 x 2 x 3 x ... x n` - -ѭ - -´Ϊ`Python`汾 - -```python -n = 5 -for k in range(1, 5): - n = n * k -print(n) # 120 -``` - - -ݹ飺 - -´Ϊ`Python`汾 - -```python -def factorial(n): - if n == 1: - return 1 - return n * fact(n - 1) - - -print(factorial(5)) # 120 -``` - -Samp02쳲 - -`f(n)=f(n-1)+f(n-2), f(0)=0 f(1)=1` - -ѭ - -´Ϊ`Python`汾 - -```python -i = 0 -j = 1 -lst = list([i, j]) -for k in range(2, 11): - k = i + j - lst.append(k) - i = j - j = k -print(lst) -# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] -``` - -ݹ飺 - -´Ϊ`Python`汾 - -```python -def recur_fibo(n): - if n <= 1: - return n - return recur_fibo(n - 1) + recur_fibo(n - 2) - - -lst = list() -for k in range(11): - lst.append(recur_fibo(k)) -print(lst) -# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] -``` - - -ע⣺õݹIJPythonĬϵݹΪ 100 - -```python -import sys - -sys.setrecursionlimit(1000) -``` - -Sample03ŵ - -ŵԴӡһϴ˵ߡ촴ʱʯӣһϴϰմС˳ 64 ƬƽԲ̡ŰԲ̴濪ʼС˳°ڷһϡҹ涨СԲϲܷŴԲ̣֮һֻƶһԲ̡ - -![](https://img-blog.csdnimg.cn/20181218104026718.png) - -Ҫ˼ÿһôƿܻdzӣǿԽ򻯡 - -ǿȼ a ֮⣬Ѿɹؽ a 63Ƶ b ʱֻҪٽ a ƶ c ɡ - -![](https://img-blog.csdnimg.cn/20181218104208856.png) - -ǽ a Ƶ c b ϱµ 63 ӣa ΪաڵĿͱ˽ 63 b Ƶ c ԭȫһֻ a Ϊ b ģ 64 Ϊ 63˿ԲͬķȽ 62 b Ƶ a ٽƵ c - -Դƣ b Ϊ壬 a 62 Բ 61 Բƶ b һԲƵ c - -Ѿֹɣÿζ a b һΪ壬ȻȽԲ֮ԲƶϣٽµԲƵ c ϣظ˹̡ - -ƶԲ̵Ĺ̾ǵݹ飬ÿ n Բ̵ƶ⣬ҪȽn-1ӽͬ⡣ - -ǿԱдһmove(n, a, b, c)⣺move(, , , յ) - -1. a ֻһӵֱӰᵽ c: - -```python -if n == 1: - print(a, '-->', c) -``` -2. a ϲֹһӵ: - -ȣҪ n-1 Ӱᵽ b ӻ塣ӡЧǣa --> b - -```python -move(n - 1, a, c, b) -``` - -ٰӰᵽ c ӣҲߴһӡa-->c - -```python -move(1, a, b, c) -``` - -󣬰ʣ b n-1 Ӱᵽ c ϣʱ㣬˻塣 - -```python -move(n - 1, b, a, c) -``` - - - Python ʵֺŵ - -```python -i = 0 - - -def move(n, a, b, c): - global i - if (n == 1): - i += 1 - print('ƶ {0} {1} --> {2}'.format(i, a, c)) - return - move(n - 1, a, c, b) - move(1, a, b, c) - move(n - 1, b, a, c) - - -move(3, "a", "b", "c") - -# ƶ 1 a --> c -# ƶ 2 a --> b -# ƶ 3 c --> b -# ƶ 4 a --> c -# ƶ 5 b --> a -# ƶ 6 b --> c -# ƶ 7 a --> c -``` - - C# ʵֺŵ +Ӧã ```c -class Program -{ - private static int i = 0; - static void Move(int n, string a, string b, string c) - { - if (n == 1) - { - Console.WriteLine("ƶ {0} {1}-->{2}", ++i, a, c); - return; - } - Move(n - 1, a, c, b); - Move(1, a, b, c); - Move(n - 1, b, a, c); - } +using System; +using LinearStruct; - static void Main(string[] args) +namespace ExampleStack +{ + class Program { - Move(3, "a", "b", "c"); + static void Main(string[] args) + { + StackTest(new LinkStack()); + } + + private static void StackTest(IStack stack) + { + stack.Push("a1"); + stack.Push("a2"); + stack.Push("a3"); + while (stack.IsEmpty() == false) + { + Console.WriteLine(stack.StackTop); + stack.Pop(); + } + // a3 + // a2 + // a1 + } } } - -// ƶ 1 a --> c -// ƶ 2 a --> b -// ƶ 3 c --> b -// ƶ 4 a --> c -// ƶ 5 b --> a -// ƶ 6 b --> c -// ƶ 7 a --> c ``` +--- +## 4. ջӦ + +**4.1 ** +һлг`n`ڳᣬÿڳὫͣڲͬijվٶ`n`վıŷֱΪ1`n`гյ`n`վ`1`վĴ򾭹ЩվıǵĿĵͬΪ˱ڴгжӦijᣬгᣬʹǰ󰴱1`n`ĴСеijᶼִʱÿվֻжһڳἴɡ -## 4. ϰο -ŵij룬£`C#`汾 +һתվɳŹתվһ졢һ`k`죨λͳ֮䣩ͼ1aһתվ`k``k=3``H1``H2``H3`ʼʱ`n`ڳĻ촦תվתʱҵձ1`n`Ĵ뿪תվͨ촦ͼ1aУ`n=9`ӺǰijʼΪ581742963ͼ1b˰ҪĴкĽ + +![ͼ1 תվ](https://img-blog.csdnimg.cn/20200920173231448.png) + + +**4.2 ** + +ڷͼ1ΪųᣬǰμϵгᡣڼijһҪijᣬֱӰŵȥǣƶϣֱҪֵʱŽŵϡϳĽͳֻڻβСųУƶ + +1Դƶһβŵг + +2Դһβƶŵг + +ͼ1a3ųǰλ1ź2ųĺ棬˲3ųᣬɰ3ų뻺`H1`һڳ6ųᣬҲ뻺졣6ų`H1`ôŹ̽޷ɣΪ3ųλ6ųĺ棬ŵҪ3ų6ų֮ǰ˿ɰ6ų`H2`һڳ9ųᣬ`H3`Ϊ`H1``H2`ŹҲ޷ɡˣĵǰ״̬ͼ2aʾ + +![ͼ2 м״̬](https://img-blog.csdnimg.cn/20200920173917456.png) + +2ųᣬԱһ죬Ϊ㻺ϳűеҪ󣬲ӦȰ2ų`H1`Ϊ`H3`ûпռƶ7ų8ųᡣ2ų`H2`ô4ų뱻`H3`޷ƶ5š7ź8ųᡣµij`u`ӦĻ죺ײij`v``v > u``v`Ļ춥Сһš**ֻʹijܵС** + +4ųʱ춥ijֱΪ2š6ź9ųᡣݷ4ųӦ`H2`֮7ųᱻ`H3`ͼ2b˵ǰ״̬ + +1ųᱻ죬ʱ԰`H1`е2ų졣֮󣬴`H1`3ųᣬ 4ųᡣˣûпijˡ8ųᱻ`H1`Ȼ5ų촦ֱ촦֮󣬴`H2`6ųᣬ`H3`7ųᣬ`H1`8ųᣬ`H3`9ųᡣ + +Ϊֹ죬족ϣһ㷨˼· +- һıǡţֱӳ졣 +- ڶıǡǻСţֱӳ졣 +- ŷ뻺졣򣺷ŵСڻջԪرջԪС档 + +**4.3 ο** ```c using System; @@ -506,7 +535,6 @@ namespace TrainArrange for (int i = 0; i < h.Length; i++) h[i] = new LinkStack(); - int nowOut = 1; //һҪij int minH = int.MaxValue; //бСij int minS = -1; //minHųӦĻ @@ -561,7 +589,7 @@ namespace TrainArrange } /// - /// һз복C + /// һз복c /// /// 복 /// ջŵСֵ @@ -608,8 +636,8 @@ namespace TrainArrange static void Main(string[] args) { - int[] p = new int[] {3, 6, 9, 2, 4, 7, 1, 8, 5}; - int k = 1; + int[] p = new int[] { 3, 6, 9, 2, 4, 7, 1, 8, 5 }; + int k = 3; bool result = RailRoad(p, k); do { @@ -625,4 +653,205 @@ namespace TrainArrange } ``` +**4.4 ** + +![ͼ3 ģ](https://img-blog.csdnimg.cn/20200920180114850.png) + +--- +## 5. ݹ + +һڲǵݹ麯 + +1nĽ׳ + +```c +n! = 1 x 2 x 3 x ... x n +``` + +ѭ + +```c +static int factorial(int n) +{ + if (n < 1) + throw new ArgumentOutOfRangeException(); + + int result = n; + for (int i = 1; i < n; i++) + { + result *= i; + } + return result; +} + +static void Main(string[] args) +{ + Console.WriteLine(factorial(5)); + //120 +} +``` + + +ݹ飺 + +```c +static int factorial(int n) +{ + if (n < 1) + throw new ArgumentOutOfRangeException(); + + if (n == 1) + return 1; + return n * factorial(n - 1); +} + +static void Main(string[] args) +{ + Console.WriteLine(factorial(5)); + //120 +} +``` + +2쳲 + +```c +f(n)=f(n-1)+f(n-2), f(0)=0 f(1)=1 +``` + + +ѭ + +```c +static int[] recur_fibo(int n) +{ + if (n < 2) + throw new ArgumentOutOfRangeException(); + + int[] result = new int[n]; + result[0]= 0; + result[1] = 1; + for (int i = 2; i < n; i++) + { + result[i] = result[i - 1] + result[i - 2]; + } + return result; +} + +static void Main(string[] args) +{ + int[] r = recur_fibo(11); + for (int i = 0; i < r.Length; i++) + { + Console.Write(r[i]+" "); + } + Console.WriteLine(); + // 0 1 1 2 3 5 8 13 21 34 55 +} +``` + +ݹ飺 + + +```c +static int recur_fibo(int n) +{ + if (n <= 1) + return n; + + return recur_fibo(n - 1) + recur_fibo(n - 2); +} + +static void Main(string[] args) +{ + int[] result = new int[11]; + for (int i = 0; i < result.Length; i++) + { + result[i] = recur_fibo(i); + Console.Write(result[i] + " "); + } + Console.WriteLine(); + // 0 1 1 2 3 5 8 13 21 34 55 +} +``` + +3ŵ + +ŵԴӡһϴ˵ߡ촴ʱʯӣһϴϰմС˳ 64 ƬƽԲ̡ŰԲ̴濪ʼС˳°ڷһϡҹ涨СԲϲܷŴԲ̣֮һֻƶһԲ̡ + +![ͼ1 ŵ](https://img-blog.csdnimg.cn/20181218104026718.png) + +Ҫ˼ÿһôƿܻdzӣǿԽ򻯡 + +ǿȼ a ֮⣬Ѿɹؽ a 63Ƶ b ʱֻҪٽ a ƶ c ɡ + +![ͼ2 ](https://img-blog.csdnimg.cn/20181218104208856.png) + +ǽ a Ƶ c b ϱµ 63 ӣa ΪաڵĿͱ˽ 63 b Ƶ c ԭȫһֻ a Ϊ b ģ 64 Ϊ 63˿ԲͬķȽ 62 b Ƶ a ٽƵ c + +Դƣ b Ϊ壬 a 62 Բ 61 Բƶ b һԲƵ c + +Ѿֹɣÿζ a b һΪ壬ȻȽԲ֮ԲƶϣٽµԲƵ c ϣظ˹̡ + +ƶԲ̵Ĺ̾ǵݹ飬ÿ n Բ̵ƶ⣬ҪȽn-1ӽͬ⡣ + +ǿԱдһmove(n, a, b, c)⣺move(, , , յ) + +1. a ֻһӵֱӰᵽ c: + +```c +if(n == 1) + Console.WriteLine(a, '-->', c) +``` +2. a ϲֹһӵ: + +ȣҪ n-1 Ӱᵽ b ӻ塣ӡЧǣa --> b + +```python +move(n - 1, a, c, b) +``` + +ٰӰᵽ c ӣҲߴһӡa-->c + +```python +move(1, a, b, c) +``` + +󣬰ʣ b n-1 Ӱᵽ c ϣʱ㣬˻塣 + +```python +move(n - 1, b, a, c) +``` + + C# ʵֺŵ + +```c +class Program +{ + private static int i = 0; + static void Move(int n, string a, string b, string c) + { + if (n == 1) + { + Console.WriteLine("ƶ {0} {1}-->{2}", ++i, a, c); + return; + } + Move(n - 1, a, c, b); + Move(1, a, b, c); + Move(n - 1, b, a, c); + } + + static void Main(string[] args) + { + Move(3, "a", "b", "c"); + } +} +// ƶ 1 a --> c +// ƶ 2 a --> b +// ƶ 3 c --> b +// ƶ 4 a --> c +// ƶ 5 b --> a +// ƶ 6 b --> c +// ƶ 7 a --> c +``` +