From 20834be10ba19e30666f64842b9f5ad0ac21cacc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=8B=A5=E6=9C=8B?= Date: Sun, 9 Jun 2019 23:58:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_01/id_7/LeetCode_189_7.cs | 22 ++++++++++++++++++++++ Week_01/id_7/LeetCode_21_7.cs | 20 ++++++++++++++++++++ Week_01/id_7/LeetCode_26_7.cs | 15 +++++++++++++++ Week_01/id_7/LeetCode_283_7.cs | 15 +++++++++++++++ Week_01/id_7/LeetCode_66_7.cs | 24 ++++++++++++++++++++++++ Week_01/id_7/LeetCode_70_7.cs | 22 ++++++++++++++++++++++ Week_01/id_7/LeetCode_88_7.cs | 19 +++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 Week_01/id_7/LeetCode_189_7.cs create mode 100644 Week_01/id_7/LeetCode_21_7.cs create mode 100644 Week_01/id_7/LeetCode_26_7.cs create mode 100644 Week_01/id_7/LeetCode_283_7.cs create mode 100644 Week_01/id_7/LeetCode_66_7.cs create mode 100644 Week_01/id_7/LeetCode_70_7.cs create mode 100644 Week_01/id_7/LeetCode_88_7.cs diff --git a/Week_01/id_7/LeetCode_189_7.cs b/Week_01/id_7/LeetCode_189_7.cs new file mode 100644 index 00000000..6dcaaf0e --- /dev/null +++ b/Week_01/id_7/LeetCode_189_7.cs @@ -0,0 +1,22 @@ +/* + * @lc app=leetcode.cn id=189 lang=csharp + * + * [189] 旋转数组 + */ +public class Solution { + public void Rotate (int[] nums, int k) { + k %= nums.Length; + Reverse (nums, 0, nums.Length - 1); + Reverse (nums, 0, k - 1); + Reverse (nums, k, nums.Length - 1); + } + public void Reverse (int[] nums, int start, int end) { + while (start < end) { + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + start++; + end--; + } + } +} \ No newline at end of file diff --git a/Week_01/id_7/LeetCode_21_7.cs b/Week_01/id_7/LeetCode_21_7.cs new file mode 100644 index 00000000..95e0ea17 --- /dev/null +++ b/Week_01/id_7/LeetCode_21_7.cs @@ -0,0 +1,20 @@ +/* + * @lc app=leetcode.cn id=21 lang=csharp + * + * [21] 合并两个有序链表 + */ +public class Solution { + public ListNode MergeTwoLists (ListNode l1, ListNode l2) { + if (l1 == null) { + return l2; + } else if (l2 == null) { + return l1; + } else if (l1.val < l2.val) { + l1.next = MergeTwoLists (l1.next, l2); + return l1; + } else { + l2.next = MergeTwoLists (l1, l2.next); + return l2; + } + } +} \ No newline at end of file diff --git a/Week_01/id_7/LeetCode_26_7.cs b/Week_01/id_7/LeetCode_26_7.cs new file mode 100644 index 00000000..d585c9c1 --- /dev/null +++ b/Week_01/id_7/LeetCode_26_7.cs @@ -0,0 +1,15 @@ +public class Solution { + public int RemoveDuplicates (int[] nums) { + if (nums.Length == 0) return 0; + if (nums.Length == 1) return 1; + + int count = 0; + for (int i = 1; i < nums.Length; i++) { + if (nums[count] != nums[i]) { + nums[++count] = nums[i]; + } + } + + return count + 1; + } +} \ No newline at end of file diff --git a/Week_01/id_7/LeetCode_283_7.cs b/Week_01/id_7/LeetCode_283_7.cs new file mode 100644 index 00000000..9bd5bf11 --- /dev/null +++ b/Week_01/id_7/LeetCode_283_7.cs @@ -0,0 +1,15 @@ +public class Solution { + public void MoveZeroes (int[] nums) { + if (nums == null || nums.Length == 0 || nums.Length == 1) return; + + int count = 0; + for (int i = 0; i < nums.Length; i++) { + if (nums[i] != 0) { + int temp = nums[count]; + nums[count] = nums[i]; + nums[i] = temp; + count++; + } + } + } +} \ No newline at end of file diff --git a/Week_01/id_7/LeetCode_66_7.cs b/Week_01/id_7/LeetCode_66_7.cs new file mode 100644 index 00000000..c735945b --- /dev/null +++ b/Week_01/id_7/LeetCode_66_7.cs @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=66 lang=csharp + * + * [66] 加一 + * + */ +using System; + +public class Solution { + public int[] PlusOne (int[] digits) { + for (int i = digits.Length - 1; i >= 0; i--) { + if ((digits[i] + 1) % 10 != 0) { + digits[i] += 1; + return digits; + } else { + digits[i] = 0; + } + } + + digits = new int[digits.Length + 1]; + digits[0] = 1; + return digits; + } +} \ No newline at end of file diff --git a/Week_01/id_7/LeetCode_70_7.cs b/Week_01/id_7/LeetCode_70_7.cs new file mode 100644 index 00000000..2e7fa630 --- /dev/null +++ b/Week_01/id_7/LeetCode_70_7.cs @@ -0,0 +1,22 @@ +public class Solution { + Dictionary dic = new Dictionary (); + public int ClimbStairs (int n) { + if (n == 1) return 1; + if (n == 2) return 2; + int A; + if (dic.ContainsKey (n - 1)) { + A = dic[n - 1]; + } else { + A = ClimbStairs (n - 1); + dic.Add (n - 1, A); + } + int B; + if (dic.ContainsKey (n - 2)) { + B = dic[n - 2]; + } else { + B = ClimbStairs (n - 2); + dic.Add (n - 2, B); + } + return A + B; + } +} \ No newline at end of file diff --git a/Week_01/id_7/LeetCode_88_7.cs b/Week_01/id_7/LeetCode_88_7.cs new file mode 100644 index 00000000..7434ac81 --- /dev/null +++ b/Week_01/id_7/LeetCode_88_7.cs @@ -0,0 +1,19 @@ +/* + * @lc app=leetcode.cn id=88 lang=csharp + * + * [88] 合并两个有序数组 + */ +using System; + +public class Solution { + public void Merge (int[] nums1, int m, int[] nums2, int n) { + int p = m + n - 1, p1 = m - 1, p2 = n - 1; + while (p1 >= 0 && p2 >= 0) { + nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--]; + } + + if (p2 >= 0) { + Array.Copy (nums2, 0, nums1, 0, p2 + 1); + } + } +} \ No newline at end of file