diff --git a/DataStructureAndAlgorithm/06 Leetcode同步练习(二).md b/DataStructureAndAlgorithm/06 Leetcode同步练习(二).md new file mode 100644 index 0000000..1bdd436 --- /dev/null +++ b/DataStructureAndAlgorithm/06 Leetcode同步练习(二).md @@ -0,0 +1,755 @@ + + + + +[toc] + + + +--- +# Leetcodeͬϰ + +## Ŀ01 + +> - ţ9 +> - Ѷȣ +> - https://leetcode-cn.com/problems/palindrome-number/ + +жһǷǻָ򣨴ң͵򣨴󣩶һ + +ʾ 1: +```c +: 121 +: true +``` + +ʾ 2: + +```c +: -121 +: false +: Ҷ, Ϊ -121 , Ϊ 121- һ +``` + +ʾ 3: +```c +: 10 +: false +: , Ϊ 01 һ +``` + +: + +ܲתΪַ + +**ο** + +- ״̬ͨ +- ִʱ: 76 ms, C# ύл 98.90% û +- ڴ: 14.9 MB, C# ύл 85.12% û + +```c +public class Solution { + public bool IsPalindrome(int x) { + if (x < 0) + return false; + + int bit = 1; + while (x / bit >= 10) + { + bit = bit * 10; + } + + while (x > 0) + { + int left = x % 10; + int right = x / bit; + if (left != right) + { + return false; + } + x = (x % bit) / 10; + bit = bit / 100; + } + return true; +} +``` + + + +--- +## Ŀ02x ƽ + +> - ţ69 +> - Ѷȣ +> - https://leetcode-cn.com/problems/sqrtx/ + +ʵ `int sqrt(int x)` + +㲢`x`ƽ`x`ǷǸ + +ڷֻIJ֣Сֽȥ + +ʾ 1: + +```c +: 4 +: 2 +``` + +ʾ 2: + +```c +: 8 +: 2 +˵: 8 ƽ 2.82842..., ڷСֽȥ +``` + +**˼·**ţٵ + +![ţٵʽ](https://img-blog.csdnimg.cn/2019082111485172.png) + + +**ο** + +- ״̬ͨ +- ִʱ: 48 ms, C# ύл 100.00% û +- ڴ: 13.7 MB, C# ύл 5.40% û + +```c +public class Solution { + public int MySqrt(int x) { + if (x < 0) + throw new ArgumentOutOfRangeException(); + + double error = 1.0e-5; + double cur = x; + while (Math.Abs(cur*cur - x) > error) + { + cur = (cur + 1.0*x/cur)/2.0; + } + return (int)cur; + } +} +``` + + +--- +## Ŀ03¥ + +> - ţ70 +> - Ѷȣ +> - https://leetcode-cn.com/problems/climbing-stairs/ + +¥ݡҪ n ܵ¥ + +ÿ 1 2 ̨סжֲͬķ¥أ + +ע n һ + +ʾ 1 +```c +룺 2 + 2 +ͣ ַ¥ +1. 1 + 1 +2. 2 +``` + +ʾ 2 +```c +룺 3 + 3 +ͣ ַ¥ +1. 1 + 1 + 1 +2. 1 + 2 +3. 2 + 1 +``` + +ʾ 3 +```c +룺 44 + 1134903170 +``` + +**˼·**ѭ + +Ŀ +- 1 ףf(1) = 1 ַ +- 2 ףf(2) = 2 ַ +- 3 ףf(3) = 3 ַ +- 4 ףf(4) = 5 ַ +- +- n ףf(n) = f(n-1) + f(n-2) ַ + +תΪ쳲⡣ + +**ο** + +- ״̬ͨ +- ִʱ: 52 ms, C# ύл 97.87% û +- ڴ: 13.7 MB, C# ύл 5.98% û + +```c +public class Solution { + public int ClimbStairs(int n) { + if (n <= 2) + return n; + + int first = 1; + int second = 2; + int result = 0; + + for (int i = 3; i <= n; i++) + { + result = first + second; + first = second; + second = result; + } + return result; + } +} +``` + +--- +## Ŀ04Ʊʱ + +> - ţ121 +> - Ѷȣ +> - https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ + +һ飬ĵ i Ԫһ֧Ʊ i ļ۸ + +ֻһʽףһ֧Ʊһ㷨ܻȡ + +ע㲻ƱǰƱ + +ʾ 1: +```c +: [7,1,5,3,6,4] +: 5 +: ڵ 2 죨Ʊ۸ = 1ʱ룬ڵ 5 죨Ʊ۸ = 6ʱ = 6-1 = 5 + ע 7-1 = 6, Ϊ۸Ҫ۸ +``` + +ʾ 2: +```c +: [7,6,4,3,1] +: 0 +: , ûн, Ϊ 0 +``` + +**˼·**ѹƱܹ׬ǮֵΪ + + +**ο** + +- ״̬ͨ +- ִʱ: 132 ms, C# ύл 97.33% û +- ڴ: 24 MB, C# ύл 5.62% û + +```c +public class Solution +{ + public int MaxProfit(int[] prices) + { + if (prices.Length <= 1) + return 0; + + int min = prices[0]; + int max = 0; + for (int i = 1; i < prices.Length; i++) + { + int earn = prices[i] - min; + if (earn > max) + { + max = earn; + } + if (prices[i] < min) + { + min = prices[i]; + } + } + return max; + } +} +``` + + +--- +## Ŀ05Ʊʱ II + +> - ţ122 +> - Ѷȣ +> - https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ + + +һ飬ĵ i Ԫһ֧Ʊ i ļ۸ + +һ㷨ܻȡԾܵɸĽףһ֧Ʊ + +ע㲻ͬʱʽףٴιǰ۵֮ǰĹƱ + +ʾ 1: +```c +: [7,1,5,3,6,4] +: 7 +: + ڵ 2 죨Ʊ۸ = 1ʱ룬ڵ 3 죨Ʊ۸ = 5ʱ, ʽܻ = 5-1 = 4 + ڵ 4 죨Ʊ۸ = 3ʱ룬ڵ 5 죨Ʊ۸ = 6ʱ, ʽܻ = 6-3 = 3 +``` + +ʾ 2: +```c +: [1,2,3,4,5] +: 4 +: + ڵ 1 죨Ʊ۸ = 1ʱ룬ڵ 5 Ʊ۸ = 5ʱ, ʽܻ = 5-1 = 4 + ע㲻ڵ 1 ͵ 2 Ʊ֮ٽ + Ϊͬʱ˶ʽףٴιǰ۵֮ǰĹƱ +``` + +ʾ 3: +```c +: [7,6,4,3,1] +: 0 +: , ûн, Ϊ 0 +``` + +**˼·**̰㷨 + +̰IJԣֻҪһ۸ǰһߣǰһһ + +![н](https://img-blog.csdnimg.cn/20190914100650406.png) + + +![](https://img-blog.csdnimg.cn/20200325110545912.png) + +**ο** + +- ״̬ͨ +- ִʱ: 140 ms, C# ύл 72.02% û +- ڴ: 24.2 MB, C# ύл 5.36% û + + +```c +public class Solution +{ + public int MaxProfit(int[] prices) + { + int earn = 0; + for (int i = 0; i < prices.Length-1; i++) + { + earn += Math.Max(prices[i + 1] - prices[i], 0); + } + return earn; + } +} +``` + + +--- +## Ŀ06ԾϷ + +> - ţ55 +> - Ѷȣе +> - https://leetcode-cn.com/problems/jump-game/ + +һǸ飬λĵһλá + +еÿԪشڸλÿԾ󳤶ȡ + +жǷܹһλá + +**ʾ 1:** + +```c +: [2,3,1,1,4] +: true +: ǿ 1 λ 0 λ 1, Ȼٴλ 1 3 +һλá +``` + +**ʾ 2:** + +```c +: [3,2,1,0,4] +: false +: ܻᵽΪ 3 λáλõԾ 0 +Զܵһλá +``` + +**ʾ 3:** + +```c +룺[0] +true +``` + +**˼·̰㷨** + +̰IJԣÿμ¼ֵǰ㳬ֵfalseֵﵽһλãtrue + +**ο룺** + +- ִнͨ +- ִʱ120 ms, C# ύл 57.32% û +- ڴģ26.2 MB, C# ύл 6.67% û + +```c +public class Solution +{ + public bool CanJump(int[] nums) + { + int maxlength = 0; //¼ܵԶ + for (int i = 0; maxlength < nums.Length-1; i++) + { + if (i > maxlength) + { + return false;//˵Ѳܵfalse + } + maxlength = Math.Max(i + nums[i], maxlength); + } + return true; + } +} +``` + + + + + +--- +## Ŀ07֮ + +> - ţ15 +> - Ѷȣе +> - https://leetcode-cn.com/problems/3sum/ + +һ`n``nums`ж`nums`ǷԪ`abc`ʹ`a + b + c = 0`ҳҲظԪ顣 + +ע⣺в԰ظԪ顣 + +**ʾ** + +```c + nums = [-1, 0, 1, 2, -1, -4] + +ҪԪ鼯Ϊ +[ + [-1, 0, 1], + [-1, -1, 2] +] +``` + + + +**˼· + ķ** + +Ϊ˱ѭִЧʡȣ`nums`Ȼ󣬹̶3`i,l(left),r(right)``i`ѭ`l`ָ`nums[i]`֮Сֵ`r`ָ`nums[i]`ֵ֮ģ¿˼·`nums[i] > 0`ͲҪˣ`nums[i] + nums[l] + nums[r]`Ƿ㲢ӦĴ㣬`l`ƶ`r`ָ룬С㣬`r`ƶ`l`㣬뵽洢resultСȻĿҪԪ鲻ظڽеĹмȥؾͺá + +**ο룺** + +- ִнͨ +- ִʱ348 ms, C# ύл 99.54% û +- ڴģ35.8 MB, C# ύл 6.63% û + +```c +public class Solution +{ + public IList> ThreeSum(int[] nums) + { + IList> result = new List>(); + + nums = nums.OrderBy(a => a).ToArray(); + int len = nums.Length; + + for (int i = 0; i < len - 2; i++) + { + if (nums[i] > 0) + break; // Сִ0, IJѾû + + if (i > 0 && nums[i - 1] == nums[i]) + continue; // ԪеһԪصظ + + int l = i + 1; + int r = len - 1; + + while (l < r) + { + int sum = nums[i] + nums[l] + nums[r]; + if (sum < 0) + { + l++; + } + else if (sum > 0) + { + r--; + } + else + { + result.Add(new List() {nums[i], nums[l], nums[r]}); + // ԪеڶԪصظ + while (l < r && nums[l] == nums[l + 1]) + { + l++; + } + // ԪеԪصظ + while (l < r && nums[r - 1] == nums[r]) + { + r--; + } + l++; + r--; + } + } + } + return result; + } +} +``` + + +--- +## Ŀ08ӽ֮ + +> - ţ16 +> - Ѷȣе +> - https://leetcode-cn.com/problems/3sum-closest/ + +һ`n``nums`һĿֵ`target`ҳ`nums`еʹǵĺ`target`ӽĺ͡ٶÿֻΨһ𰸡 + +ʾ : + +```c +磬 nums = [-121-4], target = 1. + target ӽĺΪ 2. (-1 + 2 + 1 = 2). +``` + + +˼· + ķ + + +**ο룺** + +- ״̬ͨ +- ִʱ: 132 ms, C# ύл 100.00% û +- ڴ: 24 MB, C# ύл 5.55% û + + +```c +public class Solution +{ + public int ThreeSumClosest(int[] nums, int target) + { + nums = nums.OrderBy(a => a).ToArray(); + int result = nums[0] + nums[1] + nums[2]; + for (int i = 0; i < nums.Length - 2; i++) + { + int start = i + 1, end = nums.Length - 1; + while (start < end) + { + int sum = nums[start] + nums[end] + nums[i]; + if (Math.Abs(target - sum) < Math.Abs(target - result)) + result = sum; + if (sum > target) + end--; + else if (sum < target) + start++; + else + return result; + } + } + return result; + } +} +``` + +--- +## Ŀ09 II + +> - ţ59 +> - Ѷȣе +> - https://leetcode-cn.com/problems/spiral-matrix-ii/ + +һ nһ 1 n^2 ԪأԪذ˳ʱ˳еξ + +ʾ: +```c +: 3 +: +[ + [ 1, 2, 3 ], + [ 8, 9, 4 ], + [ 7, 6, 5 ] +] +``` + +**ο** + +- ״̬ͨ +- ִʱ: 296 ms, C# ύл 97.67% û +- ڴ: 25 MB, C# ύл 11.11% û + +```c +public class Solution +{ + public int[][] GenerateMatrix(int n) + { + int[][] matrix = new int[n][]; + for (int i = 0; i < n; i++) + { + matrix[i] = new int[n]; + } + + int start = 0;//ʼλ + int end1 = n - 1;//λ + int end2 = n - 1;//±λ + int count = 1; + + while (start < end1 && start < end2) + { + LeftToRight(start, end1, start, matrix, ref count); + TopToBottom(start + 1, end2, end1, matrix, ref count); + RightToLeft(end1 - 1, start, end2, matrix, ref count); + BottomToTop(end2 - 1, start + 1, start, matrix, ref count); + start++; + end1 = n - 1 - start; + end2 = n - 1 - start; + } + if (n%2 == 1) + { + matrix[start][start] = count; + } + return matrix; + } + + private void LeftToRight(int start, int end, int rowIndex, int[][] matrix, ref int from) + { + for (int i = start; i <= end; i++) + { + matrix[rowIndex][i] = from; + from++; + } + } + + private void TopToBottom(int start, int end, int colIndex, int[][] matrix, ref int from) + { + for (int i = start; i <= end; i++) + { + matrix[i][colIndex] = from; + from++; + } + } + + private void RightToLeft(int start, int end, int rowIndex, int[][] matrix, ref int from) + { + for (int i = start; i >= end; i--) + { + matrix[rowIndex][i] = from; + from++; + } + } + + private void BottomToTop(int start, int end, int colIndex, int[][] matrix, ref int from) + { + for (int i = start; i >= end; i--) + { + matrix[i][colIndex] = from; + from++; + } + } +} +``` + + + +## Ŀ10ͬ· + +> - ţ62 +> - Ѷȣе +> - https://leetcode-cn.com/problems/unique-paths/ + +һλһ m x n Ͻ ʼͼбΪStart + +ÿֻ»ƶһͼﵽ½ǣͼбΪFinish + +ܹжͬ· + +![](https://img-blog.csdnimg.cn/20190912160514978.png) + +磬ͼһ7 x 3 жٿܵ· + +˵m n ֵ 100 + +ʾ 1: +```c +: m = 3, n = 2 +: 3 +: +Ͻǿʼܹ 3 ·Ե½ǡ +1. -> -> +2. -> -> +3. -> -> +``` + +ʾ 2: +```c +: m = 7, n = 3 +: 28 +``` + +ʾ 3: +```c +: m = 23, n = 12 +: 193536720 +``` + +˼·ö̬滮 + +̬滮01 + +![01](https://img-blog.csdnimg.cn/20190912160347481.png) + +̬滮02 + +![02](https://img-blog.csdnimg.cn/20190912160424714.png) + +̬滮ӽṹΪ`d[i,j] = d[i-1,j] + d[i,j-1]` + +- ״̬ͨ +- 62 / 62 ͨ +- ִʱ: 52 ms, C# ύл 93.18% û +- ڴ: 13.6 MB, C# ύл 17.65% û + +```c +public class Solution +{ + public int UniquePaths(int m, int n) + { + int[,] memo = new int[m, n]; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (i == 0) + { + memo[i, j] = 1; + } + else if (j == 0) + { + memo[i, j] = 1; + } + else + { + memo[i, j] = memo[i - 1, j] + memo[i, j - 1]; + } + } + } + return memo[m - 1, n - 1]; + } +} +``` + + + diff --git a/DataStructureAndAlgorithm/06 线性表.md b/DataStructureAndAlgorithm/07 线性表.md similarity index 100% rename from DataStructureAndAlgorithm/06 线性表.md rename to DataStructureAndAlgorithm/07 线性表.md diff --git a/DataStructureAndAlgorithm/07 顺序表和链表.md b/DataStructureAndAlgorithm/07 顺序表和链表.md deleted file mode 100644 index 197ca81..0000000 --- a/DataStructureAndAlgorithm/07 顺序表和链表.md +++ /dev/null @@ -1,1353 +0,0 @@ - - - - -# Task02 ˳2죩 - -## 1. ԱĶ - -**1.1 ԱĶ** - -ԱLinear List`nn >= 0`ͬ͵Ԫ`a1,a2,...,an` ɵСгβԪ⣬ԪҽһֱǰֱӺ̡ԪؽһֱӺ̣βԪؽһֱǰԪصĸΪijȣΪ`(a1,a2,...,an)` - - -**1.2 ԱIJ** -- ȡȡָԪֵ֧ -- Ԫֵ뵽ָ -- ƳƳԱָԪء -- ҲѰҾֵĽ㲢±ꡣ -- õȡԱʵʰԪصĸ -- ǷΪգжԱǷԪء -- ղƳԱеԪء - -![Աӿ](https://img-blog.csdnimg.cn/20191219081504351.png) - -´Ϊ`C#`汾 - -```c -using System; - -namespace LinearStruct -{ - /// - /// Աij - /// - /// ԱԪص - public interface ILinearList where T : IComparable - { - /// - /// ȡԱʵʰԪصĸ - /// - int Length { get; } - - /// - /// ȡָԪ - /// - /// ҪȡõԪش㿪ʼ - /// ָԪ - T this[int index] { get; set; } - - /// - /// жԱǷԪ - /// - /// Ԫطfalse,򷵻true. - bool IsEmpty(); - - /// - /// Ԫز뵽ָ - /// - /// 㿪ʼ,Ӧڸλòdata. - /// ҪԪ - void Insert(int index, T data); - - /// - /// ƳԱָԪ - /// - /// ҪƳԪش0ʼ - void Remove(int index); - - /// - /// ԱѰԪdata. - /// - /// ҪѰҵԪ - /// ڷظԪԱеλ,򷵻-1. - int Search(T data); - - /// - /// ԱƳԪ - /// - void Clear(); - } -} -``` - ---- -## 2. ԱĴ洢ʵ - -**2.1 ˳洢˳** - -壺˳洢ṹ飩ʵֵԱ - -ص㣺߼ṹ洢ṹͬȡص㡣 - -![˳洢ʾͼ](https://img-blog.csdnimg.cn/20191219081751681.png) - -ʵ֣ - -![˳洢ṹʵԱ](https://img-blog.csdnimg.cn/20191219082422397.png) - -´ΪC#汾 - -```c -using System; - -namespace LinearStruct -{ - /// - /// ˳洢ṹʵֵԱ - /// - /// ˳Ԫص - public class SeqList : ILinearList where T : IComparable - { - /// - /// ݼ - /// - protected readonly T[] Dataset; - - /// - /// ȡSeqListʵʰԪصĸ - /// - public int Length { get; private set; } - - /// - /// ȡSeqListԪصĸ - /// - public int MaxSize { get; } - - /// - /// ʼSeqListʵ - /// - /// SeqListԪصĸ - public SeqList(int max) - { - if (max <= 0) - throw new ArgumentOutOfRangeException(); - MaxSize = max; - Dataset = new T[MaxSize]; - Length = 0; - } - - /// - /// ȡָԪ - /// - /// ҪûõԪش㿪ʼ - /// ָԪ - public T this[int index] - { - get - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - return Dataset[index]; - } - set - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - Dataset[index] = value; - } - } - - /// - /// жSeqListǷԪ - /// - /// Ԫطfalse,򷵻true. - public bool IsEmpty() - { - return Length == 0; - - } - - /// - /// Ԫز뵽ָ - /// - /// 㿪ʼ,Ӧڸλòdata. - /// ҪԪ - public void Insert(int index, T data) - { - if (index < 0 || index > Length) - throw new IndexOutOfRangeException(); - if (Length == MaxSize) - throw new Exception("ﵽֵ"); - - for (int i = Length; i > index; i--) - { - Dataset[i] = Dataset[i - 1]; - } - Dataset[index] = data; - Length++; - } - - /// - /// ƳSeqListָԪ - /// - /// ҪƳԪش0ʼ - public void Remove(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - - for (int i = index; i < Length - 1; i++) - { - Dataset[i] = Dataset[i + 1]; - } - Length--; - } - - /// - /// SeqListѰԪdata. - /// - /// ҪѰҵԪ - /// ڷظԪԱеλ,򷵻-1. - public int Search(T data) - { - int i; - for (i = 0; i < Length; i++) - { - if (Dataset[i].CompareTo(data) == 0) - break; - } - return i == Length ? -1 : i; - } - - /// - /// SeqListƳԪ - /// - public void Clear() - { - Length = 0; - } - - /// - /// ת - /// - public void Reverse() - { - for (int i = 0; i < Length/2; i++) - { - T temp = Dataset[i]; - Dataset[i] = Dataset[Length - 1 - i]; - Dataset[Length - 1 - i] = temp; - } - } - } -} -``` - -**2.2 ʽ洢** - -ָ뷽ʽʵֵԱΪѭ˫Ҫ߼ڵԪλҲڣ߼ṹṹͬҲԲͬ - -**2.2.1 ** - -壺ÿֻһָ򣩵õķʽ洢Ա߼ṹ - -ṹ - -![洢ṹ](https://img-blog.csdnimg.cn/201912190831277.png) - -ʵ֣ - -Խķװ - -![Եķװ](https://img-blog.csdnimg.cn/20191219083410202.png) - -´Ϊ`C#`汾 - -```c -using System; - -namespace LinearStruct -{ - /// - /// - /// - /// Ԫص - public class SNode where T : IComparable - { - /// - /// ȡøýԪ - /// - public T Data { get; set; } - - /// - /// ȡøýĺ̽ - /// - public SNode Next { get; set; } - - /// - /// ʼSNodeʵ - /// - /// ýԪ - /// ýĺ̽ - public SNode(T data, SNode next = null) - { - Data = data; - Next = next; - } - } -} -``` - - -Եķװ - -![Եķװ](https://img-blog.csdnimg.cn/20191219084222597.png) - -´Ϊ`C#`汾 - -```c -using System; -using System.Collections; -using System.Collections.Generic; - -namespace LinearStruct -{ - /// - /// ʽ洢ṹʵֵԱ-- - /// - /// Ԫص - public class SLinkList : ILinearList, IEnumerable where T : IComparable - { - /// - /// 洢ͷ - /// - public SNode PHead { get; protected set; } - - /// - /// ȡSLinkListʵʰԪصĸ - /// - public int Length { get; private set; } - - /// - /// ʼSLinkListʵ - /// - public SLinkList() - { - Length = 0; - PHead = null; - } - - /// - /// Ԫز뵽ײ - /// - /// ҪԪ - public void InsertAtFirst(T data) - { - PHead = new SNode(data, PHead); - Length++; - } - - /// - /// ָĽ - /// - /// Ԫش㿪ʼ - /// ָĽ - private SNode Locate(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - - SNode temp = PHead; - for (int i = 0; i < index; i++) - { - temp = temp.Next; - } - return temp; - } - - /// - /// Ԫز뵽β - /// - /// ҪԪ - public void InsertAtRear(T data) - { - if (PHead == null) - { - PHead = new SNode(data); - } - else - { - Locate(Length - 1).Next = new SNode(data); - } - Length++; - } - - /// - /// ȡָԪ - /// - /// ҪûõԪش㿪ʼ - /// ָԪ - public T this[int index] - { - get - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - return Locate(index).Data; - } - set - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - Locate(index).Data = value; - } - } - - /// - /// жSLinkListǷԪ - /// - /// Ԫطfalse,򷵻true. - public bool IsEmpty() - { - return Length == 0; - } - - /// - /// Ԫز뵽ָ - /// - /// 㿪ʼ,Ӧڸλòdata. - /// ҪԪ - public void Insert(int index, T data) - { - if (index < 0 || index > Length) - throw new IndexOutOfRangeException(); - if (index == 0) - { - InsertAtFirst(data); - } - else if (index == Length) - { - InsertAtRear(data); - } - else - { - Locate(index - 1).Next = new SNode(data, Locate(index)); - Length++; - } - } - - /// - /// ƳSLinkListָԪ - /// - /// ҪƳԪش0ʼ - public void Remove(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - if (index == 0) - { - PHead = PHead.Next; - } - else - { - Locate(index - 1).Next = Locate(index).Next; - } - Length--; - } - - /// - /// SLinkListѰԪdata. - /// - /// ҪѰҵԪ - /// ڷظԪԱеλ,򷵻-1. - public int Search(T data) - { - int i; - SNode temp = PHead; - for (i = 0; i < Length; i++) - { - if (temp.Data.CompareTo(data) == 0) - break; - temp = temp.Next; - } - return i == Length ? -1 : i; - } - - /// - /// SLinkListƳԪ - /// - public void Clear() - { - PHead = null; - Length = 0; - } - - /// - /// ķת - /// - public void Reverse() - { - if (Length == 0 || Length == 1) - return; - SNode currentNode = PHead; - SNode newNode = null; - while (currentNode != null) - { - SNode tempNode = currentNode.Next; - currentNode.Next = newNode; - newNode = currentNode; - currentNode = tempNode; - } - PHead = newNode; - } - - /// - /// õö֧foreachѭ - /// - /// ö - /// - public IEnumerator GetEnumerator() - { - SNode current = PHead; - while (current != null) - { - yield return current.Data; - current = current.Next; - } - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } -} -``` - - - -**2.2.2 ѭ** - -壺һβĵڵУβָnullΪָpHead͵õʽѭ - -ʽ - -![ͷָʾѭ](https://img-blog.csdnimg.cn/20191219084644144.png) - -ͨ£ʹβָʾѭ - -![βָʾѭ](https://img-blog.csdnimg.cn/20191219084747468.png) - -ʵ֣ - -![ѭķװ](https://img-blog.csdnimg.cn/20191219084946540.png) - -´Ϊ`C#`汾 -```c -using System; - -namespace LinearStruct -{ - /// - /// ʽ洢ṹʵֵԱ--ѭ - /// - /// ѭԪص - public class CLinkList : ILinearList where T : IComparable - { - /// - /// - /// - public SNode PRear { get; protected set; } - - - /// - /// ȡCLinkListʵʰԪصĸ - /// - public int Length { get; private set; } - - /// - /// ȡָԪ - /// - /// ҪûõԪش㿪ʼ - /// ָԪ - public T this[int index] - { - get - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - return Locate(index).Data; - } - set - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - Locate(index).Data = value; - } - } - - /// - /// ʼCLinkListʵ - /// - public CLinkList() - { - Length = 0; - PRear = null; - } - - /// - /// жCLinkListǷԪ - /// - /// Ԫطfalse,򷵻true. - public bool IsEmpty() - { - return Length == 0; - } - - /// - /// Ԫز뵽ѭβ - /// - /// ҪԪ - public void InsertAtRear(T data) - { - if (IsEmpty()) - { - PRear = new SNode(data); - PRear.Next = PRear; - } - else - { - SNode temp = new SNode(data, PRear.Next); - PRear.Next = temp; - PRear = temp; - } - Length++; - } - - /// - /// Ԫز뵽ѭײ - /// - /// ҪԪ - public void InsertAtFirst(T data) - { - if (IsEmpty()) - { - PRear = new SNode(data); - PRear.Next = PRear; - } - else - { - SNode temp = new SNode(data, PRear.Next); - PRear.Next = temp; - } - Length++; - } - - /// - /// ָĽ - /// - /// Ԫش㿪ʼ - /// ָĽ - private SNode Locate(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - - SNode temp = PRear.Next; - for (int i = 0; i < index; i++) - { - temp = temp.Next; - } - return temp; - } - - /// - /// Ԫز뵽ָ - /// - /// 㿪ʼ,Ӧڸλòdata. - /// ҪԪ - public void Insert(int index, T data) - { - if (index < 0 || index > Length) - throw new IndexOutOfRangeException(); - if (index == 0) - { - InsertAtFirst(data); - } - else if (index == Length) - { - InsertAtRear(data); - } - else - { - SNode temp = Locate(index - 1); - temp.Next = new SNode(data, temp.Next); - Length++; - } - } - - /// - /// ƳCLinkListָԪ - /// - /// ҪƳԪش0ʼ - public void Remove(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - - if (PRear == PRear.Next) - { - PRear = null; - } - else - { - if (index == Length - 1) - { - SNode temp = Locate(Length - 2); - temp.Next = PRear.Next; - PRear = temp; - } - else if (index == 0) - { - PRear.Next = PRear.Next.Next; - } - else - { - SNode temp = Locate(index - 1); - temp.Next = temp.Next.Next; - } - } - Length--; - } - - /// - /// CLinkListѰԪdata. - /// - /// ҪѰҵԪ - /// ڷظԪԱеλ,򷵻-1. - public int Search(T data) - { - int i; - SNode temp = PRear; - - for (i = 0; i < Length; i++) - { - if (temp.Next.Data.CompareTo(data) == 0) - break; - temp = temp.Next; - } - return (i == Length) ? -1 : i; - - } - - /// - /// CLinkListƳԪ - /// - public void Clear() - { - Length = 0; - PRear = null; - } - } -} -``` - - -**2.2.3 ˫** - -壺ÿ㺬ָ򣩵˫ķʽ洢Ա߼ṹ - -ṹ - -![˫洢ṹ](https://img-blog.csdnimg.cn/20191219085239419.png) - -ʵ֣ - -Խķװ - -![˫ķװ](https://img-blog.csdnimg.cn/20191219085534618.png) - -´Ϊ`C#`汾 - -```c -using System; - -namespace LinearStruct -{ - /// - /// ˫ - /// - /// Ԫص - public class DNode where T : IComparable - { - /// - /// ȡøýǰ - /// - public DNode Prior { get; set; } - - /// - /// ȡøýĺ̽ - /// - public DNode Next { get; set; } - - /// - /// ȡøýԪ - /// - public T Data { get; set; } - - /// - /// ʼDNodeʵ - /// - /// ýԪ - /// ýǰ - /// ýĺ̽ - public DNode(T data, DNode prior = null, DNode next = null) - { - Prior = prior; - Data = data; - Next = next; - } - } -} -``` - -˫ķװ - -![˫ķװ](https://img-blog.csdnimg.cn/2019121909023162.png) - -´Ϊ`C#`汾 - -```c -using System; - -namespace LinearStruct -{ - /// - /// ʽ洢ṹʵֵԱ--˫ - /// - /// Ԫص - public class DLinkList : ILinearList where T : IComparable - { - /// - /// 洢ͷ - /// - public DNode PHead { get; protected set; } - - /// - /// 洢β - /// - public DNode PRear { get; protected set; } - - /// - /// ȡDLinkListʵʰԪصĸ - /// - public int Length { get; private set; } - - /// - /// ʼDLinkListʵ - /// - public DLinkList() - { - PHead = null; - PRear = null; - Length = 0; - } - - /// - /// Ԫز뵽˫ײ - /// - /// ҪԪ - public void InsertAtFirst(T data) - { - if (IsEmpty()) - { - DNode temp = new DNode(data); - PHead = temp; - PRear = temp; - } - else - { - DNode temp = new DNode(data, null, PHead); - PHead.Prior = temp; - PHead = temp; - } - Length++; - } - - /// - /// Ԫز뵽˫β - /// - /// ҪԪ - public void InsertAtRear(T data) - { - if (IsEmpty()) - { - DNode temp = new DNode(data); - PHead = temp; - PRear = temp; - } - else - { - DNode temp = new DNode(data, PRear, null); - PRear.Next = temp; - PRear = temp; - } - Length++; - } - - /// - /// ָĽ - /// - /// Ԫش㿪ʼ - /// ָĽ - private DNode Locate(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - - DNode temp = PHead; - for (int i = 0; i < index; i++) - { - temp = temp.Next; - } - return temp; - } - - /// - /// Ԫز뵽ָ - /// - /// 㿪ʼ,Ӧڸλòdata. - /// ҪԪ - public void Insert(int index, T data) - { - if (index < 0 || index > Length) - throw new IndexOutOfRangeException(); - - if (index == 0) - { - InsertAtFirst(data); - } - else if (index == Length) - { - InsertAtRear(data); - } - else - { - DNode temp1 = Locate(index); - DNode temp2 = new DNode(data, temp1.Prior, temp1); - temp2.Prior.Next = temp2; - temp2.Next.Prior = temp2; - Length++; - } - } - - /// - /// ƳDLinkListָԪ - /// - /// ҪƳԪش0ʼ - public void Remove(int index) - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - - if (Length == 1) - { - PHead = null; - PRear = null; - } - else - { - if (index == 0) - { - PHead = PHead.Next; - PHead.Prior = null; - } - else if (index == Length - 1) - { - PRear = PRear.Prior; - PRear.Next = null; - } - else - { - DNode temp = Locate(index); - temp.Prior.Next = temp.Next; - temp.Next.Prior = temp.Prior; - } - } - Length--; - } - - /// - /// жDLinkListǷԪ - /// - /// Ԫطfalse,򷵻true. - public bool IsEmpty() - { - return Length == 0; - } - - /// - /// DLinkListƳԪ - /// - public void Clear() - { - Length = 0; - PHead = null; - PRear = null; - } - - /// - /// DLinkListѰԪdata. - /// - /// ҪѰҵԪ - /// ڷظԪԱеλ,򷵻-1. - public int Search(T data) - { - int i; - DNode temp = PHead; - for (i = 0; i < Length; i++) - { - if (temp.Data.CompareTo(data) == 0) - break; - - temp = temp.Next; - } - return i == Length ? -1 : i; - } - - /// - /// ȡָԪ - /// - /// ҪûõԪش㿪ʼ - /// ָԪ - public T this[int index] - { - get - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - return Locate(index).Data; - } - set - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - Locate(index).Data = value; - } - } - } -} -``` - - - - ---- -## 3. ϰο - -**1. ϲ** - -´Ϊ`Java`汾 - -```java -/** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode(int x) { val = x; } - * } - */ - -/** - * @param l1 һ - * @param l2 ڶ - * @return ϲͷڵ - */ -public ListNode mergeTwoLists(ListNode l1, ListNode l2) { - if(l1 == null){ - return l2; - } - if(l2 == null){ - return l1; - } - ListNode head = new ListNode(0); - head.next = null; - ListNode temp, q; - q= head; - while(l1 != null && l2 != null){ - if(l1.val < l2.val){ - temp = l1; - l1 = l1.next; - }else{ - temp = l2; - l2 = l2.next; - } - q.next = temp; - q = temp; - } - ListNode node = l1 == null ? l2 : l1; - q.next = node; - return head.next; - } - - -class ListNode{ - int val; - ListNode next; - ListNode(int x){ - val = x; - } -} -``` - -´Ϊ`C#`汾 - -```c -/** - * Definition for singly-linked list. - * public class ListNode { - * public int val; - * public ListNode next; - * public ListNode(int x) { val = x; } - * } - */ -public class Solution -{ - public ListNode MergeTwoLists(ListNode l1, ListNode l2) - { - ListNode pHead = new ListNode(int.MaxValue); - ListNode temp = pHead; - - while (l1 != null && l2 != null) - { - if (l1.val < l2.val) - { - temp.next = l1; - l1 = l1.next; - } - else - { - temp.next = l2; - l2 = l2.next; - } - temp = temp.next; - } - - if (l1 != null) - temp.next = l1; - - if (l2 != null) - temp.next = l2; - - return pHead.next; - } -} -``` - - -**2. ɾĵNڵ** - - ´Ϊ`Java`汾 - -```java -/** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode(int x) { val = x; } - * } - */ - -/** - * @param head ͷڵ - * @param n Ҫɾnڵ - * @return ɾƶڵͷڵ - */ -public ListNode removeNthFromEnd(ListNode head, int n) { - if(head.next == null){ - return null; - } - ListNode p, q; - q = head; - int i = 1; - while((i < n+1) && q != null){ - q = q.next; - i++; - } - if(q == null){ - head = head.next; - return head; - } - p = head; - while(q.next != null){ - q = q.next; - p = p.next; - } - p.next = p.next.next; - return head; - } - -class ListNode { - int val; - ListNode next; - ListNode(int x) { val = x; } -} -``` - -´Ϊ`C#`汾 - -```c -/** - * Definition for singly-linked list. - * public class ListNode { - * public int val; - * public ListNode next; - * public ListNode(int x) { val = x; } - * } - */ - -public class Solution -{ - public ListNode RemoveNthFormEnd(ListNode head, int n) - { - int len = GetLength(head); - int index = len - n; - - if (index == 0) - { - head = head.next; - return head; - } - ListNode temp = head; - for (int i = 0; i < index - 1; i++) - { - temp = temp.next; - } - temp.next = temp.next.next; - return head; - } - - public int GetLength(ListNode head) - { - ListNode temp = head; - int i = 0; - while (temp != null) - { - i++; - temp = temp.next; - } - return i; - } -} -``` - -´Ϊ`C#`汾 -```c -/** - * Definition for singly-linked list. - * public class ListNode { - * public int val; - * public ListNode next; - * public ListNode(int x) { val = x; } - * } - */ -public class Solution -{ - public ListNode RemoveNthFormEnd(ListNode head, int n) - { - ListNode temp1 = head; - ListNode temp2 = head; - int len = 0; - int index = 0; - while (temp1 != null) - { - temp1 = temp1.next; - len++; - if (index == n) - { - break; - } - index++; - } - - if (len == n) - { - head = head.next; - return head; - } - - while (temp1 != null) - { - temp1 = temp1.next; - temp2 = temp2.next; - } - - temp2.next = temp2.next.next; - return head; - } -} -``` - - - -**3. ת** - -´Ϊ`C#`汾 - -```c -/** - * Definition for singly-linked list. - * public class ListNode { - * public int val; - * public ListNode next; - * public ListNode(int x) { val = x; } - * } - */ - -public class Solution -{ - public ListNode RotateRight(ListNode head, int k) - { - if (head == null || k == 0) - return head; - - int len = GetLength(head); - int index = len - k%len; - - if (index == len) - return head; - - ListNode temp1 = head; - ListNode temp2 = head; - for (int i = 0; i < index - 1; i++) - { - temp1 = temp1.next; - } - head = temp1.next; - temp1.next = null; - - temp1 = head; - while (temp1.next != null) - { - temp1 = temp1.next; - } - temp1.next = temp2; - return head; - } - - public int GetLength(ListNode head) - { - ListNode temp = head; - int i = 0; - while (temp != null) - { - i++; - temp = temp.next; - } - return i; - } -} -```