diff --git a/DataStructureAndAlgorithm/02 C#语言基本语法结构.md b/CSharpLanguage/01 C#语言基本语法结构.md similarity index 99% rename from DataStructureAndAlgorithm/02 C#语言基本语法结构.md rename to CSharpLanguage/01 C#语言基本语法结构.md index 2fe90a7..c34117f 100644 --- a/DataStructureAndAlgorithm/02 C#语言基本语法结构.md +++ b/CSharpLanguage/01 C#语言基本语法结构.md @@ -1,4 +1,4 @@ -# 02 C#Ի﷨ṹ +# 01 C#Ի﷨ṹ **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/03 C#面向对象设计 I.md b/CSharpLanguage/02 C#面向对象设计 I.md similarity index 99% rename from DataStructureAndAlgorithm/03 C#面向对象设计 I.md rename to CSharpLanguage/02 C#面向对象设计 I.md index 5be955f..2869bd4 100644 --- a/DataStructureAndAlgorithm/03 C#面向对象设计 I.md +++ b/CSharpLanguage/02 C#面向对象设计 I.md @@ -1,4 +1,4 @@ -# 03 C# I +# 02 C# I **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/04 C#面向对象设计 II.md b/CSharpLanguage/03 C#面向对象设计 II.md similarity index 99% rename from DataStructureAndAlgorithm/04 C#面向对象设计 II.md rename to CSharpLanguage/03 C#面向对象设计 II.md index d5fce42..15761af 100644 --- a/DataStructureAndAlgorithm/04 C#面向对象设计 II.md +++ b/CSharpLanguage/03 C#面向对象设计 II.md @@ -1,7 +1,7 @@ -# 04 C# II +# 03 C# II **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/01 绪论.md b/DataStructureAndAlgorithm/01 绪论.md index 808f726..46a7e1c 100644 --- a/DataStructureAndAlgorithm/01 绪论.md +++ b/DataStructureAndAlgorithm/01 绪论.md @@ -1,4 +1,4 @@ -# +# 01 ֪ʶṹ diff --git a/DataStructureAndAlgorithm/06 线性表.md b/DataStructureAndAlgorithm/02 线性表.md similarity index 99% rename from DataStructureAndAlgorithm/06 线性表.md rename to DataStructureAndAlgorithm/02 线性表.md index 10ed27f..f32fecc 100644 Binary files a/DataStructureAndAlgorithm/06 线性表.md and b/DataStructureAndAlgorithm/02 线性表.md differ diff --git a/DataStructureAndAlgorithm/09 栈与递归.md b/DataStructureAndAlgorithm/03 栈与递归.md similarity index 99% rename from DataStructureAndAlgorithm/09 栈与递归.md rename to DataStructureAndAlgorithm/03 栈与递归.md index 97b9f62..ce0cd09 100644 --- a/DataStructureAndAlgorithm/09 栈与递归.md +++ b/DataStructureAndAlgorithm/03 栈与递归.md @@ -1,6 +1,6 @@ -# 09 ջݹ +# 03 ջݹ **֪ʶṹ** @@ -16,7 +16,7 @@ 루ջɾջֻһˣջеԱȽFirst In Last OutԱ -1 Ա`$(a_0,a_1,?,a_{n-1)}$`ջջʾ +1 Ա $(a_0,a_1,?,a_{n-1)}$ ջջʾ ![ͼ2 ˳ģջջ](https://img-blog.csdnimg.cn/20191222213645860.png) diff --git a/DataStructureAndAlgorithm/10 队列与多线程.md b/DataStructureAndAlgorithm/04 队列与多线程.md similarity index 99% rename from DataStructureAndAlgorithm/10 队列与多线程.md rename to DataStructureAndAlgorithm/04 队列与多线程.md index 625b88f..37bcb2f 100644 --- a/DataStructureAndAlgorithm/10 队列与多线程.md +++ b/DataStructureAndAlgorithm/04 队列与多线程.md @@ -1,5 +1,5 @@ -# 10 ߳ +# 04 ߳ **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/05 Leetcode同步练习(一).md b/DataStructureAndAlgorithm/05 Leetcode同步练习(一).md deleted file mode 100644 index 561b9dc..0000000 --- a/DataStructureAndAlgorithm/05 Leetcode同步练习(一).md +++ /dev/null @@ -1,791 +0,0 @@ - -# Leetcodeͬϰһ - -## Ŀ01֮ - -> - ţ1 -> - Ѷȣ -> - https://leetcode-cn.com/problems/two-sum/ - -һ `nums` һĿֵ `target`ڸҳΪĿֵ ǵ±ꡣ - -ԼÿֻӦһ𰸡ǣ㲻ظͬԪء - -**ʾ1:** - -```c - nums = [2, 7, 11, 15], target = 9 - -Ϊ nums[0] + nums[1] = 2 + 7 = 9Է [0, 1] -``` - -**ʾ2** - -```c - nums = [230, 863, 916, 585, 981, 404, 316, 785, 88, 12, 70, 435, 384, 778, 887, 755, 740, 337, 86, 92, 325, 422, 815, 650, 920, 125, 277, 336, 221, 847, 168, 23, 677, 61, 400, 136, 874, 363, 394, 199, 863, 997, 794, 587, 124, 321, 212, 957, 764, 173, 314, 422, 927, 783, 930, 282, 306, 506, 44, 926, 691, 568, 68, 730, 933, 737, 531, 180, 414, 751, 28, 546, 60, 371, 493, 370, 527, 387, 43, 541, 13, 457, 328, 227, 652, 365, 430, 803, 59, 858, 538, 427, 583, 368, 375, 173, 809, 896, 370, 789], target = 542 - -Ϊ nums[28] + nums[45] = 221 + 321 = 542Է [28, 45] -``` - -**ο룺** - -˼·ֱñƥ㷨 - -- ִнͨ -- ִʱ432 ms, C# ύл 65.82% û -- ڴģ30.8 MB, C# ύл 8.67% û - -```c -public class Solution -{ - public int[] TwoSum(int[] nums, int target) - { - int[] result = new int[2]; - for (int i = 0; i < nums.Length - 1; i++) - { - int find = target - nums[i]; - for (int j = i + 1; j < nums.Length; j++) - { - if (find == nums[j]) - { - result[0] = i; - result[1] = j; - return result; - } - } - } - return result; - } -} -``` - - ---- -## Ŀ02ɾеظ - -> - ţ26 -> - Ѷȣ -> - https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ - -һ ****Ҫ **ԭ** ɾظֵԪأʹÿԪֻһΣƳ³ȡ - -Ҫʹöռ䣬 **ԭ޸** ʹ O(1) ռɡ - -ʾ 1: - -```c - nums = [1,1,2], -Ӧ÷µij 2, ԭ nums ǰԪر޸Ϊ 1, 2 -㲻Ҫг³ȺԪء -``` - -ʾ 2: - -```c - nums = [0,0,1,1,1,2,2,3,3,4], -Ӧ÷µij 5, ԭ nums ǰԪر޸Ϊ 0, 1, 2, 3, 4 -㲻Ҫг³ȺԪء -``` - -˵: - -ΪʲôֵĴ? - -ע⣬ԡ****ʽݵģζں޸ڵǿɼġ - -ڲ: - -```c -// nums ԡáʽݵġҲ˵ʵκο -int len = removeDuplicates(nums); - -// ں޸ڵǿɼġ -// ĺصij, ӡиóȷΧڵԪء -for (int i = 0; i < len; i++) { - print(nums[i]); -} -``` - - - -**ο룺** - -˼·˫һһ`j``i``nums[j] == nums[i]`ʱ`j++`Ϳظʱ`i++``nums[i] = nums[j]`ֵƹִеĩβɣʱ临ӶΪ`O(n)` - -- ִнͨ -- ִʱ300 ms, C# ύл 64.43% û -- ڴģ33.5 MB, C# ύл 5.48% û - -```c -public class Solution -{ - public int RemoveDuplicates(int[] nums) - { - if (nums.Length < 2) - return nums.Length; - - int i = 0; - for (int j = 1; j < nums.Length; j++) - { - if (nums[j] != nums[i]) - { - i++; - nums[i] = nums[j]; - } - } - return i + 1; - } -} -``` - ---- -## Ŀ03ƳԪ - -> - ţ27 -> - Ѷȣ -> - https://leetcode-cn.com/problems/remove-element/ - - -һ`nums`һֵ`val`Ҫ**ԭ**Ƴֵ`val`ԪأƳ³ȡ - -Ҫʹöռ䣬**ԭ޸**ʹ O(1) ռɡ - -Ԫص˳Ըı䡣㲻Ҫг³ȺԪء - -**ʾ 1:** - -```c - nums = [3,2,2,3], val = 3, - -Ӧ÷µij 2, nums еǰԪؾΪ 2 - -㲻Ҫг³ȺԪء -``` - -**ʾ 2:** - -```c - nums = [0,1,2,2,3,0,4,2], val = 2, - -Ӧ÷µij 5, nums еǰԪΪ 0, 1, 3, 0, 4 - -עԪؿΪ˳ - -㲻Ҫг³ȺԪء -``` - -**ʾ 3:** -```c -룺[] value = 0 - -0 -``` - -**ʾ 4:** -```c -룺[1] value = 1 - -0 -``` - -**ʾ 5:** - -```c -룺[4,5] value = 5 - -1 -``` - - - -**˵:** - -ΪʲôֵĴ? - -ע⣬ԡ****ʽݵģζں޸ڵǿɼġ - -ڲ: - -```c -// nums ԡáʽݵġҲ˵ʵκο -int len = removeElement(nums, val); - -// ں޸ڵǿɼġ -// ĺصij, ӡиóȷΧڵԪء -for (int i = 0; i < len; i++) { - print(nums[i]); -} -``` - -**ο룺** - -˼·˫˫`i``j``i`ΪϺ`j`Ϊǰ塣`nums[j]!=val``num[j]`ֵ`num[i]`ѭ`i`ָǰԪأΪҪԪأӶﵽƳԪصĿģʱ临ӶΪ `O(n)` - -- ִнͨ -- ִʱ272 ms, C# ύл 94.41% û -- ڴģ29.9 MB, C# ύл 5.21% û - -```c -public class Solution -{ - public int RemoveElement(int[] nums, int val) - { - int i = 0; - for (int j = 0; j < nums.Length; j++) - { - if (nums[j] != val) - { - nums[i] = nums[j]; - i++; - } - } - return i; - } -} -``` - - ---- -## Ŀ04 - -> - ţ53 -> - Ѷȣ -> - https://leetcode-cn.com/problems/maximum-subarray/ - -һ`nums`ҵһ͵飨ٰһԪأ͡ - -ʾ 1: -```c -: [-2,1,-3,4,-1,2,1,-5,4], -: 6 -: [4,-1,2,1] ĺΪ 6 -``` - -ʾ 2: -```c -: [-2,1], -: 1 -``` - -: - -ѾʵָӶΪ`O(n)`ĽⷨʹøΪķη⡣ - -**ο룺** - -˼·ñ㷨 - -- ״̬ͨ -- ִʱ: 596 ms, C# ύл 14.18% û -- ڴ: 24.5 MB, C# ύл 5.88% û - -```c -public class Solution { - public int MaxSubArray(int[] nums) { - int len = nums.Length; - if (len == 0) - return 0; - if (len == 1) - return nums[0]; - int max = int.MinValue; - - for (int i = 0; i < len; i++) - { - int sum = nums[i]; - if (sum > max) - { - max = sum; - } - for (int j = i + 1; j < len; j++) - { - sum += nums[j]; - if (sum > max) - { - max = sum; - } - } - } - return max; - } -} -``` - ---- -## Ŀ05ʢˮ - - -> - ţ11 -> - Ѷȣе -> - https://leetcode-cn.com/problems/container-with-most-water/ - -`n`Ǹ`a1a2...an`ÿеһ`(i, ai)`ڻ`n`ֱߣֱ`i` ˵ֱΪ`(i, ai)``(i, 0)`ҳеߣʹ`x`Ṳͬɵˮ - -˵㲻б`n`ֵΪ 2 - -![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hbGl5dW4tbGMtdXBsb2FkLm9zcy1jbi1oYW5nemhvdS5hbGl5dW5jcy5jb20vYWxpeXVuLWxjLXVwbG9hZC91cGxvYWRzLzIwMTgvMDcvMjUvcXVlc3Rpb25fMTEuanBn?x-oss-process=image/format,png) - -ͼдֱߴ [1,8,6,2,5,4,8,3,7]ڴ£ܹˮʾΪɫֵ֣Ϊ 49 - -ʾ: -```c -: [1,8,6,2,5,4,8,3,7] -: 49 -``` - - -**ο룺** - -˼·˫ķ - -![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9waWMubGVldGNvZGUtY24uY29tLzg4NGQ1YmNlOTU1ZThiYzA1MGY2NTUxNTQwNGEwNDI2NGYyYzFmZjgyNzYyMWFiZDcyYjgxNzA5ZmUzNzMyM2YtJUU5JTgxJThEJUU1JThFJTg2JUU3JTlBJTg0JUU2JTgzJTg1JUU1JTg2JUI1LmpwZw?x-oss-process=image/format,png) - -0-7ߵ1-7һΪΪʲô0-6һ֧ - -```c -h(i)ʾi߶εĸ߶ȣS(ij)ʾi߶κ͵j߶Ȧ - -֪ h(0) < h(7)ӶS(07) = h(0) * 7 - -S(06) = min(h(0), h(6)) * 6 - -h(0) <= h(6)S(06) = h(0) * 6 -h(0) > h(6)S(06) = h(6) * 6S(06) < h(0) * 6 - -ɴ˿֪S(06)ȻСS(07) -``` -ÿһͬķ׿֪˫ߵ· - -- ״̬ͨ -- 50 / 50 ͨ -- ִʱ: 144 ms, C# ύл 99.64% û -- ڴ: 26.6 MB, C# ύл 5.45% û - -```c -public class Solution -{ - public int MaxArea(int[] height) - { - int i = 0, j = height.Length - 1; - int max = int.MinValue; - while (i < j) - { - int temp = (j - i) * Math.Min(height[i], height[j]); - if (temp > max) - { - max = temp; - } - if (height[i] < height[j]) - { - i++; - } - else - { - j--; - } - } - return max; - } -} -``` - ---- -## Ŀ06ת - -> - ţ33 -> - Ѷȣе -> - https://leetcode-cn.com/problems/search-in-rotated-sorted-array/ - -谴Ԥδ֪ijϽת - -( 磬 [0,1,2,4,5,6,7] ܱΪ [4,5,6,7,0,1,2] ) - -һĿֵдĿֵ򷵻򷵻 -1 - -ԼвظԪء - -㷨ʱ临Ӷȱ`O(log n)` - -ʾ 1: -```c -: nums = [4,5,6,7,0,1,2], target = 0 -: 4 -``` - -ʾ 2: -```c -: nums = [4,5,6,7,0,1,2], target = 3 -: -1 -``` - -ʾ 3: -```c -: nums = [5,1,3], target = 5 -: 0 -``` - -ʾ 4: -```c -: nums = [4,5,6,7,8,1,2,3], target = 8 -: 4 -``` - -ʾ 5: -```c -: nums = [3,1], target = 1 -: 1 -``` - -**ο룺** - -˼·öַ - -- ״̬ͨ -- ִʱ: 128 ms, C# ύл 97.17% û -- ڴ: 23.8 MB, C# ύл 12.00% û - -```c -public class Solution -{ - public int Search(int[] nums, int target) - { - int i = 0, j = nums.Length - 1; - while (i <= j) - { - int mid = (i + j) / 2; - if (nums[mid] == target) - return mid; - - if (nums[mid] >= nums[i]) - { - //벿 - if (target > nums[mid]) - { - i = mid + 1; - } - else - { - if (target == nums[i]) - return i; - - if (target > nums[i]) - j = mid - 1; - else - i = mid + 1; - } - } - else - { - if (target < nums[mid]) - { - j = mid - 1; - } - else - { - if (target == nums[j]) - return j; - if (target < nums[j]) - i = mid + 1; - else - j = mid - 1; - } - } - } - return -1; - } -} -``` - - - - - ---- -## Ŀ07еĵKԪ - -> - ţ215 -> - Ѷȣе -> - https://leetcode-cn.com/problems/kth-largest-element-in-an-array/ - -δҵ `k` Ԫءע⣬Ҫҵĵ `k` Ԫأǵ `k` ͬԪء - -ʾ 1: -```c -: [3,2,1,5,6,4] k = 2 -: 5 -``` - -ʾ 2: -```c -: [3,2,3,1,2,4,5,5,6] k = 4 -: 4 -``` - -˵: - -Լ `k` Чģ `1 k ij` - - - - - -**ο룺** - -˼·ķ - -- ״̬ͨ -- ִʱ: 152 ms, C# ύл 76.47% û -- ڴ: 24.6 MB, C# ύл 5.55% û - -```c -public class Solution -{ - public int FindKthLargest(int[] nums, int k) - { - nums = nums.OrderBy(a => a).ToArray(); - return nums[nums.Length - k]; - } -} -``` - - - - ---- -## Ŀ08ij˻ - - -> - ţ238 -> - Ѷȣе -> - https://leetcode-cn.com/problems/product-of-array-except-self/ - -Ϊ`n``nums``n > 1``output``output[i]` `nums`г`nums[i]`֮Ԫصij˻ - -ʾ: -```c -: [1,2,3,4] -: [24,12,8,6] -``` - -˵: 벻Ҫʹó`O(n)` ʱ临Ӷɴ⡣ - - - -ڳռ临ӶĿ𣿣 ڶԿռ临ӶȷĿģ鲻Ϊռ䡣 - - - - - - -**ο룺** - -˼·˻ = ǰߵij˻ * ǰұߵij˻ - -```c - [1, 2, 3, 4] - ߵij˻ [1, 1, 2, 6] - ұߵij˻ [24,12,4, 1] - = * [24,12,8, 6] -``` - -- ״̬ͨ -- ִʱ: 304 ms, C# ύл 100.00% û -- ڴ: 34.6 MB, C# ύл 100.00% û - -```c -public class Solution -{ - public int[] ProductExceptSelf(int[] nums) - { - int len = nums.Length; - int[] output1 = new int[len];//˻ - int[] output2 = new int[len];//˻ - output1[0] = 1; - output2[len - 1] = 1; - for (int i = 1; i < len; i++) - { - output1[i] = output1[i - 1]*nums[i - 1]; - output2[len - i - 1] = output2[len - i]*nums[len - i]; - } - for (int i = 0; i < len; i++) - { - output1[i] *= output2[i]; - } - return output1; - } -} -``` - - ---- -## Ŀ09Ѱλ - - -> - ţ4 -> - Ѷȣ -> - https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ - -СΪ`m``n``nums1``nums2` - -ҳλҪ㷨ʱ临ӶΪ`O(log(m + n))` - -Լ`nums1``nums2`ͬʱΪա - -ʾ 1: -```c -nums1 = [1, 3] -nums2 = [2] - -λ 2.0 -``` - -ʾ 2: - -```c -nums1 = [1, 2] -nums2 = [3, 4] - -λ (2 + 3)/2 = 2.5 -``` - -**ο룺** - -˼·öֲԡ - -λһϻΪȵӼһӼеԪǴһӼеԪء - -ĿҪʱ临ӶΪ`O(log(m + n))`ԲܴβҵλӶ `O(m + n)`ҪֲͨԣͨÿαȽϣֱܹӰˢһ֣ҵλӶ`O(log(m + n))` - -```c -nums1: [a1,a2,a3,...am] -nums2: [b1,b2,b3,...bn] - -[nums1[:i],nums2[:j] | nums1[i:], nums2[j:]] - -nums1 ȡ i -nums2 ȡ j = (m+n+1)/2 - i -``` -ֻҪ֤ ͬλ `|` ߽Ա߲Ӷöַҵʵ`i` - -- ״̬ͨ -- ִʱ: 160 ms, C# ύл 99.18% û -- ڴ: 26.8 MB, C# ύл 5.05% û - -```c -public class Solution { - public double FindMedianSortedArrays(int[] nums1, int[] nums2) { - int m = nums1.Length; - int n = nums2.Length; - if (m > n) - return FindMedianSortedArrays(nums2, nums1); - - int k = (m + n + 1)/2; - int left = 0; - int right = m; - while (left < right) - { - int i = (left + right)/2; - int j = k - i; - if (nums1[i] < nums2[j - 1]) - left = i + 1; - else - right = i; - } - int m1 = left; - int m2 = k - left; - int c1 = Math.Max(m1 == 0 ? int.MinValue : nums1[m1 - 1], - m2 == 0 ? int.MinValue : nums2[m2 - 1]); - - if ((m + n)%2 == 1) - return c1; - - int c2 = Math.Min(m1 == m ? int.MaxValue : nums1[m1], - m2 == n ? int.MaxValue : nums2[m2]); - return (c1 + c2)*0.5; - } -} -``` - ---- -## Ŀ10ȱʧĵһ - - -> - ţ41 -> - Ѷȣ -> - https://leetcode-cn.com/problems/first-missing-positive/ - -һδ飬ҳûгֵС - -ʾ1: - -```c -: [1,2,0] -: 3 -``` - -ʾ2: - -```c -: [3,4,-1,1] -: 2 -``` - -ʾ3: -```c -: [7,8,9,11,12] -: 1 -``` - -ʾ4: -```c -: [1,1] -: 2 -``` - -ʵ5: - -```c -: [] -: 1 -``` - -˵: - -㷨ʱ临ӶӦΪO(n)ֻʹóĿռ䡣 - -**ο룺** - -˼·һΡ򡱣򡱵Ĺǣ`i`ڡ䷶Χ`i`Ӧ÷Ϊ`i - 1`λϡ - -- ִнͨ -- ִʱ100 ms, C# ύл 93.75% û -- ڴģ24.2 MB, C# ύл 97.44% û - - -```c -public class Solution { - public int FirstMissingPositive(int[] nums) { - int len = nums.Length; - for (int i = 0; i < len; i++) - { - while (nums[i] != i + 1 && nums[i] <= len && nums[i] > 0 && nums[i] != nums[nums[i] - 1]) - { - int temp = nums[i]; - nums[i] = nums[temp - 1]; - nums[temp - 1] = temp; - } - } - for (int i = 0; i < len; i++) - { - if (nums[i] != i + 1) - { - return i + 1; - } - } - return len + 1; //nums.Length = 0 - } -} -``` - - - diff --git a/DataStructureAndAlgorithm/12 数组与稀疏矩阵.md b/DataStructureAndAlgorithm/05 数组与稀疏矩阵.md similarity index 99% rename from DataStructureAndAlgorithm/12 数组与稀疏矩阵.md rename to DataStructureAndAlgorithm/05 数组与稀疏矩阵.md index 590e8ac..4b6bfbb 100644 --- a/DataStructureAndAlgorithm/12 数组与稀疏矩阵.md +++ b/DataStructureAndAlgorithm/05 数组与稀疏矩阵.md @@ -1,4 +1,4 @@ -# 12 ϡ +# 05 ϡ **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/13 字符串与整数集合.md b/DataStructureAndAlgorithm/06 字符串与整数集合.md similarity index 99% rename from DataStructureAndAlgorithm/13 字符串与整数集合.md rename to DataStructureAndAlgorithm/06 字符串与整数集合.md index 608ff97..5365050 100644 --- a/DataStructureAndAlgorithm/13 字符串与整数集合.md +++ b/DataStructureAndAlgorithm/06 字符串与整数集合.md @@ -1,4 +1,4 @@ -# 13 ַ +# 06 ַ **֪ʶ㣺** diff --git a/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md b/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md deleted file mode 100644 index 91b09a6..0000000 --- a/DataStructureAndAlgorithm/07 Leetcode同步练习(二).md +++ /dev/null @@ -1,759 +0,0 @@ - -# 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 һ -``` - -**д** - -```c -public class Solution -{ - public bool IsPalindrome(int x) - { - - } -} -``` - -: - -ܲתΪַ - -**ο** - -- ״̬ͨ -- ִʱ: 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/15 树.md b/DataStructureAndAlgorithm/07 树.md similarity index 99% rename from DataStructureAndAlgorithm/15 树.md rename to DataStructureAndAlgorithm/07 树.md index 6c936b4..38f476f 100644 --- a/DataStructureAndAlgorithm/15 树.md +++ b/DataStructureAndAlgorithm/07 树.md @@ -1,5 +1,5 @@ -# 15 +# 07 **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/08 Leetcode同步练习(三).md b/DataStructureAndAlgorithm/08 Leetcode同步练习(三).md deleted file mode 100644 index 11215b2..0000000 --- a/DataStructureAndAlgorithm/08 Leetcode同步练习(三).md +++ /dev/null @@ -1,794 +0,0 @@ - -# Leetcodeͬϰ - - -## Ŀ01ϲ - -> - ţ21 -> - Ѷȣ -> - https://leetcode-cn.com/problems/merge-two-sorted-lists/ - -ϲΪһµءͨƴӸнڵɵġ - -**ʾ** - -```c -룺1->2->4, 1->3->4 -1->1->2->3->4->4 -``` - -**ο** - -- ִнͨ -- ִʱ108 ms, C# ύл 83.80% û -- ڴģ25.9 MB, C# ύл 5.85% û - -```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; - } -} -``` - ---- -## Ŀ02ɾеظԪ - -> - ţ83 -> - Ѷȣ -> - https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ - - -һɾظԪأʹÿԪֻһΡ - - -**ʾ 1:** - -```c -: 1->1->2 -: 1->2 -``` - -**ʾ 2:** -```c -: 1->1->2->3->3 -: 1->2->3 -``` - -**˼·**˫ָķʽ - -`p1`Ϊǰָ̽·`p2`ΪָظԪأ`p2.next`ȥ`p1`ظԪضժ - -**ο** - -- ִнͨ -- ִʱ160 ms, C# ύл 5.23% û -- ڴģ25.9 MB, C# ύл 5.72% û - -```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 DeleteDuplicates(ListNode head) - { - if (head == null) - return head; - - ListNode p1 = head.next; - ListNode p2 = head; - while (p1 != null) - { - if (p1.val == p2.val) - p2.next = p1.next; - else - p2 = p2.next; - p1 = p1.next; - } - return head; - } -} -``` - ---- -## Ŀ03 - -> - ţ141 -> - Ѷȣ -> - https://leetcode-cn.com/problems/linked-list-cycle/ - -һжǷл - -Ϊ˱ʾеĻʹ`pos` ʾβӵеλã 0 ʼ `pos` -1ڸûл - -ʾ 1 -```c -룺head = [3,2,0,-4], pos = 1 -true -ͣһβӵڶڵ㡣 -``` - -![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hc3NldHMubGVldGNvZGUtY24uY29tL2FsaXl1bi1sYy11cGxvYWQvdXBsb2Fkcy8yMDE4LzEyLzA3L2NpcmN1bGFybGlua2VkbGlzdC5wbmc) - -ʾ 2 -```c -룺head = [1,2], pos = 0 -true -ͣһβӵһڵ㡣 -``` -![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hc3NldHMubGVldGNvZGUtY24uY29tL2FsaXl1bi1sYy11cGxvYWQvdXBsb2Fkcy8yMDE4LzEyLzA3L2NpcmN1bGFybGlua2VkbGlzdF90ZXN0Mi5wbmc) - -ʾ 3 -```c -룺head = [1], pos = -1 -false -ͣûл -``` -![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hc3NldHMubGVldGNvZGUtY24uY29tL2FsaXl1bi1sYy11cGxvYWQvdXBsb2Fkcy8yMDE4LzEyLzA3L2NpcmN1bGFybGlua2VkbGlzdF90ZXN0My5wbmc) - - - - O(1)ڴ - -**˼·**˫ָķʽ - -ͨ£жǷظԪأʹ`Hash`ķʽڵֳҲʹ˫ָķʽ - -һָ `p1` ÿƶڵ㣬ڶָ `p2` ÿƶһڵ㣬ڻĻһָһٴڶָ룬֮򲻴ڻ - -磺`head = [1,2,3,4,5]` - -```c -p11 3 5 2 4 1 -p21 2 3 4 5 1 -``` - -磺`head = [1,2,3,4]`ż -```c -p11 3 1 3 1 -p21 2 3 4 1 -``` - -**ο** - -- ״̬ͨ -- ִʱ: 112 ms, C# ύл 98.43% û -- ڴ: 24.9 MB, C# ύл 5.13% û - -```c -/** - * Definition for singly-linked list. - * public class ListNode { - * public int val; - * public ListNode next; - * public ListNode(int x) { - * val = x; - * next = null; - * } - * } - */ -public class Solution { - public bool HasCycle(ListNode head) { - ListNode p1 = head; - ListNode p2 = head; - - while (p1 != null && p1.next != null) - { - p1 = p1.next.next; - p2 = p2.next; - if (p1 == p2) - return true; - } - return false; - } -} -``` - -## Ŀ04ת - - -> - ţ206 -> - Ѷȣ -> - https://leetcode-cn.com/problems/reverse-linked-list/ - -תһ - -ʾ: -```c -: 1->2->3->4->5->NULL -: 5->4->3->2->1->NULL -``` - -: - -Եݹطתַܷ⣿ - -**˼·**˫ָķʽ - -`p1`Ϊǰָ̽·`p2`Ϊָ˳һȦ㶨⡣ - -**ο** - -- ״̬ͨ -- ִʱ: 116 ms, C# ύл 97.50% û -- ڴ: 23.3 MB, C# ύл 5.26% û - -```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 ReverseList(ListNode head) - { - if (head == null || head.next == null) - return head; - - ListNode p1 = head; - ListNode p2 = null; - while (p1 != null) - { - ListNode temp = p1.next; - p1.next = p2; - p2 = p1; - p1 = temp; - } - return p2; - } -} -``` - ---- -## Ŀ05ɾеĽڵ - -> - ţ237 -> - Ѷȣ -> - https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ - -дһʹɾijиģĩβڵ㣬㽫ֻҪɾĽڵ㡣 - -һ -- head = [4,5,1,9]ԱʾΪ: - -![](https://img-blog.csdnimg.cn/20190920111622847.png) - - -ʾ 1: -```c -: head = [4,5,1,9], node = 5 -: [4,1,9] -: ֵΪ 5 ĵڶڵ㣬ôڵĺ֮󣬸ӦΪ 4 -> 1 -> 9. -``` - -ʾ 2: -```c -: head = [4,5,1,9], node = 1 -: [4,5,9] -: ֵΪ 1 ĵڵ㣬ôڵĺ֮󣬸ӦΪ 4 -> 5 -> 9. -``` - -˵: - -- ٰڵ㡣 -- нڵֵΨһġ -- ĽڵΪĩβڵ㲢һеһЧڵ㡣 -- Ҫĺзκν - - - -**˼·** ûиͷڵ㣬ֱӸҪɾĽڵ㣬ǽԭɾǶڸýڵǰһڵһ֪޷ֱִɾˣǽҪɾڵ next ڵֵֵҪɾĽڵ㣬תȥɾ next ڵ㣬ӶĿġ - -**ο** - -- ״̬ͨ -- 41 / 41 ͨ -- ִʱ: 120 ms, C# ύл 99.55% û -- ڴ: 24.4 MB, C# ύл 5.88% û - -```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 void DeleteNode(ListNode node) - { - ListNode temp = node.next; - while (temp != null) - { - node.val = temp.val; - temp = temp.next; - if (temp != null) - { - node = node.next; - } - } - node.next = null; - } -} -``` - - - ---- -## Ŀ06 - -> - ţ2 -> - Ѷȣе -> - https://leetcode-cn.com/problems/add-two-numbers/ - - ǿ ʾǸУǸԵλǰ ķʽ洢ģǵÿڵֻܴ洢 һλ ֡ - -ǽ᷵һµʾǵĺ͡ - -Լ 0 ֮⣬ 0 ͷ - -ʾ 1 -```c -룺(2 -> 4 -> 3) + (5 -> 6 -> 4) -7 -> 0 -> 8 -ԭ342 + 465 = 807 -``` - -ʾ 2 -```c -룺(3 -> 7) + (9 -> 2) -2 -> 0 -> 1 -ԭ73 + 29 = 102 -``` - -**˼·**ģСѧʱѧļӷ㡣λӳʮһʮλнλϽλơ - -ο룺 - -- ״̬ͨ -- ִʱ: 144 ms, C# ύл 97.98% û -- ڴ: 26.7 MB, C# ύл 5.07% û - -```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 AddTwoNumbers(ListNode l1, ListNode l2) - { - ListNode result = new ListNode(-1); - ListNode l3 = result; - int flag = 0; - while (l1 != null && l2 != null) - { - int a = l1.val; - int b = l2.val; - int c = a + b + flag; - l3.next = new ListNode(c%10); - - flag = c >= 10 ? 1 : 0; - l1 = l1.next; - l2 = l2.next; - l3 = l3.next; - } - - while (l1 != null) - { - int a = l1.val + flag; - - l3.next = new ListNode(a%10); - - flag = a >= 10 ? 1 : 0; - l1 = l1.next; - l3 = l3.next; - } - - while (l2 != null) - { - int b = l2.val + flag; - - l3.next = new ListNode(b%10); - flag = b >= 10 ? 1 : 0; - l2 = l2.next; - l3 = l3.next; - } - - if (flag == 1) - { - l3.next = new ListNode(flag); - } - return result.next; - } -} -``` - ---- -## Ŀ07ɾĵNڵ - -> - ţ19 -> - Ѷȣе -> - https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ - -һɾĵ`n`ڵ㣬ҷͷ㡣 - -**ʾ** - -```c -һ: 1->2->3->4->5, n = 2. - -ɾ˵ڶڵΪ 1->2->3->5. -``` - -**˵** - -`n`֤Чġ - -**** - -ܳʹһɨʵ - -**˼·** ʹָ룬ǰָ`p1``n`úָ`p2``p1`ͬߣ`p1`ߵյ㣬`p2`ߵҪƳĽλá - -**ο룺** -- ִнͨ -- ִʱ104 ms, C# ύл 86.93% û -- ڴģ24.6 MB, C# ύл 100.00% û - -```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 RemoveNthFromEnd(ListNode head, int n) - { - ListNode p1 = head; - ListNode p2 = head; - - while (n > 0) - { - p1 = p1.next; - n--; - } - - if (p1 == null) //Ƴͷ - { - return head.next; - } - - while (p1.next != null) - { - p1 = p1.next; - p2 = p2.next; - } - - p2.next = p2.next.next; - return head; - } -} -``` - - ---- -## Ŀ08еĽڵ - -> - ţ24 -> - Ѷȣе -> - https://leetcode-cn.com/problems/swap-nodes-in-pairs/ - -һڵĽڵ㣬ؽ - -**㲻ֻǵĸıڵڲֵ**ҪʵʵĽнڵ㽻 - - - -**ʾ:** - -> 1->2->3->4, Ӧ÷ 2->1->4->3. - - -**ο** - -- ִнͨ -- ִʱ100 ms, C# ύл 93.18% û -- ڴģ23.4 MB, C# ύл 87.72% û - -```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 SwapPairs(ListNode head) - { - if (head == null || head.next == null) - return head; - - head = Swap(head); - - ListNode temp = head.next; - - while (temp != null && temp.next != null) - { - temp.next = Swap(temp.next); - if (temp.next != null) - { - temp = temp.next.next; - } - } - return head; - } - - public ListNode Swap(ListNode node) - { - if (node == null || node.next == null) - return node; - - ListNode t = node.next; - node.next = t.next; - t.next = node; - return t; - } -} -``` - ---- -## Ŀ09ת - - -> - ţ61 -> - Ѷȣе -> - https://leetcode-cn.com/problems/rotate-list/ - -һתÿڵƶ k λã k ǷǸ - -**ʾ 1:** - -```c -: 1->2->3->4->5->NULL, k = 2 -: 4->5->1->2->3->NULL - -: -ת 1 : 5->1->2->3->4->NULL -ת 2 : 4->5->1->2->3->NULL -``` - -**ʾ 2:** - -```c -: 0->1->2->NULL, k = 4 -: 2->0->1->NULL - -: -ת 1 : 2->0->1->NULL -ת 2 : 1->2->0->NULL -ת 3 : 0->1->2->NULL -ת 4 : 2->0->1->NULL -``` - - - - -**ο** - -- ִнͨ -- ִʱ100 ms, C# ύл 98.13% û -- ڴģ25.1 MB, C# ύл 100.00% û - - -```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; - } -} -``` - - - ---- -## Ŀ10ϲK - -> - ţ23 -> - Ѷȣ -> - https://leetcode-cn.com/problems/merge-k-sorted-lists/ - - -ϲ k غϲ㷨ĸӶȡ - -ʾ: -```c -: -[ - 1->4->5, - 1->3->4, - 2->6 -] -: 1->1->2->3->4->4->5->6 -``` - -˼·ϲķʽ - -ϲõһµķ`ListNode MergeTwoLists(ListNode l1, ListNode l2)`ʹø÷ϲǰõһµ֮ϲƣõϲ`K`бб - - -**ο** - -- ִнͨ -- ִʱ256 ms, C# ύл 36.69% û -- ڴģ29.3 MB, C# ύл 18.37% û - - -```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; - } - - public ListNode MergeKLists(ListNode[] lists) { - if (lists.Length == 0) - return null; - - ListNode result = lists[0]; - for (int i = 1; i < lists.Length; i++) - { - result = MergeTwoLists(result, lists[i]); - } - return result; - } -} -``` - - - - - - - - diff --git a/DataStructureAndAlgorithm/17 图.md b/DataStructureAndAlgorithm/08 图.md similarity index 99% rename from DataStructureAndAlgorithm/17 图.md rename to DataStructureAndAlgorithm/08 图.md index 4fa76e6..6cf9437 100644 --- a/DataStructureAndAlgorithm/17 图.md +++ b/DataStructureAndAlgorithm/08 图.md @@ -1,5 +1,5 @@ -# 17 ͼ +# 08 ͼ **֪ʶṹ** diff --git a/DataStructureAndAlgorithm/19 排序.md b/DataStructureAndAlgorithm/09 排序.md similarity index 99% rename from DataStructureAndAlgorithm/19 排序.md rename to DataStructureAndAlgorithm/09 排序.md index b52a53b..66673ef 100644 --- a/DataStructureAndAlgorithm/19 排序.md +++ b/DataStructureAndAlgorithm/09 排序.md @@ -1,4 +1,4 @@ -# 19 排序 +# 09 排序 **知识结构:** diff --git a/DataStructureAndAlgorithm/11 Leetcode同步练习(四).md b/DataStructureAndAlgorithm/11 Leetcode同步练习(四).md deleted file mode 100644 index f839e68..0000000 --- a/DataStructureAndAlgorithm/11 Leetcode同步练习(四).md +++ /dev/null @@ -1,1171 +0,0 @@ -# Leetcodeͬϰģ - -## Ŀ01Сջ - -> - ţ155 -> - Ѷȣ -> - https://leetcode-cn.com/problems/min-stack/ - -һ֧ pushpoptop ڳʱڼСԪصջ - -- push(x) -- Ԫ x ջС -- pop() -- ɾջԪء -- top() -- ȡջԪء -- getMin() -- ջеСԪء - -ʾ: - -```c -MinStack minStack = new MinStack(); -minStack.push(-2); -minStack.push(0); -minStack.push(-3); -minStack.getMin(); --> -3. -minStack.pop(); -minStack.top(); --> 0. -minStack.getMin(); --> -2. -``` - -**˼·**øջķʽ - -**ο** - -- ״̬ͨ -- ִʱ: 192 ms, C# ύл 96.43% û -- ڴ: 33.5 MB, C# ύл 13.63% û - -```c -public class MinStack -{ - /** initialize your data structure here. */ - private readonly Stack _stack; - private readonly Stack _stackMin; - - public MinStack() - { - _stack = new Stack(); - _stackMin = new Stack(); - } - - public void Push(int x) - { - _stack.Push(x); - if (_stackMin.Count == 0 || _stackMin.Peek() >= x) - { - _stackMin.Push(x); - } - } - - public void Pop() - { - int x = _stack.Pop(); - if (_stackMin.Peek() == x) - { - _stackMin.Pop(); - } - } - - public int Top() - { - return _stack.Peek(); - } - - public int GetMin() - { - return _stackMin.Peek(); - } -} - -/** - * Your MinStack object will be instantiated and called as such: - * MinStack obj = new MinStack(); - * obj.Push(x); - * obj.Pop(); - * int param_3 = obj.Top(); - * int param_4 = obj.GetMin(); - */ -``` - - - - ---- -## Ŀ02Ч - -> - ţ20 -> - Ѷȣ -> - https://leetcode-cn.com/problems/valid-parentheses/ - -һֻ '('')''{''}''['']' ַжַǷЧ - -Чַ㣺 - -- űͬ͵űպϡ -- űȷ˳պϡ - -עַɱΪЧַ - -ʾ 1: -```c -: "()" -: true -``` - -ʾ 2: -```c -: "()[]{}" -: true -``` - -ʾ 3: -```c -: "(]" -: false -``` - -ʾ 4: -```c -: "([)]" -: false -``` - -ʾ 5: -```c -: "{[]}" -: true -``` - -ʾ 6: -```c -: "" -: true -``` - - - -ʾ 7: -```c -: "((" -: false -``` - -**˼·**ջ - -жϸַȵżԣ򷵻falseջȽص㣬{[(ջ}])ջԪؽбȽϣǶӦջ򷵻false - -**ο** - -- ִнͨ -- ִʱ88 ms, C# ύл 70.31% û -- ڴģ22 MB, C# ύл 17.91% û - - -```c -public class Solution { - public bool IsValid(string s) - { - if (string.IsNullOrEmpty(s)) - return true; - - int count = s.Length; - if(count%2 == 1) - return false; - - Stack stack = new Stack(); - for (int i = 0; i < count; i++) - { - char c = s[i]; - if (stack.Count == 0) - { - stack.Push(c); - } - else if(c == '[' || c == '{' || c == '(') - { - stack.Push(c); - } - else if (stack.Peek() == GetPair(c)) - { - stack.Pop(); - } - else - { - return false; - } - } - return stack.Count == 0; - } - - public static char GetPair(char c) - { - if (c == ')') - return '('; - if (c == '}') - return '{'; - if (c == ']') - return '['; - return '\0'; - } -} -``` - ---- -## Ŀ03öʵջ - -> - ţ225 -> - Ѷȣ -> - https://leetcode-cn.com/problems/implement-stack-using-queues/ - -ʹöʵջв - -- `push(x)` -- Ԫ x ջ -- `pop()` -- ƳջԪ -- `top()` -- ȡջԪء -- `empty()` -- ջǷΪ - -**ע:** - -- ֻʹöеĻ-- Ҳ push to back, peek/pop from front, size, is empty ЩǺϷġ -- ʹõҲֶ֧Сʹ `list` `deque`˫˶Уģһ, ֻҪDZ׼Ķвɡ -- ԼвЧģ, һյջ `pop` `top` - - -**˼·**νʵջӻʱѭǰn-1ڵ㣬Ӷ׳Ӻټڶβʵֶӷ - -**ο룺** - -- ִнͨ -- ִʱ124 ms, C# ύл 56.21% û -- ڴģ23.4 MB, C# ύл 63.73% û - -```c -public class MyStack -{ - Queue queue = new Queue(); - - /** Initialize your data structure here. */ - public MyStack() - { - } - - /** Push element x onto stack. */ - public void Push(int x) - { - queue.Enqueue(x); - for (int i = 0; i < queue.Count - 1; i++) - { - queue.Enqueue(queue.Dequeue()); - } - } - - /** Removes the element on top of the stack and returns that element. */ - public int Pop() - { - return queue.Count > 0 ? queue.Dequeue() : -1; - } - - /** Get the top element. */ - public int Top() - { - return queue.Count > 0 ? queue.Peek() : -1; - } - - /** Returns whether the stack is empty. */ - public bool Empty() - { - return queue.Count == 0; - } -} - -/** - * Your MyStack object will be instantiated and called as such: - * MyStack obj = new MyStack(); - * obj.Push(x); - * int param_2 = obj.Pop(); - * int param_3 = obj.Top(); - * bool param_4 = obj.Empty(); - */ -``` - - ---- -## Ŀ04ת - -> - ţ7 -> - Ѷȣ -> - https://leetcode-cn.com/problems/reverse-integer/ - - -һ 32 λзҪÿλϵֽзת - -ʾ 1: -```python -: 123 -: 321 -``` - -ʾ 2: -```python -: -123 -: -321 -``` - -ʾ 3: -```python -: 120 -: 21 -``` -ʾ 4: -```python -: 21474836472^31 ? 1 = int.MaxValue -: 0 -``` -ʾ 5: -```python -: -2147483648?2^31 = int.MinValue -: 0 -``` - - -ע - -ǵĻֻܴ洢 32 λзֵΧΪ`[?2^31, 2^31 ? 1]`裬תôͷ 0 - - -**˼·**ͨеķʽѸתΪͨСͳһ - -**ο** - -- ״̬ͨ -- ִʱ: 56 ms, C# ύл 93.28% û -- ڴ: 13.9 MB, C# ύл 13.98% û - -```c -public class Solution { - public int Reverse(int x) { - if (x == int.MinValue) - return 0; - - long result = 0; - int negative = x < 0 ? -1 : 1; - x = negative * x; - - Queue q = new Queue(); - while (x != 0) - { - q.Enqueue(x % 10); - x = x/10; - } - - while (q.Count != 0) - { - result += q.Dequeue()*(long) Math.Pow(10, q.Count); - if (negative == 1 && result > int.MaxValue) - { - result = 0; - break; - } - if (negative == -1 && result*-1 < int.MinValue) - { - result = 0; - break; - } - } - return (int)result*negative; - } -} -``` - ---- -## Ŀ05沨ʽֵ - -> - ţ150 -> - Ѷȣе -> - https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ - -沨ʾʽֵ - -Ч +, -, *, / ÿҲһ沨ʽ - -˵ - -- ֻ֡ -- 沨ʽЧġ仰˵ʽܻóЧֵҲڳΪ 0 - -ʾ 1 - -```c -: ["2", "1", "+", "3", "*"] -: 9 -: ((2 + 1) * 3) = 9 -``` - -ʾ 2 - -```c -: ["4", "13", "5", "/", "+"] -: 6 -: (4 + (13 / 5)) = 6 -``` - -ʾ 3 - -```c -: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] -: 22 -: - ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 -= ((10 * (6 / (12 * -11))) + 17) + 5 -= ((10 * (6 / -132)) + 17) + 5 -= ((10 * 0) + 17) + 5 -= (0 + 17) + 5 -= 17 + 5 -= 22 -``` - - - -**˼·**ջȳԽ㷨ʵ֡ - -**ο** - -- ִнͨ -- ִʱ100 ms, C# ύл 100.00% û -- ڴģ25 MB, C# ύл 69.81% û - -```c -public class Solution { - public int EvalRPN(string[] tokens) - { - Stack stack = new Stack(); - for (int i = 0; i < tokens.Length; i++) - { - string str = tokens[i]; - if (str == "+" || str == "-" || str == "*" || str == "/") - { - int b = stack.Pop(); - int a = stack.Pop(); - int c = Compute(a, b, str); - stack.Push(c); - } - else - { - stack.Push(int.Parse(str)); - } - } - return stack.Pop(); - } - - public static int Compute(int a, int b, string oper) - { - int result = 0; - switch (oper) - { - case "+": - result = a + b; - break; - case "-": - result = a - b; - break; - case "*": - result = a * b; - break; - case "/": - result = a / b; - break; - } - return result; - } -} -``` - - ---- -## Ŀ06ȫ - -> - ţ46 -> - Ѷȣе -> - https://leetcode-cn.com/problems/permutations/ - -һûظֵУпܵȫС - -ʾ: -```c -: [1,2,3] -: -[ - [1,2,3], - [1,3,2], - [2,1,3], - [2,3,1], - [3,1,2], - [3,2,1] -] -``` - - - -˼·ݷback tracking һѡֳΪ̽ѡǰԴﵽĿꡣ̽ijһʱԭѡ񲢲ŻﲻĿ꣬˻һѡ߲ͨ˻ߵļΪݷij״̬ĵΪݵ㡱 - -> ׻ݷΪͨѡͬIJ·ѰĿĵأһ·һ·ڵȥҵĿĵءߴ·ҵ·ڵһ·ֱҵĿĵء - -ϰĻݹʾ - - -![ݹ](https://img-blog.csdnimg.cn/20201009214444744.png) - -**ο룺** - -- ״̬ͨ -- ִʱ: 364 ms, C# ύл 80.00% û -- ڴ: 30.6 MB, C# ύл 7.14% û - -```c -public class Solution -{ - private IList> _result; - private bool[] _used; - - public IList> Permute(int[] nums) - { - _result = new List>(); - if (nums == null || nums.Length == 0) - return _result; - _used = new bool[nums.Length]; - - FindPath(nums, 0, new List()); - return _result; - } - - public void FindPath(int[] nums, int count, List path) - { - if (count == nums.Length) - { - //ݹֹ - List item = new List(); - item.AddRange(path); - //뿽 - _result.Add(item); - return; - } - for (int i = 0; i < nums.Length; i++) - { - if (_used[i] == false) - { - path.Add(nums[i]); - _used[i] = true; - FindPath(nums, count + 1, path); - path.RemoveAt(path.Count - 1); - _used[i] = false; - } - } - } -} -``` - - - - ---- -## Ŀ07ַת (atoi) - -> - ţ8 -> - Ѷȣе -> - https://leetcode-cn.com/problems/string-to-integer-atoi/ - -ʵһ `atoi` ʹַܽת - -ȣúҪõĿͷոֱַѰҵһǿոַΪֹ - -ѰҵĵһǿַΪ߸ʱ򽫸÷֮澡ܶΪţһǿֱַ֣ӽַ֮γ - -ַЧ֮ҲܻڶַЩַԱԣǶںӦӰ졣 - -ע⣺ַеĵһǿոַһЧַַΪջַհַʱĺҪת - -κ£ܽЧתʱ뷵 0 - -˵ - -ǵĻֻܴ洢 32 λСзôֵΧΪ `[?2^31, 2^31? 1]`ֵΧ뷵 `INT_MAX (2^31? 1)` `INT_MIN (?2^31)` - -ʾ 1: -```c -: "42" -: 42 -``` - -ʾ 2: -```c -: " -42" -: -42 -: һǿհַΪ '-', һš -Ǿֵܽõ -42 -``` -ʾ 3: -```c -: "4193 with words" -: 4193 -: תֹ '3' ΪһַΪ֡ -``` - -ʾ 4: -```c -: "words and 987" -: 0 -: һǿַ 'w', ֻš -޷ִЧת -``` - -ʾ 5: -```c -: "-91283472332" -: -2147483648 -: "-91283472332" 32 λзΧ -˷ INT_MIN (?231) -``` - - -ʾ 6: -```c -: " 0000000000012345678" -: 12345678 -``` - -ʾ 7: -```c -: "20000000000000000000" -: 2147483647 -``` - -**˼·**ͨеķʽ - -**ο** - -- ״̬ͨ -- ִʱ: 104 ms, C# ύл 98.32% û -- ڴ: 24.3 MB, C# ύл 24.45% û - -```c -public class Solution { - public int MyAtoi(string str) { - str = str.Trim(); - if (string.IsNullOrEmpty(str)) - return 0; - - if (str[0] != '-' && str[0] != '+') - { - if (str[0] < '0' || str[0] > '9') - return 0; - } - int negative = 1; - long result = 0; - Queue q = new Queue(); - for (int i = 0; i < str.Length; i++) - { - if (str[i] == '-' && i == 0) - { - negative = -1; - continue; - } - if (str[i] == '+' && i == 0) - { - continue; - } - if (str[i] < '0' || str[i] > '9') - { - break; - } - q.Enqueue(str[i] - '0'); - } - - while (q.Count != 0) - { - int i = q.Dequeue(); - - //ȥǰ˵ - if (i == 0 && result == 0) - continue; - - // سλ - if (negative == 1 && q.Count > 10) - { - return int.MaxValue; - } - if (negative == -1 && q.Count > 10) - { - return int.MinValue; - } - - result += i * (long)Math.Pow(10, q.Count); - if (negative == 1 && result > int.MaxValue) - { - return int.MaxValue; - } - if (negative == -1 && result * -1 < int.MinValue) - { - return int.MinValue; - } - } - return (int)result * negative; - } -} -``` - ---- -## Ŀ08ѭ˫˶ - -> - ţ641 -> - Ѷȣе -> - https://leetcode-cn.com/problems/design-circular-deque/ - -ʵ˫˶С - -ʵҪ֧² - -- `MyCircularDeque(k)`캯,˫˶еĴСΪk -- `insertFront()`һԪӵ˫˶ͷ ɹ true -- `insertLast()`һԪӵ˫˶βɹ true -- `deleteFront()`˫˶ͷɾһԪء ɹ true -- `deleteLast()`˫˶βɾһԪءɹ true -- `getFront()`˫˶ͷһԪء˫˶Ϊգ -1 -- `getRear()`˫˶еһԪء˫˶Ϊգ -1 -- `isEmpty()`˫˶ǷΪա -- `isFull()`˫˶Ƿˡ - -ʾ - -```c -MyCircularDeque circularDeque = new MycircularDeque(3); // СΪ3 -circularDeque.insertLast(1); // true -circularDeque.insertLast(2); // true -circularDeque.insertFront(3); // true -circularDeque.insertFront(4); // Ѿˣ false -circularDeque.getRear(); // 2 -circularDeque.isFull(); // true -circularDeque.deleteLast(); // true -circularDeque.insertFront(4); // true -circularDeque.getFront(); // 4 -``` - -ʾ - -- ֵķΧΪ [1, 1000] -- ķΧΪ [1, 1000] -- 벻Ҫʹõ˫˶п⡣ - - -**ο** - -- ִнͨ -- ִʱ160 ms, C# ύл 90.48% û -- ڴģ34.7 MB, C# ύл 7.14% û - - -```c -public class MyCircularDeque -{ - private int _pFront; - private int _pRear; - private readonly int[] _dataset; - private int _length; - private int _maxSize; - - /** Initialize your data structure here. Set the size of the deque to be k. */ - public MyCircularDeque(int k) - { - _dataset = new int[k]; - _length = 0; - _maxSize = k; - _pFront = 0; - _pRear = 0; - } - - /** Adds an item at the front of Deque. Return true if the operation is successful. */ - public bool InsertFront(int value) - { - if (_length == _maxSize) - return false; - - _pFront = (_pFront - 1 + _maxSize)%_maxSize; - _dataset[_pFront] = value; - _length++; - return true; - } - - /** Adds an item at the rear of Deque. Return true if the operation is successful. */ - public bool InsertLast(int value) - { - if (_length == _maxSize) - return false; - - _dataset[_pRear] = value; - _pRear = (_pRear + 1)%_maxSize; - _length++; - return true; - } - - /** Deletes an item from the front of Deque. Return true if the operation is successful. */ - public bool DeleteFront() - { - if (_length == 0) - return false; - _pFront = (_pFront + 1)%_maxSize; - _length--; - return true; - } - - /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ - public bool DeleteLast() - { - if (_length == 0) - return false; - _pRear = (_pRear - 1 + _maxSize)%_maxSize; - _length--; - return true; - } - - /** Get the front item from the deque. */ - public int GetFront() - { - if (_length == 0) - return -1; - - return _dataset[_pFront]; - } - - /** Get the last item from the deque. */ - public int GetRear() - { - if (_length == 0) - return -1; - int index = (_pRear - 1 + _maxSize)%_maxSize; - return _dataset[index]; - } - - /** Checks whether the circular deque is empty or not. */ - public bool IsEmpty() - { - return _length == 0; - } - - /** Checks whether the circular deque is full or not. */ - public bool IsFull() - { - return _length == _maxSize; - } -} - -/** - * Your MyCircularDeque object will be instantiated and called as such: - * MyCircularDeque obj = new MyCircularDeque(k); - * bool param_1 = obj.InsertFront(value); - * bool param_2 = obj.InsertLast(value); - * bool param_3 = obj.DeleteFront(); - * bool param_4 = obj.DeleteLast(); - * int param_5 = obj.GetFront(); - * int param_6 = obj.GetRear(); - * bool param_7 = obj.IsEmpty(); - * bool param_8 = obj.IsFull(); - */ -``` - - - - - ---- -## Ŀ09Ч - -> - ţ32 -> - Ѷȣ -> - https://leetcode-cn.com/problems/longest-valid-parentheses/ - -һֻ '(' ')' ַҳİЧŵӴijȡ - -ʾ 1: - -```c -: "(()" -: 2 -: ЧӴΪ "()" -``` - -ʾ 2: -```c -: ")()())" -: 4 -: ЧӴΪ "()()" -``` - -ʾ 3: -```c -: "" -: 0 -: -``` -ʾ 4: -```c -: "()(())" -: 6 -: ЧӴΪ "()(())" -``` - -**˼·**ջ - -ǿջڱַĹȥжϵĿǰΪֹɨַЧԣͬʱЧַijȡȽ ?1 ջ - -- ÿ(ǽ±ջС -- ÿ)ǵջԪأжЧԣЧȡ - -**ο** - -- ״̬ͨ -- ִʱ92 ms, C# ύл 53.78% û -- ڴģ23.5 MB, C# ύл 7.14% û - -```c -public class Solution { - public int LongestValidParentheses(string s) { - Stack stack = new Stack(); - stack.Push(-1); - int result = 0; - for (int i = 0; i < s.Length; i++) - { - if (s[i] == '(') - { - stack.Push(i); - } - else - { - stack.Pop(); - if (stack.Count == 0) - { - stack.Push(i); - } - else - { - result = Math.Max(result, i - stack.First()); - } - } - } - return result; - } -} -``` - ---- -## Ŀ10ֵ - -> - ţ239 -> - Ѷȣ -> - https://leetcode-cn.com/problems/sliding-window-maximum/ - -һ numsһСΪ k ĻڴƶҲࡣֻԿڻڵ k ֡ÿֻƶһλ - -ػеֵ - -ʾ: - -```c -: nums = [1,3,-1,-3,5,3,6,7], k = 3 -: [3,3,5,5,6,7] -: - - ڵλ ֵ ---------------- ----- -[1 3 -1] -3 5 3 6 7 3 - 1 [3 -1 -3] 5 3 6 7 3 - 1 3 [-1 -3 5] 3 6 7 5 - 1 3 -1 [-3 5 3] 6 7 5 - 1 3 -1 -3 [5 3 6] 7 6 - 1 3 -1 -3 5 [3 6 7] 7 -``` - -ʾ - -Լ k Чģ鲻Ϊյ£1 k ĴС - - - - - -ʱ临Ӷڽ - -**ο룺** - -ʵһѰÿڵֵ - - -- ״̬ͨ -- ִʱ: 472 ms, C# ύл 53.33% û -- ڴ: 34.3 MB, C# ύл 50.00% û - -```c -public class Solution { - public int[] MaxSlidingWindow(int[] nums, int k) { - int len = nums.Length; - if (len == 0) - return nums; - - int[] result = new int[len - k + 1]; - for (int i = 0; i < result.Length; i++) - { - result[i] = GetMax(nums, i, i + k); - } - return result; - } - - public int GetMax(int[] arr,int start,int end) - { - int max = int.MinValue; - for (int i = start; i < end; i++) - { - if (arr[i] > max) - max = arr[i]; - } - return max; - } -} -``` - -ʵֶʱЧʾҪʹýṹˡ - -- ״̬ͨ -- ִʱ: 364 ms, C# ύл 75.33% û -- ڴ: 40.5 MB, C# ύл 100.00% û - - -**˼·**ѭ˫˶¼ǰڵϢдֵλü¼ڶԪС˫˶Ҫ֤λõֵɴС -- ҪɾԪء -- ڶβԪأҪɾβԪأѸԪصλò뵽β - - -```c -public class MyCircularDeque -{ - private int _pFront; - private int _pRear; - private readonly int[] _dataset; - private int _length; - private int _maxSize; - - /** Initialize your data structure here. Set the size of the deque to be k. */ - public MyCircularDeque(int k) - { - _dataset = new int[k]; - _length = 0; - _maxSize = k; - _pFront = 0; - _pRear = 0; - } - - /** Adds an item at the front of Deque. Return true if the operation is successful. */ - public bool InsertFront(int value) - { - if (_length == _maxSize) - return false; - - _pFront = (_pFront - 1 + _maxSize) % _maxSize; - _dataset[_pFront] = value; - _length++; - return true; - } - - /** Adds an item at the rear of Deque. Return true if the operation is successful. */ - public bool InsertLast(int value) - { - if (_length == _maxSize) - return false; - - _dataset[_pRear] = value; - _pRear = (_pRear + 1) % _maxSize; - _length++; - return true; - } - - /** Deletes an item from the front of Deque. Return true if the operation is successful. */ - public bool DeleteFront() - { - if (_length == 0) - return false; - _pFront = (_pFront + 1) % _maxSize; - _length--; - return true; - } - - /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ - public bool DeleteLast() - { - if (_length == 0) - return false; - _pRear = (_pRear - 1 + _maxSize) % _maxSize; - _length--; - return true; - } - - /** Get the front item from the deque. */ - public int GetFront() - { - if (_length == 0) - return -1; - - return _dataset[_pFront]; - } - - /** Get the last item from the deque. */ - public int GetRear() - { - if (_length == 0) - return -1; - int index = (_pRear - 1 + _maxSize) % _maxSize; - return _dataset[index]; - - } - - /** Checks whether the circular deque is empty or not. */ - public bool IsEmpty() - { - return _length == 0; - } - - /** Checks whether the circular deque is full or not. */ - public bool IsFull() - { - return _length == _maxSize; - } -} - -public class Solution { - public int[] MaxSlidingWindow(int[] nums, int k) { - int len = nums.Length; - if (len == 0) - return nums; - int[] result = new int[len - k + 1]; - MyCircularDeque deque = new MyCircularDeque(k); - - for (int i = 0; i < len; i++) - { - // жϵǰж׵ֵǷЧ - if (deque.IsEmpty() == false && deque.GetFront() <= i - k) - { - deque.DeleteFront(); - } - - // ֤ӴС ǰСҪεֱҪ - while (deque.IsEmpty() == false && nums[deque.GetRear()] <= nums[i]) - { - deque.DeleteLast(); - } - //ӵǰֵӦ± - deque.InsertLast(i); - - // ڳΪkʱ 浱ǰֵ - if (i + 1 >= k) - { - result[i + 1 - k] = nums[deque.GetFront()]; - } - } - return result; - } -} -``` \ No newline at end of file diff --git a/DataStructureAndAlgorithm/14 Leetcode同步练习(五).md b/DataStructureAndAlgorithm/14 Leetcode同步练习(五).md deleted file mode 100644 index 3c02b54..0000000 --- a/DataStructureAndAlgorithm/14 Leetcode同步练习(五).md +++ /dev/null @@ -1,1114 +0,0 @@ -# Leetcodeͬϰ壩 -## Ŀ¼ -- Ŀ01ջʵֶ -- Ŀ02ľ -- Ŀ03ת -- Ŀ04ǰ׺ -- Ŀ05תַ -- Ŀ06ظַӴ -- Ŀ07Ӵ -- Ŀ08ַ -- Ŀ09ʽƥ -- Ŀ10ͨƥ - - ---- -## Ŀ01ջʵֶ - -> - ţ232 -> - Ѷȣ -> - https://leetcode-cn.com/problems/implement-queue-using-stacks/ - -ʹջʵֶев - -- `push(x)` -- һԪطеβ -- `pop()` -- ӶײƳԪء -- `peek()` -- ضײԪء -- `empty()` -- ضǷΪա - - -ʾ: - -```c -MyQueue queue = new MyQueue(); - -queue.push(1); -queue.push(2); -queue.peek(); // 1 -queue.pop(); // 1 -queue.empty(); // false -``` - -˵: - -- ֻʹñ׼ջ -- Ҳֻpush to toppeek/pop from topsize is empty ǺϷġ -- ʹõҲ֧ջʹ`list``deque`˫˶УģһջֻҪDZ׼ջɡ -- вЧģ磬һյĶв`pop``peek` - -**ο룺** - -- ִнͨ -- ִʱ112 ms, C# ύл 95.10% û -- ڴģ24.3 MB, C# ύл 5.33% û - -```c -public class MyQueue -{ - private Stack _stack = new Stack(); - - /** Initialize your data structure here. */ - public MyQueue() - { - ; - } - - /** Push element x to the back of queue. */ - public void Push(int x) - { - if (_stack.Count == 0) - { - _stack.Push(x); - return; - } - - Stack temp = new Stack(); - while (_stack.Count != 0) - { - temp.Push(_stack.Pop()); - } - _stack.Push(x); - while (temp.Count != 0) - { - _stack.Push(temp.Pop()); - } - } - - /** Removes the element from in front of queue and returns that element. */ - public int Pop() - { - return _stack.Count > 0 ? _stack.Pop() : -1; - } - - /** Get the front element. */ - public int Peek() - { - return _stack.Count > 0 ? _stack.Peek() : -1; - } - - /** Returns whether the queue is empty. */ - public bool Empty() - { - return _stack.Count == 0; - } -} - -/** - * Your MyQueue object will be instantiated and called as such: - * MyQueue obj = new MyQueue(); - * obj.Push(x); - * int param_2 = obj.Pop(); - * int param_3 = obj.Peek(); - * bool param_4 = obj.Empty(); - */ -``` - ---- -## Ŀ02ľ - -> - ţ766 -> - Ѷȣ -> - https://leetcode-cn.com/problems/toeplitz-matrix/ - - -ÿһϵµĶԽϵԪضͬô ľ - -һ`M x N`ľ󣬵ҽľʱ`True` - -**ʾ 1:** - -```c -: -matrix = [ - [1,2,3,4], - [5,1,2,3], - [9,5,1,2] -] -: True -: -, ԽΪ: -"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]" -ԽϵԪؾͬ, ˴True -``` - -**ʾ 2:** - -```c -: -matrix = [ - [1,2], - [2,2] -] -: False -: -Խ"[1, 2]"ϵԪزͬ -``` - -**˵:** - -1. `matrix`һĶά顣 -2. `matrix` [1, 20]Χڡ -3. `matrix[i][j]` [0, 99]Χڡ - - -**:** - -1. 洢ڴϣҴڴ޵ģһֻܽһоصڴУô죿 -2. ֻ̫һνмصڴУô죿 - -**˼·**жԽϵԪض$a_{11} = a_{00}, a_{22} = a_{11}, a_{kk} = a_{k-1k-1}$ڶԽϵԪ˵ǰԪزǵһֵԪأôǰԪһڵǰԪصϽǡƳλ$(r, c)$ϵԪأֻҪ`r == 0 OR c == 0 OR matrix[r-1][c-1] == matrix[r][c]`Ϳˡ - -**ο** - -- ִнͨ -- ִʱ128 ms, C# ύл 47.50% û -- ڴģ27.5 MB, C# ύл 16.13% û - -```c -public class Solution -{ - public bool IsToeplitzMatrix(int[][] matrix) - { - for (int i = 1; i < matrix.Length; i++) - { - int[] row = matrix[i]; - for (int j = 1; j < row.Length; j++) - { - if (matrix[i][j] != matrix[i - 1][j - 1]) - return false; - } - } - return true; - } -} -``` - - ---- -## Ŀ03ת - -> - ţ13 -> - Ѷȣ -> - https://leetcode-cn.com/problems/roman-to-integer/ - -ְַ: `I V X LCD``M` - -``` -ַ ֵ -I 1 -V 5 -X 10 -L 50 -C 100 -D 500 -M 1000 -``` - -磬 2 д`II`Ϊе 112 д`XII`Ϊ`X + II` 27 д`XXVII`, Ϊ`XX + V + II` - - -ͨ£СڴֵұߡҲ 4 д`IIII``IV` 1 5 ߣʾڴ 5 С 1 õֵ 4 ͬأ 9 ʾΪ`IX`Ĺֻ - -``` -I Է V (5) X (10) ߣʾ 4 9 -X Է L (50) C (100) ߣʾ 40 90 -C Է D (500) M (1000) ߣʾ 400 900 -``` - -һ֣תȷ 1 3999 ķΧڡ - -**ʾ 1:** - -```c -:"III" -: 3 -``` - -**ʾ 2:** - -```c -: "IV" -: 4 -``` - -**ʾ 3:** -```c -: "IX" -: 9 -``` - -**ʾ 4:** -```c -: "LVIII" -: 58 -: L = 50, V= 5, III = 3. -``` - -**ʾ 5:** -```c -: "MCMXCIV" -: 1994 -: M = 1000, CM = 900, XC = 90, IV = 4. -``` - -**˼·**ֱӷݡСڴֵұߡԼĹֱȥд롣 - -**ο** - -- ִнͨ -- ִʱ96 ms, C# ύл 95.88% û -- ڴģ25.7 MB, C# ύл 5.27% û - -```c -public class Solution -{ - public int RomanToInt(string s) - { - int result = 0; - int count = s.Length; - int i = 0; - while (i < count) - { - char c = s[i]; - int move = 0; - switch (c) - { - case 'I': - result += PreI(i, s, ref move); - break; - case 'V': - result += 5; - move = 1; - break; - case 'X': - result += PreX(i, s, ref move); - break; - case 'L': - result += 50; - move = 1; - break; - case 'C': - result += PreC(i, s, ref move); - break; - case 'D': - result += 500; - move = 1; - break; - case 'M': - result += 1000; - move = 1; - break; - } - i += move; - } - return result; - } - private int PreI(int index, string s, ref int move) - { - //I 1 - //IV 4 - //IX 9 - //II 2 - int result = 0; - int count = s.Length; - if (index + 1 < count) - { - char c = s[index + 1]; - switch (c) - { - case 'V': - result = 4; - move = 2; - break; - case 'X': - result = 9; - move = 2; - break; - case 'I': - result = 2; - move = 2; - break; - } - } - else - { - result = 1; - move = 1; - } - return result; - } - - private int PreX(int index, string s, ref int move) - { - //X 10 - //XL 40 - //XC 90 - int result = 0; - int count = s.Length; - if (index + 1 < count) - { - char c = s[index + 1]; - switch (c) - { - case 'L': - result = 40; - move = 2; - break; - case 'C': - result = 90; - move = 2; - break; - default: - result = 10; - move = 1; - break; - } - } - else - { - result = 10; - move = 1; - } - return result; - } - - private int PreC(int index, string s, ref int move) - { - //C 100 - //CD 400 - //CM 900 - int result = 0; - int count = s.Length; - if (index + 1 < count) - { - char c = s[index + 1]; - switch (c) - { - case 'D': - result = 400; - move = 2; - break; - case 'M': - result = 900; - move = 2; - break; - default: - result = 100; - move = 1; - break; - } - } - else - { - result = 100; - move = 1; - } - return result; - } -} -``` - ---- -## Ŀ04ǰ׺ - -> - ţ14 -> - Ѷȣ -> - https://leetcode-cn.com/problems/longest-common-prefix/ - -дһַеǰ׺ - -ڹǰ׺ؿַ "" - -ʾ 1: -```c -: ["flower","flow","flight"] -: "fl" -``` - -ʾ 2: -```c -: ["dog","racecar","car"] -: "" -: 벻ڹǰ׺ -``` - -˵: - -ֻСдĸ a-z - - -**ο** - -- ״̬ͨ -- ִʱ: 144 ms, C# ύл 94.92% û -- ڴ: 23.4 MB, C# ύл 11.69% û - - -```c -public class Solution { - public string LongestCommonPrefix(string[] strs) - { - if (strs.Length == 0) - return string.Empty; - - string result = strs[0]; - for (int i = 1; i < strs.Length; i++) - { - result = Prefix(result, strs[i]); - if (string.IsNullOrEmpty(result)) - break; - } - return result; - } - - public string Prefix(string str1, string str2) - { - int len1 = str1.Length; - int len2 = str2.Length; - int len = Math.Min(len1, len2); - int i = 0; - for (; i < len; i++) - { - if (str1[i] != str2[i]) - break; - } - return i == 0 ? string.Empty : str1.Substring(0, i); - } -} -``` - ---- -## Ŀ05תַ - -> - ţ344 -> - Ѷȣ -> - https://leetcode-cn.com/problems/reverse-string/ - - - -дһǽַתַַ char[] ʽ - -ҪĿռ䣬ԭ ޸ʹ O(1) Ķռһ⡣ - -Լеַ ASCII еĿɴӡַ - - -ʾ 1 -```c -룺["h","e","l","l","o"] -["o","l","l","e","h"] -``` - -ʾ 2 -```c -룺["H","a","n","n","a","h"] -["h","a","n","n","a","H"] -``` - - - -**ο룺** - -- ״̬ͨ -- ִʱ: 572 ms, C# ύл 94.94% û -- ڴ: 33.6 MB, C# ύл 5.05% û - -```c -public class Solution { - public void ReverseString(char[] s) { - int i = 0; - int j = s.Length-1; - while (i < j) - { - char c = s[i]; - s[i] = s[j]; - s[j] = c; - i++; - j--; - } - } -} -``` - ---- -## Ŀ06ظַӴ - -> - ţ3 -> - Ѷȣе -> - https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ - -һַҳвظַ **Ӵ** ijȡ - -**ʾ 1:** - -```c -: "abcabcbb" -: 3 -: ΪظַӴ "abc"䳤Ϊ 3 -``` - -**ʾ 2:** - -```c -: "bbbbb" -: 1 -: ΪظַӴ "b"䳤Ϊ 1 -``` - -**ʾ 3:** -```c -: "pwwkew" -: 3 -: ΪظַӴ "wke"䳤Ϊ 3 - ע⣬Ĵ𰸱 Ӵ ijȣ"pwke" һУӴ -``` - -**˼·** - -̬滮˼·ǰÿλΪֹλãظӴijȣ֮Щȵֵɡ - -λ`index`ظӴijΪ`result[index] = min{k1,k2}``k1 = result[index-1] + 1``k2`Ϊ`index`λǰֱ`index`λõַ`index=0`ΪֹӴȡ - - -**ο룺** - -- ִнͨ -- ִʱ96 ms, C# ύл 82.81% û -- ڴģ25.2 MB, C# ύл 25.52% û - -```c -public class Solution -{ - public int LengthOfLongestSubstring(string s) - { - if (string.IsNullOrEmpty(s)) - return 0; - int[] result = new int[s.Length]; - result[0] = 1; - - for (int i = 1; i < s.Length; i++) - { - int count = GetLength(i, s); - result[i] = result[i-1] < count ? result[i-1]+1 : count; - } - return result.Max(); - } - private int GetLength(int index,string s) - { - char c = s[index]; - int result = 1; - for (int i = index-1; i >= 0; i--) - { - if (s[i] != c) - result += 1; - else - break; - } - return result; - } -} -``` - ---- -## Ŀ07Ӵ - -> - ţ5 -> - Ѷȣе -> - https://leetcode-cn.com/problems/longest-palindromic-substring/ - - -һַ sҵ s ĻӴԼ s 󳤶Ϊ 1000 - -ʾ 1 -```c -: "babad" -: "bab" -ע: "aba" ҲһЧ𰸡 -``` - -ʾ 2 -```c -: "cbbd" -: "bb" -``` - -ʾ 3 -```c -: "a" -: "a" -``` - -**˼·** ö̬滮ķ - -һͷַͬ磬abaǻģabcǡ - -̬滮㷨ηƣ˼Ҳǽֽɸ⣬⣬ȻЩĽõԭĽ⡣ - -ηͬǣʺö̬滮⣬ֽõǻģһӽ׶εǽһӽ׶εĽĻϣнһ⣩÷η⣬ֽõĿ̫࣬Щⱻظ˺ܶΡܹѽĴ𰸣ҪʱҳõĴ𰸣ͿԱظ㣬ʡʱ - -ǿһ¼ѽĴ𰸡ܸԺǷõֻҪͽСǶ̬滮Ļ˼· - -Ķ̬滮㷨ֶǾͬʽ - -ʹüǺ `s[l, r]` ʾԭʼַһӴ`l``r` ֱұֵ߽ʹաұʾұ߽ȡ - -`dp[l, r]` ʾӴ `s[l, r]`Ҷ˵㣩Ƿ񹹳ɻĴһά顣 - -- Ӵֻ 1 ַһǻӴ -- Ӵ 2 ַʱ`s[l, r]` һĴôĴ߸һַԵĻӴ `s[l + 1, r - 1]` ҲһǻĴ - -ʣ `s[l] == s[r]` ʱ`dp[l, r]`ֵ `dp[l + 1, r - l]` ﻹҪٶ࿼һ㣺ԭַȥұ߽硱Ӵı߽ - -- ԭַԪظΪ 3 ʱұ߽ȣôȥԺֻʣ 1 ַһǻĴԭַҲһǻĴ -- ԭַԪظΪ 2 ʱұ߽ȣôȥԺֻʣ 0 ַȻԭַҲһǻĴ - -ϣһַұ߽ȣжΪֻ¶֮һɣ -- ȥұ߽Ժַ䣬`s[l + 1, r - 1]`Ԫ2`r - l <= 2` -- ȥұ߽ԺַǻĴ`dp[l + 1, r - 1] == true` - -![](https://img-blog.csdnimg.cn/20200317155934443.png) - -**ο** - -- ״̬ͨ -- ִʱ: 232 ms, C# ύл 46.79% û -- ڴ: 40.9 MB, C# ύл 5.43% û - - -```c -public class Solution { - public string LongestPalindrome(string s) - { - if (string.IsNullOrEmpty(s)) - return string.Empty; - int len = s.Length; - if (len == 1) - return s; - int longestPalindromelen = 1; - string longestPalindromeStr = s.Substring(0, 1); - bool[,] dp = new bool[len, len]; - - for (int r = 1; r < len; r++) - { - for (int l = 0; l < r; l++) - { - if (s[r] == s[l] && (r - l <= 2 || dp[l + 1, r - 1] == true)) - { - dp[l, r] = true; - if (longestPalindromelen < r - l + 1) - { - longestPalindromelen = r - l + 1; - longestPalindromeStr = s.Substring(l, r - l + 1); - } - } - } - } - return longestPalindromeStr; - } -} -``` - - ---- -## Ŀ08ַ - -> - ţ43 -> - Ѷȣе -> - https://leetcode-cn.com/problems/multiply-strings/ - - -ַʽʾķǸ`num1``num2``num1``num2`ij˻ǵij˻ҲʾΪַʽ - -ʾ 1: -```c -: num1 = "2", num2 = "3" -: "6" -``` - -ʾ 2: -```c -: num1 = "123", num2 = "456" -: "56088" -``` - - - -ʾ 3: -```c -: num1 = "498828660196", num2 = "840477629533" -: "419254329864656431168468" -``` - - -˵ - -- `num1``num2`ijС110 -- `num1``num2`ֻ 0-9 -- `num1``num2`㿪ͷ 0 -- ʹκα׼Ĵͣ BigIntegerֱӽתΪ - - -**ο** - -- ״̬ͨ -- ִʱ132 ms, C# ύл 94.74% û -- ڴģ24.1 MB, C# ύл 31.82% û - -```c -public class Solution { - public string Multiply(string num1, string num2) { - if (num1 == "0" || num2 == "0") - return "0"; - int len1 = num1.Length; - int len2 = num2.Length; - int len = len1 + len2; - int[] temp = new int[len]; - - for (int i = len2 - 1; i >= 0; i--) - { - int k = len2 - i; - int b = num2[i] - '0'; - for (int j = len1 - 1; j >= 0; j--) - { - int a = num1[j] - '0'; - int c = a*b; - - temp[len - k] += c%10; - if (temp[len - k] >= 10) - { - temp[len - k] = temp[len - k]%10; - temp[len - k - 1]++; - } - - temp[len - k - 1] += c/10; - if (temp[len - k - 1] >= 10) - { - temp[len - k - 1] = temp[len - k - 1]%10; - temp[len - k - 2]++; - } - k++; - } - } - - StringBuilder sb = new StringBuilder(); - int s = temp[0] == 0 ? 1 : 0; - while (s < len) - { - sb.Append(temp[s]); - s++; - } - return sb.ToString(); - } -} -``` - - - - - - ---- -## Ŀ09ʽƥ - -> - ţ10 -> - Ѷȣ -> - https://leetcode-cn.com/problems/regular-expression-matching/ - -һַ`s`һַ`p`ʵһ֧ '.' '*' ʽƥ䡣 - -```c -'.' ƥⵥַ -'*' ƥǰһԪ -``` - -νƥ䣬Ҫ **** ַ`s`ģDzַ - -**˵:** - -- `s`Ϊգֻ a-z Сдĸ -- `p`Ϊգֻ a-z СдĸԼַ '.' '*' - -**ʾ 1:** - -```c -: -s = "aa" -p = "a" -: false -: "a" ޷ƥ "aa" ַ -``` -**ʾ 2:** - -```c -: -s = "aa" -p = "a*" -: true -: Ϊ '*' ƥǰһԪ, ǰԪؾ 'a'ˣַ "aa" ɱΪ 'a' ظһΡ -``` - -**ʾ 3:** - -```c -: -s = "ab" -p = ".*" -: true -: ".*" ʾƥ'*'ַ'.' -``` - -**ʾ 4:** - -```c -: -s = "aab" -p = "c*a*b" -: true -: Ϊ '*' ʾ 'c' Ϊ 0 , 'a' ظһΡ˿ƥַ "aab" -``` - -**ʾ 5:** - -```c -: -s = "mississippi" -p = "mis*is*p*." -: false -``` - -**˼·**ݷ - -ƥ˼·ʵDzϵؼ`s``p`ĿƥײֱһַΪյʱ򣬸óۡ - -ַֻͨƥ䣬Ƚϼɣ - -```c -if (s[i] == p[i]) -``` - -ʽַpֻһ"."һǣȻǰȽϼ - -```c -if (s[i] == p[i] || p[i] == '.') -``` - -ʵʱҪжַȺַпյIJ - -ǣ"*"ַҪ⴦pĵiԪصһԪǺʱ - -- `i`ԪҪ0ΣǾͱ`s`䣬`p`ļԪأ`IsMatch``sbcpa*bc`Ǿͱ`s`䣬`p`"a*"`IsMatch(s:bc,p:bc)` -- `i`ԪҪһλΣȱȽ`i`Ԫغ`s`Ԫأ򱣳`p`䣬`s`Ԫأ`IsMatch``saabbpa*bb`ͱ`p`䣬`s`Ԫأ`IsMatch(s:abb,p:a*bb)` - -ʱһЩҪ˼`sabbpa*abb`ַʽ - -- ڶȽ`i`Ԫغ`s`ԪأȾͻ`s`ַ`IsMatch(s:bb,p:a*abb)`ڰһȥ`p`Ԫأ`IsMatch(s:bb,p:abb)`յ`false` -- ֱӰһȥ`p`Ԫأ`IsMatch(s:abb,p:abb)`յ`true` - -˵һֱὫеһߣǷڿƥ - - - - -**ο** - -- ִнͨ -- ִʱ768 ms, C# ύл 10.69% û -- ڴģ25.6 MB, C# ύл 5.00% û - -```c -public class Solution -{ - public bool IsMatch(string s, string p) - { - //pΪմsΪմƥɹsΪմƥʧܡ - if (string.IsNullOrEmpty(p)) - return string.IsNullOrEmpty(s) ? true : false; - - //жspַǷƥ䣬עҪжsΪ - bool headMatched = string.IsNullOrEmpty(s) == false - && (s[0] == p[0] || p[0] == '.'); - - if (p.Length >= 2 && p[1] == '*') - { - //pĵһԪصһԪ*ֱж - return IsMatch(s, p.Substring(2)) || - (headMatched && IsMatch(s.Substring(1), p)); - } - else if (headMatched) - { - //spַ - return IsMatch(s.Substring(1), p.Substring(1)); - } - else - { - return false; - } - } -} -``` - -## Ŀ10ͨƥ - - -> - ţ44 -> - Ѷȣ -> - https://leetcode-cn.com/problems/wildcard-matching/ - - -һַ(`s`) һַģʽ(`p`) ʵһ֧`'?'``'*'`ͨƥ䡣 - -``` -'?' ƥκεַ -'*' ƥַַ -ַȫƥƥɹ -``` - -**˵:** - -- `s`Ϊգֻ`a-z`Сдĸ -- `p`Ϊգֻ`a-z`СдĸԼַ`?``*` - -**ʾ 1:** - -```c -: -s = "aa" -p = "a" -: false -: "a" ޷ƥ "aa" ַ -``` -**ʾ 2:** - -```c -: -s = "aa" -p = "*" -: true -: '*' ƥַ -``` - -**ʾ 3:** - -```c -: -s = "cb" -p = "?a" -: false -: '?' ƥ 'c', ڶ 'a' ޷ƥ 'b' -``` - -**ʾ 4:** - -```c -: -s = "adceb" -p = "*a*b" -: true -: һ '*' ƥַ, ڶ '*' ƥַ "dce". -``` - -**ʾ 5:** - -```c -: -s = "acdcb" -p = "a*c?b" -: false -``` - -**ʾ 6:** - -```c -룺 -"abefcdgiescdfimde" -"ab*cd?i*de" -true -``` - -**ʾ 7:** - -```c -룺 -"aaaa" -"***a" -true -``` - - -**˼·**˫ - -`i``j`ֱ`s``p`ĵһַ±꣬ʼΪ0`istart``jstart`ֱ`s``p``'*'`ƥλãʼΪ`-1` - -ַͨƥ˼·࣬ѾƥɹIJ־ͲٿˣҪ`i``j`ǵǰڱȽϵַƥ`'*'`ܻᱻظʹȥƥַҪ`istart``jstart`ֱ`s``p`ƥ`'*'`λá - -1. `i``j`ǵַȻ`j`ַ`'?'`ƥɹ"Ƴ"`i``j`Ԫأ`i``j` -2. `j`ַ`'*'`Ȼƥɹ`istart``jstart`ֱ`i`Ԫغ`j`Ԫأ`j` -3. ٷ`istart>-1`˵֮ǰƥ`'*'`Ϊ`'*'`ƥַҪٴƥ`'*'`ƥַƶ`i``istart`һַ`istart`±`i`Ԫأͬʱƶ`j``jstart`һַ -4. 㣬ƥʧܣ`false` - -`s`еַжϣΪ`s`ΪգʱҪ`p`Ϊջ`p`ֻʣǺŵʱ򣬲ܳɹƥ䡣 - -**ο** - -- ִнͨ -- ִʱ92 ms, C# ύл 95.00% û -- ڴģ25.7 MB, C# ύл 66.67% û - -```c -public class Solution -{ - public bool IsMatch(string s, string p) - { - //pΪմsΪմƥɹsΪմƥʧܡ - if (string.IsNullOrEmpty(p)) - return string.IsNullOrEmpty(s) ? true : false; - - int i = 0, j = 0, istart = -1, jstart = -1, plen = p.Length; - - //жsַǷƥ - while (i < s.Length) - { - //ƥɹԼƥʧܷfalse - if (j < plen && (s[i] == p[j] || p[j] == '?')) - { - i++; - j++; - } - else if (j < plen && p[j] == '*') - { - istart = i; - jstart = j; - j++; - } - else if (istart > -1) - { - i = istart + 1; - istart = i; - j = jstart + 1; - } - else - { - return false; - } - } - //sеַжϣΪsΪ - //ʱҪpΪջpֻʣǺŵʱ򣬲ܳɹƥ䡣 - //pʣĶ*Ƴʣ* - while (j < plen && p[j] == '*') - { - j++; - } - return j == plen; - } -} -``` - diff --git a/DataStructureAndAlgorithm/16 Leetcode同步练习(六).md b/DataStructureAndAlgorithm/16 Leetcode同步练习(六).md deleted file mode 100644 index edd67d6..0000000 --- a/DataStructureAndAlgorithm/16 Leetcode同步练习(六).md +++ /dev/null @@ -1,1054 +0,0 @@ - -# Leetcodeͬϰ -## Ŀ¼ -- Ŀ01ͬ -- Ŀ02Գƶ -- Ŀ03 -- Ŀ04 Pow(x, n) -- Ŀ05Ӽ -- Ŀ06ױ -- Ŀ07 -- Ŀ08ǰ -- Ŀ09 -- Ŀ10ĺ - ---- -## Ŀ01ͬ - -> - ţ100 -> - Ѷȣ -> - https://leetcode-cn.com/problems/same-tree/ - -дһǷͬ - -ڽṹͬҽڵֵͬΪͬġ - -**ʾ 1**: - -``` -: 1 1 - / \ / \ - 2 3 2 3 - - [1,2,3], [1,2,3] - -: true -``` - -**ʾ 2**: - -``` -: 1 1 - / \ - 2 2 - - [1,2], [1,null,2] - -: false -``` - -**ʾ 3**: -``` -: 1 1 - / \ / \ - 2 1 1 2 - - [1,2,1], [1,1,2] - -: false -``` - -**ο룺** - -- ִнͨ -- ִʱ160 ms, C# ύл 5.85% û -- ڴģ24 MB, C# ύл 6.67% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ - -public class Solution -{ - public bool IsSameTree(TreeNode p, TreeNode q) - { - //ݹֹ - if (p == null && q == null) - return true; - - if (p != null && q != null && p.val == q.val) - { - return IsSameTree(p.left, q.left) - && IsSameTree(p.right, q.right); - } - return false; - } -} -``` - ---- -## Ŀ02Գƶ - -> - ţ101 -> - Ѷȣ -> - https://leetcode-cn.com/problems/symmetric-tree/ - -һǷǾԳƵġ - -磬 `[1,2,2,3,4,4,3]` ǶԳƵġ - -``` - 1 - / \ - 2 2 - / \ / \ -3 4 4 3 -``` - - `[1,2,2,null,3,null,3]` ǾԳƵ: - -``` - 1 - / \ - 2 2 - \ \ - 3 3 -``` - -**ο룺** - -**һ֣õݹķ** - -- ִнͨ -- ִʱ132 ms, C# ύл 16.67% û -- ڴģ25.1 MB, C# ύл 5.17% û - - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ - -//ԳƵĵݹ麯 -public class Solution -{ - public bool IsSymmetric(TreeNode root) - { - return IsMirror(root, root); - } - private bool IsMirror(TreeNode t1, TreeNode t2) - { - if (t1 == null && t2 == null) return true; - if (t1 == null || t2 == null) return false; - return (t1.val == t2.val) - && IsMirror(t1.left, t2.right) - && IsMirror(t1.right, t2.left); - } -} -``` - -**ڶ֣öеķ** - -**˼·**öIJαķʽʵ֡ - -- ִнͨ -- ִʱ112 ms, C# ύл 70.93% û -- ڴģ24.9 MB, C# ύл 5.17% û - -```c -public class Solution -{ - public bool IsSymmetric(TreeNode root) - { - if (root == null) - return true; - - Queue nodes = new Queue(); - nodes.Enqueue(root.left); - nodes.Enqueue(root.right); - while (nodes.Count != 0) - { - TreeNode node1 = nodes.Dequeue(); - TreeNode node2 = nodes.Dequeue(); - - if (node1 == null && node2 == null) - continue; - if (node1 == null || node2 == null) - return false; - if (node1.val != node2.val) - return false; - nodes.Enqueue(node1.left); - nodes.Enqueue(node2.right); - nodes.Enqueue(node1.right); - nodes.Enqueue(node2.left); - } - return true; - } -} -``` - ---- -## Ŀ03 - -> - ţ104 -> - Ѷȣ -> - https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ - - -һҳȡ - -Ϊڵ㵽ԶҶӽڵ·ϵĽڵ - -˵: ҶӽڵָûӽڵĽڵ㡣 - -**ʾ** - - [3,9,20,null,null,15,7] - -```c - 3 - / \ - 9 20 - / \ - 15 7 -``` - - 3 - -**ο룺** - -**һ֣öʵֲα˼·** - -- ִнͨ -- ִʱ108 ms, C# ύл 88.13% û -- ڴģ25.5 MB, C# ύл 5.97% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public int MaxDepth(TreeNode root) - { - if (root == null) - return 0; - - Queue q = new Queue(); - int deep = 0; - q.Enqueue(root); - - while (q.Count != 0) - { - deep++; - int count = 0; - int size = q.Count; - - while (count < size) - { - TreeNode node = q.Dequeue(); - count++; - - if (node.left != null) - q.Enqueue(node.left); - if (node.right != null) - q.Enqueue(node.right); - } - } - return deep; - } -} -``` - - - -**ڶ֣õݹ** - -**˼·**ݹֱȣӵԭвϣ󷵻еֵ - -- ִнͨ -- ִʱ132 ms, C# ύл 16.62% û -- ڴģ25.5 MB, C# ύл 6.06% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public int MaxDepth(TreeNode root) - { - if (root == null) - return 0; - int llen = 1; - int rlen = 1; - if (root.left != null) - { - llen += MaxDepth(root.left); - } - if (root.right != null) - { - rlen += MaxDepth(root.right); - } - return llen > rlen ? llen : rlen; - } -} -``` ---- -## Ŀ04 Pow(x, n) - -> - ţ50 -> - Ѷȣе -> - https://leetcode-cn.com/problems/powx-n/ - - -ʵ `pow(x, n)` x n ݺ - -**ʾ 1:** - -```c -: 2.00000, 10 -: 1024.00000 -``` - -**ʾ 2:** -```c -: 2.10000, 3 -: 9.26100 -``` - -**ʾ 3:** -```c -: 2.00000, -2 -: 0.25000 -: 2-2 = 1/22 = 1/4 = 0.25 -``` - -**ʾ 4:** -```c -: 1.00000, -2147483648 -: 1.00000 -``` - -**˵:** - -- -100.0 < x < 100.0 -- n 32 λзֵΧ [?2^31, 2^31 ? 1] - - -**˼·**ÿݷ - -Ҫ`a^b`ô`b`ԲɶƱʾ統`b = 5`ʱ5Ķ0101`5 = 2^30 + 2^21 + 2^10 + 2^01`ˣǽ`a^5`תΪ `a^(2^30 + 2^21 + 2^10 + 2^01)``a^(2^0) a^(2^2)` - - -![](https://img-blog.csdnimg.cn/20200419235913493.png) - -2ݣȻx2ݴηٰnɶƣѶƵжӦλ1ֵ͵õ˽׷Ϊ **ݷ** - - -**ο** - -- ִнͨ -- ִʱ56 ms, C# ύл 51.87% û -- ڴģ15.1 MB, C# ύл 50.00% û - -```c -public class Solution -{ - public double MyPow(double x, int n) - { - int neg = n < 0 ? -1 : 1; - long g = Math.Abs((long)n); - - double[] d = new double[32]; - d[0] = x; - for (int i = 1; i < 32; i++) - { - d[i] = d[i - 1] * d[i - 1]; - } - - double result = 1.0d; - for (int i = 0; i < 32; i++) - { - int mask = 1 << i; - if ((mask & g) != 0) - { - result *= d[i]; - } - } - return neg != -1 ? result : 1.0 / result; - } -} -``` ---- -## Ŀ05Ӽ - -> - ţ78 -> - Ѷȣе -> - https://leetcode-cn.com/problems/subsets/ - - -һ**ظԪ** numsظпܵӼݼ - -˵⼯ܰظӼ - -ʾ: -```c -: nums = [1,2,3] -: -[ - [3], - [1], - [2], - [1,2,3], - [1,3], - [2,3], - [1,2], - [] -] -``` - -**ο** - -**һ֣ݷ** - -nums[i]ΪʼҺֵҪǰһֵظ - -![ݹ](https://img-blog.csdnimg.cn/20190913152245222.png) - -- ״̬ͨ -- ʱ: 356 ms, C# ύл 92.31% û -- ڴ: 29.2 MB, C# ύл 6.67% û - -```c -public class Solution -{ - private IList> _result; - - public IList> Subsets(int[] nums) - { - _result = new List>(); - int len = nums.Length; - - if (len == 0) - { - return _result; - } - IList item = new List(); - Find(nums, 0, item); - return _result; - } - - private void Find(int[] nums, int begin, IList item) - { - // ע⣺Ҫ new һ - _result.Add(new List(item)); - - if (begin == nums.Length) - return; - - for (int i = begin; i < nums.Length; i++) - { - item.Add(nums[i]); - Find(nums, i + 1, item); - - // ⣬״̬ڵݹɺҪ - item.RemoveAt(item.Count - 1); - } - } -} -``` - -**ڶ֣Ӽչ** - -- ״̬ͨ -- ִʱ: 352 ms, C# ύл 94.51% û -- ڴ: 29.2 MB, C# ύл 6.67% û - -```c -public class Solution -{ - public IList> Subsets(int[] nums) - { - IList> result = new List>(); - IList item = new List(); - result.Add(item); - for (int i = 0; i < nums.Length; i++) - { - for (int j = 0, len = result.Count; j < len; j++) - { - item = new List(result[j]); - item.Add(nums[i]); - result.Add(item); - } - } - return result; - } -} -``` - -**֣λ** - -**˼·** ϵ˼· - -`{1,2,3}`Ϊ`2^3`Ӽ - -```c -000 -> [] -100 -> [1] -101 -> [1,3] -110 -> [1,2] -111 -> [1,2,3] -... -``` - -- ״̬ͨ -- ִʱ: 348 ms, C# ύл 97.80% û -- ڴ: 29.5 MB, C# ύл 6.67% û - -```c -public class Solution -{ - public IList> Subsets(int[] nums) - { - IList> result = new List>(); - int count = nums.Length; - - for (int i = 0; i < 1 << count; i++) - { - IList item = new List(); - for (int j = 0; j < count; j++) - { - int mask = 1 << j; - if ((mask & i) != 0) - item.Add(nums[j]); - } - result.Add(item); - } - return result; - } -} -``` - ---- -## Ŀ06ױ - -> - ţ89 -> - Ѷȣе -> - https://leetcode-cn.com/problems/gray-code/ - -ױһϵͳڸϵͳУֵһλIJ졣 - -һλķǸ nӡױСױб 0 ͷ - -ʾ 1: -```c -: 2 -: [0,1,3,2] -: -00 - 0 -01 - 1 -11 - 3 -10 - 2 - -ڸ nױвΨһ -磬[0,2,3,1] ҲһЧĸױС - -00 - 0 -10 - 2 -11 - 3 -01 - 1 -``` - -ʾ 2: -```c -: 0 -: [0] -: Ƕױб 0 ͷ - λΪ n ĸױУ䳤Ϊ 2^n - n = 0 ʱΪ 2^0 = 1 - ˣ n = 0 ʱױΪ [0] -``` - -**˼·** - - -![׸](https://img-blog.csdnimg.cn/20190915115931873.png) - - n λƵ n+1 λʱn+1 λ n λͬʱ n λڸλһλ 1 γɵһһҪǰһС - -**ο** - -- ״̬ͨ -- 12 / 12 ͨ -- ִʱ: 296 ms, C# ύл 95.83% û -- ڴ: 24.8 MB, C# ύл 16.67% û - -```c -public class Solution -{ - public IList GrayCode(int n) - { - IList lst = new List(); - lst.Add(0); - for (int i = 1; i <= n; i++) - { - for (int j = lst.Count - 1; j >= 0; j--) - { - int item = lst[j] + (1 << i - 1); - lst.Add(item); - } - } - return lst; - } -} -``` - - ---- -## Ŀ07 - - - -> - ţ236 -> - Ѷȣе -> - https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ - -һ, ҵָڵȡ - -ٶȰٿȵĶΪи T pqȱʾΪһ x x pq x ȾܴһڵҲԼ - -磬¶: root = [3,5,1,6,2,0,8,null,null,7,4] - -![](https://img-blog.csdnimg.cn/20190922095031349.png) - -ʾ 1: -```c -: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 -: 3 -: ڵ 5 ͽڵ 1 ǽڵ 3 -``` - -ʾ 2: -```c -: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 -: 5 -: ڵ 5 ͽڵ 4 ǽڵ 5ΪݶȽڵΪڵ㱾 -``` - -˵: - -- нڵֵΨһġ -- pq ΪͬڵҾڸĶС - - -**ο** - -**˼·**õݹ - -- ״̬ͨ -- ִʱ: 132 ms, C# ύл 96.10% û -- ڴ: 27.5 MB, C# ύл 20.00% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ - -public class Solution -{ - public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) - { - return Find(root, p, q); - } - - private TreeNode Find(TreeNode current, TreeNode p, TreeNode q) - { - if (current == null || current == p || current == q) - return current; - TreeNode left = Find(current.left, p, q); - TreeNode right = Find(current.right, p, q); - if (left != null && right != null) - return current; - return left != null ? left : right; - } -} -``` - - - ---- -## Ŀ08ǰ - -> - ţ144 -> - Ѷȣе -> - https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ - - -һ ǰ - -**ʾ:** - - -```c -: [1,null,2,3] - 1 - \ - 2 - / - 3 - -: [1,2,3] -``` - - -: ݹ㷨ܼ򵥣ͨ㷨 - - -**ο** - -**һ֣ջ** - -- ִнͨ -- ִʱ276 ms, C# ύл 84.15% û -- ڴģ29.9 MB, C# ύл 5.00% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public IList PreorderTraversal(TreeNode root) - { - IList lst = new List(); - Stack stack = new Stack(); - while (stack.Count != 0 || root != null) - { - if (root != null) - { - stack.Push(root); - lst.Insert(lst.Count, root.val); - root = root.left; - } - else - { - TreeNode node = stack.Pop(); - root = node.right; - } - } - return lst; - } -} -``` - -**ڶ֣õݹ** - -- ִнͨ -- ִʱ280 ms, C# ύл 79.27% û -- ڴģ29.8 MB, C# ύл 5.00% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public IList PreorderTraversal(TreeNode root) - { - IList lst = new List(); - PreOrder(root, lst); - return lst; - } - private void PreOrder(TreeNode node, IList lst) - { - if (node == null) - return; - - lst.Add(node.val); - PreOrder(node.left, lst); - PreOrder(node.right, lst); - } -} -``` - - ---- -## Ŀ09 - -> - ţ94 -> - Ѷȣе -> - https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ - -һ - -**ʾ:** - -```c -: [1,null,2,3] - 1 - \ - 2 - / - 3 - -: [1,3,2] -``` - -: ݹ㷨ܼ򵥣ͨ㷨 - - -**ο룺** - -**һ֣ջ** - -- ִнͨ -- ִʱ284 ms, C# ύл 53.59% û -- ڴģ30 MB, C# ύл 6.67% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public IList InorderTraversal(TreeNode root) - { - IList lst = new List(); - Stack stack = new Stack(); - while (stack.Count != 0 || root != null) - { - if (root != null) - { - stack.Push(root); - root = root.left; - } - else - { - TreeNode node = stack.Pop(); - lst.Add(node.val); - root = node.right; - } - } - return lst; - } -} -``` - - - -**ڶ֣ʹõݹ** - -- ִнͨ -- ִʱ264 ms, C# ύл 99.16% û -- ڴģ29.8 MB, C# ύл 6.67% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public IList InorderTraversal(TreeNode root) - { - IList lst = new List(); - MidOrder(root, lst); - return lst; - } - private void MidOrder(TreeNode node, IList lst) - { - if (node == null) - return; - MidOrder(node.left, lst); - lst.Add(node.val); - MidOrder(node.right, lst); - } -} -``` - - - ---- -## Ŀ10ĺ - -> - ţ145 -> - Ѷȣ -> - https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ - - -һ - -**ʾ:** - - -```c -: [1,null,2,3] - 1 - \ - 2 - / - 3 - -: [3,2,1] -``` - - -: ݹ㷨ܼ򵥣ͨ㷨 - - -**ο** - - -**һ֣ջ** - -ǰ˳Ϊ -> -> - -˳Ϊ -> -> - -1ǽǰнڵβ߼޸Ϊڵͷ - -ôͱΪˣ -> -> - -2ǽ˳ɴ޸Ϊҵ1 - -ôͱΪˣ -> -> - -պǺ˳ - -˼·Ǵʽ£ - -һ޸ǰУڵдĴ룬β޸Ϊ - -ڶ޸ǰУÿȲ鿴ڵٲ鿴ҽڵ߼ΪȲ鿴ҽڵٲ鿴ڵ - -- ִнͨ -- ִʱ272 ms, C# ύл 98.15% û -- ڴģ30 MB, C# ύл 6.90% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public IList PostorderTraversal(TreeNode root) - { - IList lst = new List(); - Stack stack = new Stack(); - while (stack.Count != 0 || root != null) - { - if (root != null) - { - stack.Push(root); - lst.Insert(0, root.val); - root = root.right; - } - else - { - TreeNode node = stack.Pop(); - root = node.left; - } - } - return lst; - } -} -``` - -**ڶ֣õݹ** - -- ִнͨ -- ִʱ276 ms, C# ύл 89.81% û -- ڴģ30 MB, C# ύл 6.90% û - -```c -/** - * Definition for a binary tree node. - * public class TreeNode { - * public int val; - * public TreeNode left; - * public TreeNode right; - * public TreeNode(int x) { val = x; } - * } - */ -public class Solution -{ - public IList PostorderTraversal(TreeNode root) - { - IList lst = new List(); - PostOrder(root, lst); - return lst; - } - private void PostOrder(TreeNode node, IList lst) - { - if (node == null) - return; - - PostOrder(node.left, lst); - PostOrder(node.right, lst); - lst.Add(node.val); - } -} -``` - - diff --git a/DataStructureAndAlgorithm/22 精选练习50题.md b/DataStructureAndAlgorithm/精选练习50题.md similarity index 100% rename from DataStructureAndAlgorithm/22 精选练习50题.md rename to DataStructureAndAlgorithm/精选练习50题.md