From 0353954030406dbebd8d2f0f9b7f8a83a328701b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=8B=A5=E6=9C=8B?= Date: Sun, 16 Jun 2019 21:57:44 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_7/LeetCode_1_7.cs | 24 ++++++++++++ Week_02/id_7/LeetCode_242_7.cs | 28 ++++++++++++++ Week_02/id_7/LeetCode_3_7.cs | 24 ++++++++++++ Week_02/id_7/LeetCode_726_7.cs | 71 ++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 Week_02/id_7/LeetCode_1_7.cs create mode 100644 Week_02/id_7/LeetCode_242_7.cs create mode 100644 Week_02/id_7/LeetCode_3_7.cs create mode 100644 Week_02/id_7/LeetCode_726_7.cs diff --git a/Week_02/id_7/LeetCode_1_7.cs b/Week_02/id_7/LeetCode_1_7.cs new file mode 100644 index 00000000..d6a87b3a --- /dev/null +++ b/Week_02/id_7/LeetCode_1_7.cs @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=1 lang=csharp + * + * [1] 两数之和 + */ + +using System.Collections.Generic; + +public class Solution { + public int[] TwoSum (int[] nums, int target) { + Dictionary dicNums = new Dictionary (); + for (int i = 0; i < nums.Length; i++) { + if (!dicNums.ContainsKey (target - nums[i])) { + if (!dicNums.ContainsKey (nums[i])) { + dicNums.Add (nums[i], i); + } + } else { + return new int[] { dicNums[target - nums[i]], i }; + } + } + + return null; + } +} \ No newline at end of file diff --git a/Week_02/id_7/LeetCode_242_7.cs b/Week_02/id_7/LeetCode_242_7.cs new file mode 100644 index 00000000..cfe573bf --- /dev/null +++ b/Week_02/id_7/LeetCode_242_7.cs @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=242 lang=csharp + * + * [242] 有效的字母异位词 + */ +using System.Collections.Generic; + +public class Solution { + public bool IsAnagram (string s, string t) { + if (s.Length != t.Length) { + return false; + } + + int[] arrRes = new int[26]; + for (int i = 0; i < s.Length; i++) { + arrRes[s[i] - 'a']++; + arrRes[t[i] - 'a']--; + } + + for (int i = 0; i < 26; i++) { + if (arrRes[i] != 0) { + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/Week_02/id_7/LeetCode_3_7.cs b/Week_02/id_7/LeetCode_3_7.cs new file mode 100644 index 00000000..e2bb64cd --- /dev/null +++ b/Week_02/id_7/LeetCode_3_7.cs @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=3 lang=csharp + * + * [3] 无重复字符的最长子串 + */ +using System; +using System.Collections.Generic; + +public class Solution { + public int LengthOfLongestSubstring (string s) { + Dictionary dic = new Dictionary (); + int ans = 0; + for (int left = 0, right = 0; right < s.Length; right++) { + if (dic.ContainsKey (s[right])) { + left = Math.Max (left, dic[s[right]]); + dic[s[right]] = right + 1; + } else { + dic.Add (s[right], right + 1); + } + ans = Math.Max (ans, right - left + 1); + } + return ans; + } +} \ No newline at end of file diff --git a/Week_02/id_7/LeetCode_726_7.cs b/Week_02/id_7/LeetCode_726_7.cs new file mode 100644 index 00000000..2313119e --- /dev/null +++ b/Week_02/id_7/LeetCode_726_7.cs @@ -0,0 +1,71 @@ +/* + * @lc app=leetcode.cn id=726 lang=csharp + * + * [726] 原子的数量 + * a-z:97-122 + * A-Z:65-90 + * 0-9:48-57 + */ +using System; +using System.Collections; +using System.Collections.Generic; + +public class Solution { + public string CountOfAtoms (string formula) { + Stack> skRes = new Stack> (); + skRes.Push (new Dictionary ()); + int i = 0; + while (i < formula.Length) { + if (formula[i].Equals ('(')) { + skRes.Push (new Dictionary ()); + i++; + } else if (formula[i].Equals (')')) { + int start = ++i; + while (i < formula.Length && formula[i] >= 48 && formula[i] <= 57) i++; + int multiplicity = i > start? Convert.ToInt32 (formula.Substring (start, i - start)) : 1; + if (multiplicity != 1) { + Dictionary dicTemp = skRes.Pop (); + Dictionary dicTemp2 = skRes.Peek (); + foreach (var name in dicTemp.Keys) { + if (dicTemp2.ContainsKey (name)) { + dicTemp2[name] += dicTemp[name] * multiplicity; + } else { + dicTemp2.Add (name, dicTemp[name] * multiplicity); + } + } + } + } else { + int start = i++; + while (i < formula.Length && formula[i] >= 97 && formula[i] <= 122) i++; + string name = formula.Substring (start, i - start); + + start = i; + while (i < formula.Length && formula[i] >= 48 && formula[i] <= 57) i++; + int multiplicity = i > start? Convert.ToInt32 (formula.Substring (start, i - start)) : 1; + + Dictionary dicTemp = skRes.Peek (); + if (dicTemp.ContainsKey (name)) { + dicTemp[name] = dicTemp[name] + multiplicity; + } else { + dicTemp.Add (name, multiplicity); + } + } + } + + string strRes = ""; + Dictionary dicRes = skRes.Peek (); + List lst = new List (); + foreach (var name in dicRes.Keys) { + lst.Add (name); + } + lst.Sort (); + for (int k = 0; k < lst.Count; k++) { + if (dicRes[lst[k]] > 1) + strRes += (lst[k] + dicRes[lst[k]]); + else + strRes += (lst[k] + ""); + } + + return strRes; + } +} \ No newline at end of file From 722c3eb6ee991e83f7d103fb5afbeb5cd06f5a0f Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 23 Jun 2019 23:16:51 +0800 Subject: [PATCH 2/5] =?UTF-8?q?200.=20=E5=B2=9B=E5=B1=BF=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_03/id_7/LeetCode_200_7.cs | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Week_03/id_7/LeetCode_200_7.cs diff --git a/Week_03/id_7/LeetCode_200_7.cs b/Week_03/id_7/LeetCode_200_7.cs new file mode 100644 index 00000000..169c8462 --- /dev/null +++ b/Week_03/id_7/LeetCode_200_7.cs @@ -0,0 +1,71 @@ +/* + * @lc app=leetcode.cn id=200 lang=csharp + * + * [200] 岛屿数量 + */ +using System.Collections; +using System.Collections.Generic; + +public class Solution { + // 解法1:DFS + // char[][] m_g; + // public int NumIslands (char[][] grid) { + // int nLands = 0; + // m_g = grid; + // for (int i = 0; i < m_g.Length; i++) { + // for (int j = 0; j < m_g[i].Length; j++) { + // if (m_g[i][j].Equals ('1')) + // nLands += sink (i, j); + // } + // } + // return nLands; + // } + // public int sink (int i, int j) { + // if (i < 0 || i >= m_g.Length || j < 0 || j >= m_g[i].Length) { + // return 0; + // } + // if (m_g[i][j].Equals ('0')) { + // return 0; + // } + // m_g[i][j] = '0'; + // sink (i + 1, j); + // sink (i - 1, j); + // sink (i, j + 1); + // sink (i, j - 1); + // return 1; + // } + + // 解法2:BFS + int[] dx = new int[] {-1, 1, 0, 0 }; + int[] dy = new int[] { 0, 0, -1, 1 }; + public int NumIslands (char[][] grid) { + int nLands = 0; + + for (int i = 0; i < grid.Length; i++) { + for (int j = 0; j < grid[i].Length; j++) { + if (grid[i][j] == '0') { + continue; + } + + nLands++; + Queue que = new Queue (); + que.Enqueue (new KeyValuePair (i, j)); + while (que.Count > 0) { + KeyValuePair kvp = (KeyValuePair) que.Dequeue (); + if (grid[kvp.Key][kvp.Value] == '0') { + continue; + } + grid[kvp.Key][kvp.Value] = '0'; + for (int k = 0; k < dx.Length; k++) { + int x = kvp.Key + dx[k], y = kvp.Value + dy[k]; + if (x >= 0 && x < grid.Length && y >= 0 && y < grid[kvp.Key].Length && grid[x][y] == '1') { + que.Enqueue (new KeyValuePair (x, y)); + } + } + } + } + } + + return nLands; + } +} \ No newline at end of file From 35bd66fb404ce8d97af0a7345f575e781ddc961c Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Mon, 24 Jun 2019 00:20:22 +0800 Subject: [PATCH 3/5] =?UTF-8?q?[703]=20=E6=95=B0=E6=8D=AE=E6=B5=81?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E7=AC=ACK=E5=A4=A7=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4k\345\244\247\345\205\203\347\264\240.cs" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "Week_03/id_7/703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254k\345\244\247\345\205\203\347\264\240.cs" diff --git "a/Week_03/id_7/703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254k\345\244\247\345\205\203\347\264\240.cs" "b/Week_03/id_7/703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254k\345\244\247\345\205\203\347\264\240.cs" new file mode 100644 index 00000000..54b1c54a --- /dev/null +++ "b/Week_03/id_7/703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254k\345\244\247\345\205\203\347\264\240.cs" @@ -0,0 +1,58 @@ +/* + * @lc app=leetcode.cn id=703 lang=csharp + * + * [703] 数据流中的第K大元素 + */ +using System; + +public class KthLargest { + int[] heap; + int count; + int capacity; + + public KthLargest (int k, int[] nums) { + heap = new int[nums.Length]; + capacity = k; + count = 0; + for (int i = 0; i < nums.Length; i++) { + Add (nums[i]); + } + } + + public int Add (int val) { + if (count < capacity) { + heap[count++] = val; + int i = count - 1; + while (i > 0) { + if (heap[i] < heap[(i - 1) / 2]) { + int temp = heap[i]; + heap[i] = heap[(i - 1) / 2]; + heap[(i - 1) / 2] = temp; + } + i = (i - 1) / 2; + } + } else if (val > heap[0]) { + heap[0] = val; + int i = 0; + while (i * 2 + 1 < count) { + i = i * 2 + 1; + if (i + 1 < count && heap[i] > heap[i + 1]) { + i++; + } + if (heap[(i - 1) / 2] > heap[i]) { + int temp = heap[(i - 1) / 2]; + heap[(i - 1) / 2] = heap[i]; + heap[i] = temp; + } + } + } + + return heap[0]; + } +} + +/** + * Your KthLargest object will be instantiated and called as such: + * KthLargest obj = new KthLargest(k, nums); + * int param_1 = obj.Add(val); + */ \ No newline at end of file From ec6e6bac98b42c7644762f48bfb34f5254bd016d Mon Sep 17 00:00:00 2001 From: Yrp1994 <39095997+Yrp1994@users.noreply.github.com> Date: Mon, 24 Jun 2019 00:37:52 +0800 Subject: [PATCH 4/5] test --- Week_03/id_7/NOTE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Week_03/id_7/NOTE.md b/Week_03/id_7/NOTE.md index 107ea7d6..090285c3 100644 --- a/Week_03/id_7/NOTE.md +++ b/Week_03/id_7/NOTE.md @@ -1 +1,2 @@ # 学习笔记 +test From 8d8b7d67ad357922b305700bfbc980653e69d016 Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Mon, 24 Jun 2019 00:55:10 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=EF=BC=8Cleetcod?= =?UTF-8?q?e=20101=E3=80=81111=E3=80=81783?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_7/LeetCode_101_7.cs | 39 +++++++++++++++++++++++++++++ Week_02/id_7/LeetCode_111_7.cs | 45 ++++++++++++++++++++++++++++++++++ Week_02/id_7/LeetCode_783_7.cs | 30 +++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 Week_02/id_7/LeetCode_101_7.cs create mode 100644 Week_02/id_7/LeetCode_111_7.cs create mode 100644 Week_02/id_7/LeetCode_783_7.cs diff --git a/Week_02/id_7/LeetCode_101_7.cs b/Week_02/id_7/LeetCode_101_7.cs new file mode 100644 index 00000000..5f7b20ce --- /dev/null +++ b/Week_02/id_7/LeetCode_101_7.cs @@ -0,0 +1,39 @@ +/* + * @lc app=leetcode.cn id=101 lang=csharp + * + * [101] 对称二叉树 + */ + +using System.Collections; +/** + * 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 { + // 按层遍历,按顺序:左子树左节点、右子树右节点、左子树右节点、右子树左节点,进队列,然后每次出队列两个,比较是否相等,遇不等就直接返回false + public bool IsSymmetric (TreeNode root) { + Queue que = new Queue (); + que.Enqueue (root); + que.Enqueue (root); + while (que.Count > 0) { + TreeNode nodeL = (TreeNode) que.Dequeue (); + TreeNode nodeR = (TreeNode) que.Dequeue (); + if (nodeL == null && nodeR == null) + continue; + if (nodeL == null || nodeR == null) + return false; + if (nodeL.val != nodeR.val) + return false; + que.Enqueue (nodeL.left); + que.Enqueue (nodeR.right); + que.Enqueue (nodeL.right); + que.Enqueue (nodeR.left); + } + return true; + } +} \ No newline at end of file diff --git a/Week_02/id_7/LeetCode_111_7.cs b/Week_02/id_7/LeetCode_111_7.cs new file mode 100644 index 00000000..a20ea6fd --- /dev/null +++ b/Week_02/id_7/LeetCode_111_7.cs @@ -0,0 +1,45 @@ +/* + * @lc app=leetcode.cn id=111 lang=csharp + * + * [111] 二叉树的最小深度 + */ + +using System; +using System.Collections; +using System.Collections.Generic; +/** + * 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 MinDepth (TreeNode root) { + Stack sk = new Stack (); + if (root == null) { + return 0; + } else { + sk.Push (new KeyValuePair (root, 1)); + } + int nDepth = int.MaxValue; + while (sk.Count > 0) { + KeyValuePair kvp = (KeyValuePair) sk.Pop (); + root = kvp.Key; + int nTemp = kvp.Value; + if (root.left == null && root.right == null) { + nDepth = Math.Min (nDepth, nTemp); + } + if (root.left != null) { + sk.Push (new KeyValuePair (root.left, nTemp + 1)); + } + if (root.right != null) { + sk.Push (new KeyValuePair (root.right, nTemp + 1)); + } + } + return nDepth; + } +} \ No newline at end of file diff --git a/Week_02/id_7/LeetCode_783_7.cs b/Week_02/id_7/LeetCode_783_7.cs new file mode 100644 index 00000000..0f28b662 --- /dev/null +++ b/Week_02/id_7/LeetCode_783_7.cs @@ -0,0 +1,30 @@ +/* + * @lc app=leetcode.cn id=783 lang=csharp + * + * [783] 二叉搜索树结点最小距离 + */ + +using System; +/** + * 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 { + int m_nRsult = int.MaxValue; + int m_nLastv = -999999; + public int MinDiffInBST (TreeNode root) { + if (root.left != null) + MinDiffInBST (root.left); + m_nRsult = Math.Min (m_nRsult, root.val - m_nLastv); + m_nLastv = root.val; + if (root.right != null) + MinDiffInBST (root.right); + + return m_nRsult; + } +} \ No newline at end of file