├── .gitignore
├── LICENSE
├── LeetCode-Python3
├── 434. countSegments.py
├── 509. fib.py
├── 686. repeatedStringMatch.py
└── 905. sortArrayByParity.py
├── LeetCode.sln
├── LeetCode
├── App.config
├── Easy.cs
├── Hard.cs
├── LeetCode.csproj
├── Medium.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── Sudoku
│ └── Sudoku.cs
└── Tools.cs
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .vs/
2 | LeetCode/bin/
3 | LeetCode/obj/
4 | .idea
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Yao Yilin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/LeetCode-Python3/434. countSegments.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # 434. 字符串中的单词数
4 | # https://leetcode-cn.com/problems/number-of-segments-in-a-string/
5 |
6 | '''
7 | 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
8 |
9 | 请注意,你可以假定字符串里不包括任何不可打印的字符。
10 |
11 | 示例:
12 |
13 | 输入: "Hello, my name is John"
14 | 输出: 5
15 | '''
16 |
17 | class Solution:
18 | def countSegments(self, s):
19 | """
20 | :type s: str
21 | :rtype: int
22 | """
23 | i = 0
24 | f = False
25 | s = s.strip()
26 | if s == '':
27 | return 0
28 |
29 | for c in s:
30 | if c == ' ':
31 | f = True
32 | elif f:
33 | i += 1
34 | f = False
35 |
36 | return i + 1
37 |
--------------------------------------------------------------------------------
/LeetCode-Python3/509. fib.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # 509. 斐波那契数
4 | # https://leetcode-cn.com/problems/fibonacci-number/
5 |
6 | '''
7 | 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
8 |
9 | F(0) = 0, F(1) = 1
10 | F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
11 | 给定 N,计算 F(N)。
12 |
13 | 示例 1:
14 |
15 | 输入:2
16 | 输出:1
17 | 解释:F(2) = F(1) + F(0) = 1 + 0 = 1.
18 | 示例 2:
19 |
20 | 输入:3
21 | 输出:2
22 | 解释:F(3) = F(2) + F(1) = 1 + 1 = 2.
23 | 示例 3:
24 |
25 | 输入:4
26 | 输出:3
27 | 解释:F(4) = F(3) + F(2) = 2 + 1 = 3.
28 |
29 |
30 | 提示:
31 |
32 | 0 ≤ N ≤ 30
33 | '''
34 |
35 |
36 | class Solution:
37 | def fib(self, N):
38 | """
39 | :type N: int
40 | :rtype: int
41 | """
42 | if (N <= 1):
43 | return N
44 | else:
45 | return self.fib(N - 1) + self.fib(N - 2)
46 |
--------------------------------------------------------------------------------
/LeetCode-Python3/686. repeatedStringMatch.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # 686. 重复叠加字符串匹配
4 | # https://leetcode-cn.com/problems/repeated-string-match/
5 |
6 | '''
7 | 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1。
8 |
9 | 举个例子,A = "abcd",B = "cdabcdab"。
10 |
11 | 答案为 3, 因为 A 重复叠加三遍后为 “abcdabcdabcd”,此时 B 是其子串;A 重复叠加两遍后为"abcdabcd",B 并不是其子串。
12 |
13 | 注意:
14 |
15 | A 与 B 字符串的长度在1和10000区间范围内。
16 | '''
17 |
18 | class Solution:
19 | def repeatedStringMatch(self, A, B):
20 | """
21 | :type A: str
22 | :type B: str
23 | :rtype: int
24 | """
25 |
26 | i = 1
27 | a = A
28 | maxLen = 2 * len(A) + len(B)
29 | while len(a) < maxLen:
30 | if (B in a):
31 | return i
32 | else:
33 | i = i + 1
34 | a += A
35 |
36 | return -1
37 |
--------------------------------------------------------------------------------
/LeetCode-Python3/905. sortArrayByParity.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # 905. 按奇偶排序数组
4 | # https://leetcode-cn.com/problems/sort-array-by-parity/
5 |
6 | '''
7 | 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。
8 |
9 | 你可以返回满足此条件的任何数组作为答案。
10 |
11 | 示例:
12 |
13 | 输入:[3,1,2,4]
14 | 输出:[2,4,3,1]
15 | 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。
16 |
17 | 提示:
18 |
19 | 1 <= A.length <= 5000
20 | 0 <= A[i] <= 5000
21 | '''
22 |
23 | class Solution:
24 | def sortArrayByParity(self, A):
25 | """
26 | :type A: List[int]
27 | :rtype: List[int]
28 | """
29 | i = 0
30 | r = len(A) - 1
31 | while (i < r):
32 | t = A[i]
33 | if (t % 2 != 0):
34 | A[i] = A[r]
35 | A[r] = t
36 | r = r - 1
37 | else:
38 | i = i + 1
39 | return A
40 |
--------------------------------------------------------------------------------
/LeetCode.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCode", "LeetCode\LeetCode.csproj", "{4CAB5BB0-5CDF-4BD8-858B-5EB1B48B6FA9}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {4CAB5BB0-5CDF-4BD8-858B-5EB1B48B6FA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {4CAB5BB0-5CDF-4BD8-858B-5EB1B48B6FA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {4CAB5BB0-5CDF-4BD8-858B-5EB1B48B6FA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {4CAB5BB0-5CDF-4BD8-858B-5EB1B48B6FA9}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/LeetCode/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LeetCode/Easy.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace LeetCode
8 | {
9 | public static class Easy
10 | {
11 | //https://leetcode-cn.com/problems/two-sum/description/
12 | #region 1. 两数之和
13 | public static int[] TwoSum(int[] nums, int target)
14 | {
15 | int[] arr = new int[2];
16 | for (int i = 0; i < nums.Length; i++)
17 | {
18 | for (int j = i + 1; j < nums.Length; j++)
19 | {
20 | if (nums[j] + nums[i] == target)
21 | {
22 | arr[0] = i;
23 | arr[1] = j;
24 | break;
25 | }
26 | }
27 | }
28 | return arr;
29 | }
30 | #endregion
31 |
32 | //https://leetcode-cn.com/problems/reverse-integer/description/
33 | #region 7. 颠倒整数
34 | public static int Reverse(int x)
35 | {
36 | int result = 0;
37 |
38 | while (x != 0)
39 | {
40 | int tail = x % 10;
41 | int newResult = result * 10 + tail;
42 | if ((newResult - tail) / 10 != result)
43 | return 0;
44 |
45 | result = newResult;
46 | x = x / 10;
47 | }
48 |
49 | return result;
50 | }
51 | #endregion
52 |
53 | //https://leetcode.cn/problems/palindrome-number/
54 | #region 9. 回文数
55 | public static bool IsPalindrome(int x)
56 | {
57 | if (x < 0)
58 | {
59 | return false;
60 | }
61 |
62 | int cur = 0;
63 | int num = x;
64 | while(num != 0)
65 | {
66 | cur = cur * 10 + num % 10;
67 | num /= 10;
68 | }
69 | return cur == x;
70 | }
71 | #endregion
72 |
73 | //https://leetcode.cn/problems/longest-common-prefix/
74 |
75 | #region 14. 最长公共前缀
76 | public static string LongestCommonPrefix(string[] strs)
77 | {
78 | if (strs == null || strs.Length <= 0)
79 | {
80 | return "";
81 | }
82 |
83 | if (strs.Length == 1)
84 | {
85 | return strs[0];
86 | }
87 |
88 | List res = new List();
89 | string str = strs[0];
90 | for (int i = 0; i < str.Length; i++)
91 | {
92 | char c = str[i];
93 | for (int j = 1; j < strs.Length; j++)
94 | {
95 | if (strs[j].Length > i)
96 | {
97 | char cx = strs[j][i];
98 | if (c != cx)
99 | {
100 | return new string(res.ToArray());
101 | }
102 | }
103 | else
104 | {
105 | return new string(res.ToArray());
106 | }
107 | }
108 | res.Add(c);
109 | }
110 |
111 | return new string(res.ToArray());
112 | }
113 | #endregion
114 |
115 | // https://leetcode.cn/problems/roman-to-integer/
116 | #region 13. 罗马数字转整数
117 | public static int RomanToInt(string s)
118 | {
119 | int res = 0;
120 | Dictionary map = new Dictionary()
121 | {
122 | {'I', 1},
123 | {'V', 5},
124 | {'X', 10},
125 | {'L', 50},
126 | {'C', 100},
127 | {'D', 500},
128 | {'M', 1000},
129 | };
130 | int length = s.Length;
131 | for (int i = 0; i < length; i++)
132 | {
133 | char c = s[i];
134 | int value = map[c];
135 | if (i < length - 1 && value < map[s[i + 1]])
136 | {
137 | res -= value;
138 | }
139 | else
140 | {
141 | res += value;
142 | }
143 | }
144 | return res;
145 | }
146 | #endregion
147 | //https://leetcode-cn.com/problems/valid-parentheses/description/
148 | #region 20. 有效的括号
149 | public static bool IsValid(string s)
150 | {
151 | Stack stack = new Stack();
152 |
153 | for (int i = 0; i < s.Length; i++)
154 | {
155 | char c = s[i];
156 | if (c == '(')
157 | stack.Push(')');
158 | else if (c == '{')
159 | stack.Push('}');
160 | else if (c == '[')
161 | stack.Push(']');
162 | else if (stack.Count == 0 || stack.Pop() != c)
163 | return false;
164 | }
165 |
166 | return stack.Count == 0;
167 | }
168 | #endregion
169 |
170 | //https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
171 | #region 21. 合并两个有序链表
172 | public static ListNode MergeTwoLists(ListNode l1, ListNode l2)
173 | {
174 | if (l1 == null)
175 | {
176 | return l2;
177 | }
178 |
179 | if (l2 == null)
180 | {
181 | return l1;
182 | }
183 |
184 | if (l1.val < l2.val)
185 | {
186 | l1.next = MergeTwoLists(l1.next, l2);
187 | return l1;
188 | }
189 | else
190 | {
191 | l2.next = MergeTwoLists(l2.next, l1);
192 | return l2;
193 | }
194 | }
195 |
196 | public static ListNode MergeTwoLists_2(ListNode l1, ListNode l2)
197 | {
198 | ListNode dummy = new ListNode(0);
199 | ListNode tail = dummy;
200 | while (l1 != null && l2 != null)
201 | {
202 | if (l1.val < l2.val)
203 | {
204 | tail.next = l1;
205 | l1 = l1.next;
206 | }
207 | else
208 | {
209 | tail.next = l2;
210 | l2 = l2.next;
211 | }
212 | tail = tail.next;
213 | }
214 | tail.next = l1 != null ? l1 : l2;
215 | return dummy.next;
216 | }
217 | #endregion
218 |
219 | //https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/
220 | #region 26. 删除排序数组中的重复项
221 | public static int RemoveDuplicates(int[] nums)
222 | {
223 | if (nums.Length < 2)
224 | return nums.Length;
225 |
226 | int index = 0;
227 | int v = nums[0];
228 | for (int i = 1; i < nums.Length; i++)
229 | {
230 | if (nums[i] != v)
231 | {
232 | index++;
233 | v = nums[i];
234 | nums[index] = v;
235 | }
236 | }
237 |
238 | return index + 1;
239 | }
240 | #endregion
241 |
242 | //https://leetcode-cn.com/problems/remove-element/description/
243 | #region 27. 移除元素
244 | public static int RemoveElement(int[] nums, int val)
245 | {
246 | int len = nums.Length;
247 | int i = 0;
248 | while (i != len)
249 | {
250 | if (nums[i] == val)
251 | {
252 | nums[i] = nums[len - 1];
253 | len--;
254 | }
255 | else
256 | {
257 | i++;
258 | }
259 | }
260 |
261 | return len;
262 | }
263 | #endregion
264 |
265 | //https://leetcode-cn.com/problems/implement-strstr/
266 | #region 28. 实现 strStr()
267 | public static int StrStr(string haystack, string needle)
268 | {
269 | if (haystack.Length < needle.Length)
270 | {
271 | return -1;
272 | }
273 | if (string.IsNullOrEmpty(needle))
274 | {
275 | return 0;
276 | }
277 | char n = needle[0];
278 | for (int i = 0; i < haystack.Length - needle.Length + 1; i++)
279 | {
280 | char c = haystack[i];
281 | if (c == n)
282 | {
283 | bool isFound = true;
284 | for (int j = 1; j < needle.Length; j++)
285 | {
286 | if (haystack[i + j] != needle[j])
287 | {
288 | isFound = false;
289 | break;
290 | }
291 | }
292 | if (isFound)
293 | {
294 | return i;
295 | }
296 | }
297 | }
298 | return -1;
299 | }
300 | #endregion
301 |
302 | //https://leetcode-cn.com/problems/search-insert-position/description/
303 | #region 35. 搜索插入位置
304 | public static int SearchInsert(int[] nums, int target)
305 | {
306 | for (int i = 0; i < nums.Length; i++)
307 | {
308 | int v = nums[i];
309 | if (v >= target)
310 | return i;
311 | }
312 |
313 | return nums.Length;
314 | }
315 | #endregion
316 |
317 | //https://leetcode-cn.com/problems/length-of-last-word/description/
318 | #region 58. 最后一个单词的长度
319 | public static int LengthOfLastWord(string s)
320 | {
321 | if (string.IsNullOrWhiteSpace(s))
322 | return 0;
323 |
324 | string str = s.TrimEnd();
325 | int i = str.Length - 1;
326 | int l = 0;
327 | while (i >= 0)
328 | {
329 | if (s[i--] == ' ')
330 | break;
331 | l++;
332 | }
333 | return l;
334 | }
335 | #endregion
336 |
337 | //https://leetcode-cn.com/problems/plus-one/description/
338 | #region 66. 加一
339 | public static int[] PlusOne(int[] digits)
340 | {
341 | int carry = 1;
342 | LinkedList res = new LinkedList();
343 |
344 | for (int i = digits.Length - 1; i >= 0; i--)
345 | {
346 | int sum = digits[i] + carry;
347 | res.AddFirst(sum % 10);
348 | carry = sum / 10;
349 | }
350 |
351 | if (carry > 0)
352 | res.AddFirst(carry);
353 |
354 | return res.ToArray();
355 | }
356 | #endregion
357 |
358 | //https://leetcode-cn.com/problems/add-binary/description/
359 | #region 67. 二进制求和
360 | public static string AddBinary(string a, string b)
361 | {
362 | int i = a.Length - 1;
363 | int j = b.Length - 1;
364 | char carry = '0';
365 | LinkedList list = new LinkedList();
366 | while (i >= 0 || j >= 0)
367 | {
368 | char res = carry;
369 | char ca = i >= 0 ? a[i] : '0';
370 | char cb = j >= 0 ? b[j] : '0';
371 | res ^= ca;
372 | res ^= cb;
373 |
374 | if (ca == cb)
375 | carry = ca;
376 | else
377 | carry = res == '0' ? '1' : '0';
378 |
379 | list.AddFirst(res);
380 | i--;
381 | j--;
382 | }
383 |
384 | if (carry == '1')
385 | list.AddFirst(carry);
386 |
387 | return new string(list.ToArray());
388 | }
389 | #endregion
390 |
391 | //https://leetcode-cn.com/problems/sqrtx/description/
392 | #region 69. x 的平方根
393 | public static int MySqrt(int x)
394 | {
395 | int m = 0x40000000, y = 0, b;
396 | while (m != 0)
397 | {
398 | b = y | m;
399 | y >>= 1;
400 | if (x >= b)
401 | {
402 | x = x - b;
403 | y |= m;
404 | }
405 | m >>= 2;
406 | }
407 | return y;
408 | //if(x == 0)
409 | // return 0;
410 |
411 | //int left = 0;
412 | //int right = x;
413 | //while(true)
414 | //{
415 | // int middle = (right + left) / 2;
416 | // if(middle == x / middle)
417 | // {
418 | // return middle;
419 | // }
420 |
421 | // if(middle > x / middle)
422 | // {
423 | // right = middle - 1;
424 | // }
425 | // else
426 | // {
427 | // if(middle + 1 > x / (middle + 1))
428 | // {
429 | // return middle;
430 | // }
431 | // left = middle + 1;
432 | // }
433 | //}
434 | }
435 | #endregion
436 |
437 | //https://leetcode.cn/problems/climbing-stairs/
438 | #region 70. 爬楼梯
439 | public static int ClimbStairs(int n)
440 | {
441 | if(n <= 2)
442 | {
443 | return n;
444 | }
445 | int pre = 2, prePre = 1;
446 | for (int i = 2; i < n; i++)
447 | {
448 | int current = pre + prePre;
449 | prePre = pre;
450 | pre = current;
451 | }
452 |
453 | return pre;
454 | }
455 | #endregion
456 |
457 | //https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
458 | #region 83. 删除排序链表中的重复元素
459 | public static ListNode DeleteDuplicates(ListNode head)
460 | {
461 | if (head == null)
462 | return null;
463 |
464 | var node = head;
465 | while (node.next != null)
466 | {
467 | if (node.val == node.next.val)
468 | {
469 | node.next = node.next.next;
470 | }
471 | else
472 | {
473 | node = node.next;
474 | }
475 | }
476 |
477 | return head;
478 | }
479 | #endregion
480 |
481 | //https://leetcode-cn.com/problems/merge-sorted-array/description/
482 | #region 88. 合并两个有序数组
483 | public static void Merge(int[] nums1, int m, int[] nums2, int n)
484 | {
485 | int end = m + n - 1;
486 | m--;
487 | n--;
488 | while (end >= 0)
489 | {
490 | int a = m >= 0 ? nums1[m] : int.MinValue;
491 | int b = n >= 0 ? nums2[n] : int.MinValue;
492 | if (a > b)
493 | {
494 | nums1[end--] = a;
495 | m--;
496 | }
497 | else
498 | {
499 | nums1[end--] = b;
500 | n--;
501 | }
502 | }
503 | }
504 | #endregion
505 |
506 | //https://leetcode-cn.com/problems/same-tree/description/
507 | #region 100. 相同的树
508 | public static bool IsSameTree(TreeNode p, TreeNode q)
509 | {
510 | if (p == null && q == null)
511 | {
512 | return true;
513 | }
514 |
515 | if (p == null || q == null)
516 | {
517 | return false;
518 | }
519 |
520 | if (p.val != q.val)
521 | {
522 | return false;
523 | }
524 |
525 | return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right);
526 | }
527 | #endregion
528 |
529 | //https://leetcode-cn.com/problems/symmetric-tree/description/
530 | #region 101. 对称二叉树
531 | public static bool IsSymmetric(TreeNode root)
532 | {
533 | if (root != null)
534 | return IsEqual(root.left, root.right);
535 |
536 | return true;
537 | }
538 | private static bool IsEqual(TreeNode n1, TreeNode n2)
539 | {
540 | if (n1 == null && n2 == null)
541 | return true;
542 |
543 | if (n1 != null && n2 != null)
544 | return n1.val == n2.val && IsEqual(n1.left, n2.right) && IsEqual(n1.right, n2.left);
545 |
546 | return false;
547 | }
548 |
549 | #endregion
550 |
551 | //https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
552 | #region 104. 二叉树的最大深度
553 | public static int MaxDepth(TreeNode root)
554 | {
555 | return MaxDepth(root, 0);
556 | }
557 | private static int MaxDepth(TreeNode root, int level)
558 | {
559 | if (root == null)
560 | return level;
561 | int l = Math.Max(MaxDepth(root.left, level + 1), level);
562 | int r = Math.Max(MaxDepth(root.right, level + 1), level);
563 | return Math.Max(l, r);
564 | }
565 |
566 | #endregion
567 |
568 | //https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
569 | #region 111. 二叉树的最小深度
570 | public static int MinDepth(TreeNode root)
571 | {
572 | if (root == null)
573 | return 0;
574 |
575 | int L = MinDepth(root.left), R = MinDepth(root.right);
576 | return L < R && L > 0 || R < 1 ? 1 + L : 1 + R;
577 | }
578 | #endregion
579 |
580 | //https://leetcode-cn.com/problems/pascals-triangle/description/
581 | #region 118. 帕斯卡三角形(杨辉三角)
582 | public static IList> Generate(int numRows)
583 | {
584 | IList> res = new List>();
585 | for (int i = 0; i < numRows; i++)
586 | res.Add(GetRow(i)); // No. 119
587 |
588 | return res;
589 | }
590 | #endregion
591 |
592 | //https://leetcode-cn.com/problems/pascals-triangle-ii/description/
593 | #region 119. 帕斯卡三角形(杨辉三角)II
594 | ///
595 | /// https://en.wikipedia.org/wiki/Pascal%27s_triangle
596 | /// ∵ C[n, m] = n! / (m! * (n - m)!)
597 | /// ∴ C[n, m - 1] = n! / ((m - 1)! * (n - m - 1)!)
598 | /// ∴ C[n, m] = C[n, m - 1] / (m * ( n - (m - 1)))
599 | /// thus C[n, m] = C[n, m - 1] * (n - m + 1) / m
600 | /// ∵ C[n, 0] = 1
601 | /// thus for index = 1 to row, we can get the result
602 | ///
603 | public static IList GetRow(int rowIndex)
604 | {
605 | int[] row = new int[rowIndex + 1];
606 | row[0] = 1;
607 | for (int i = 1; i <= rowIndex; i++)
608 | row[i] = (int)((long)row[i - 1] * (rowIndex - (i - 1)) / (i));
609 |
610 | return new List(row);
611 | }
612 | #endregion
613 |
614 | //https://leetcode-cn.com/problems/valid-palindrome/description/
615 | #region 125. 验证回文串
616 | public static bool IsPalindrome(string s)
617 | {
618 | int l = 0, r = s.Length - 1;
619 | while (l < r)
620 | {
621 | char c1 = default(char);
622 | while (l < s.Length)
623 | {
624 | if (char.IsLetterOrDigit(s[l]))
625 | {
626 | c1 = Tools.ToUpper(s[l]);
627 | break;
628 | }
629 | l++;
630 | }
631 | char c2 = default(char);
632 | while (r >= 0)
633 | {
634 | if (char.IsLetterOrDigit(s[r]))
635 | {
636 | c2 = Tools.ToUpper(s[r]);
637 | break;
638 | }
639 | r--;
640 | }
641 | if (c1 != c2)
642 | return false;
643 |
644 | l++;
645 | r--;
646 | }
647 | return true;
648 | }
649 |
650 | ///
651 | /// C# source code is an array of ascii type enum, so is faster than below.
652 | /// char.IsLetterOrDigit(char c)
653 | ///
654 | ///
655 | ///
656 | [Obsolete("Use char.IsLetterOrDigit(char c) instead")]
657 | private static bool IsLetterOrDigit(char c)
658 | {
659 | if (c >= '0' && c <= '9')
660 | return true;
661 | if (c >= 'A' && c <= 'Z')
662 | return true;
663 | if (c >= 'a' && c <= 'z')
664 | return true;
665 | return false;
666 | }
667 |
668 | #endregion
669 |
670 | //https://leetcode-cn.com/problems/single-number/description/
671 | #region 136. 只出现一次的数字
672 | public static int SingleNumber(int[] nums)
673 | {
674 | int res = nums[0];
675 | for (int i = 1; i < nums.Length; i++)
676 | {
677 | res ^= nums[i];
678 | }
679 | return res;
680 | }
681 | #endregion
682 |
683 | //https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/
684 | #region 160. 相交链表
685 | public static ListNode GetIntersectionNode(ListNode headA, ListNode headB)
686 | {
687 | if (headA == null || headB == null)
688 | return null;
689 |
690 | ListNode a = headA, b = headB;
691 |
692 | while (a != b)
693 | {
694 | a = a == null ? headB : a.next;
695 | b = b == null ? headA : b.next;
696 | }
697 | return a;
698 | }
699 | #endregion
700 |
701 | //https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/description/
702 | #region 167. 两数之和 II - 输入有序数组
703 | public static int[] TwoSum167(int[] numbers, int target)
704 | {
705 | int[] res = new int[2];
706 |
707 | if (numbers == null || numbers.Length < 2)
708 | return res;
709 |
710 | int l = 0, r = numbers.Length - 1;
711 |
712 | while (l < r)
713 | {
714 | int lv = numbers[l];
715 | int rv = numbers[r];
716 | int sum = lv + rv;
717 | if (sum == target)
718 | {
719 | res[0] = l + 1;
720 | res[1] = r + 1;
721 | break;
722 | }
723 | else if (sum > target)
724 | r--;
725 | else
726 | l++;
727 | }
728 |
729 | return res;
730 | }
731 | #endregion
732 |
733 | //https://leetcode-cn.com/problems/excel-sheet-column-title/description/
734 | #region 168. Excel表列名称
735 | public static string ConvertToTitle(int n)
736 | {
737 | LinkedList chars = new LinkedList();
738 | while (true)
739 | {
740 | chars.AddFirst((char)(--n % 26 + 'A'));
741 | n /= 26;
742 | if (n <= 0)
743 | break;
744 | }
745 | return new string(chars.ToArray());
746 | }
747 | #endregion
748 |
749 | //https://leetcode-cn.com/problems/excel-sheet-column-number/description/
750 | #region 171. Excel表列序号
751 | public static int TitleToNumber(string s)
752 | {
753 | int res = 0;
754 | for (int i = 0; i < s.Length; i++)
755 | res += (ParseExcelTitleChar(s[i]) * (int)Math.Pow(26, s.Length - i - 1));
756 |
757 | return res;
758 | }
759 | private static int ParseExcelTitleChar(char c)
760 | {
761 | if (c >= 'A' && c <= 'Z')
762 | {
763 | return c - 'A' + 1;
764 | }
765 | return 0;
766 | }
767 | #endregion
768 |
769 | //https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/
770 | #region 172. 阶乘后的零
771 | ///
772 | /// while 0 < n < 5 , f(n!) = 0;
773 | /// while n >= 5,f(n!) = k + f(k!), there k = n / 5
774 | ///
775 | ///
776 | ///
777 | public static int TrailingZeroes(int n)
778 | {
779 | if (n < 5)
780 | return 0;
781 | return n / 5 + TrailingZeroes(n / 5);
782 | }
783 | #endregion
784 |
785 | //https://leetcode-cn.com/problems/rotate-array/description/
786 | #region 189. 旋转数组
787 | public static void Rotate(int[] nums, int k)
788 | {
789 | k = k % nums.Length;
790 | Array.Reverse(nums);
791 |
792 | Array.Reverse(nums, 0, k);
793 | Array.Reverse(nums, k, nums.Length - k);
794 | }
795 | #endregion
796 |
797 | //https://leetcode-cn.com/problems/reverse-bits/description/
798 | #region 190. 颠倒二进制位
799 | public static uint ReverseBits(uint n)
800 | {
801 | //uint[] bits = new uint[32];
802 | //uint num = n;
803 | //int i = 0;
804 | //while(num > 0)
805 | //{
806 | // uint r = num % 2;
807 | // bits[i++] = r;
808 | // num /= 2;
809 | //}
810 | //uint res = 0;
811 |
812 | //uint p = 1;
813 | //for(int j = bits.Length - 1; j >= 0; j--)
814 | //{
815 | // res += (bits[j] * p);
816 | // p *= 2;
817 | //}
818 |
819 | //return res;
820 |
821 | uint m = 0;
822 | for (int i = 0; i < 32; i++)
823 | {
824 | m <<= 1;
825 | m |= n & 1;
826 | n >>= 1;
827 | }
828 | return m;
829 | }
830 | #endregion
831 |
832 | //https://leetcode-cn.com/problems/number-of-1-bits/description/
833 | #region 191. 位1的个数
834 | public static int HammingWeight(uint n)
835 | {
836 | int c = 0;
837 | while (n > 0)
838 | {
839 | n &= (n - 1);
840 | c++;
841 | }
842 | return c;
843 | }
844 | #endregion
845 |
846 | //https://leetcode-cn.com/problems/happy-number
847 | #region 202. 快乐数
848 | public static bool IsHappy(int n)
849 | {
850 | return IsHappy(n, new HashSet());
851 | }
852 | private static bool IsHappy(int n, HashSet set)
853 | {
854 | if (n == 1)
855 | return true;
856 |
857 | if (set.Contains(n))
858 | return false;
859 | else
860 | set.Add(n);
861 |
862 | int sum = 0;
863 | while (n > 0)
864 | {
865 | int r = n % 10;
866 | sum += (r * r);
867 | n /= 10;
868 | }
869 |
870 | return IsHappy(sum, set);
871 | }
872 | #endregion
873 |
874 | //https://leetcode-cn.com/problems/remove-linked-list-elements/description/
875 | #region 203. 删除链表中的节点
876 | public static ListNode RemoveElements(ListNode head, int val)
877 | {
878 | ListNode dummy = head;
879 | while (dummy != null)
880 | {
881 | ListNode next = dummy.next;
882 | if (next != null && next.val == val)
883 | dummy.next = next.next;
884 | else
885 | dummy = dummy.next;
886 | }
887 |
888 | if (head != null && head.val == val)
889 | head = head.next;
890 |
891 | return head;
892 | }
893 | #endregion
894 |
895 | //https://leetcode-cn.com/problems/count-primes/description/
896 | #region 204. 计数质数
897 | ///
898 | /// idea copy from https://leetcode.com/problems/count-primes/discuss/57588/My-simple-Java-solution
899 | ///
900 | ///
901 | ///
902 | public static int CountPrimes(int n)
903 | {
904 | bool[] notPrime = new bool[n];
905 | int count = 0;
906 | for (int i = 2; i < n; i++)
907 | {
908 | if (notPrime[i] == false)
909 | {
910 | count++;
911 | for (int j = 2; i * j < n; j++)
912 | notPrime[i * j] = true;
913 | }
914 | }
915 |
916 | return count;
917 | }
918 |
919 | #endregion
920 |
921 | //https://leetcode-cn.com/problems/isomorphic-strings/description/
922 | #region 205. 同构字符串
923 | public static bool IsIsomorphic(string s, string t)
924 | {
925 | char[] m = new char[256];
926 | char[] n = new char[256];
927 | for (int i = 0; i < s.Length; i++)
928 | {
929 | var si = s[i];
930 | var ti = t[i];
931 |
932 | if (m[si] == 0 && n[ti] == 0)
933 | {
934 | m[si] = ti;
935 | n[ti] = si;
936 | }
937 | else
938 | {
939 | if (m[si] != ti && n[ti] != si)
940 | return false;
941 | }
942 | }
943 |
944 | return true;
945 | }
946 | #endregion
947 |
948 | //https://leetcode-cn.com/problems/reverse-linked-list/description/
949 | #region 206. 反转链表
950 | public static ListNode ReverseList(ListNode head)
951 | {
952 | ListNode list = null;
953 | while (head != null)
954 | {
955 | var next = head.next;
956 | head.next = list;
957 | list = head;
958 | head = next;
959 | }
960 |
961 | return list;
962 | }
963 | #endregion
964 |
965 | //https://leetcode-cn.com/problems/contains-duplicate/description/
966 | #region 217. 存在重复元素
967 | public static bool ContainsDuplicate(int[] nums)
968 | {
969 | HashSet hash = new HashSet();
970 | foreach (var n in nums)
971 | if (!hash.Add(n))
972 | return true;
973 | return false;
974 | }
975 | #endregion
976 |
977 | //https://leetcode-cn.com/problems/contains-duplicate-ii/description/
978 | #region 219. 存在重复元素 II
979 | public static bool ContainsNearbyDuplicate(int[] nums, int k)
980 | {
981 | Dictionary map = new Dictionary();
982 | for (int i = 0; i < nums.Length; i++)
983 | {
984 | int key = nums[i];
985 | if (map.ContainsKey(key))
986 | {
987 | if (Math.Abs(map[key] - i) <= k)
988 | return true;
989 | else
990 | map[key] = i;
991 | }
992 | else
993 | map.Add(key, i);
994 | }
995 |
996 | return false;
997 | }
998 | #endregion
999 |
1000 | //https://leetcode-cn.com/problems/invert-binary-tree/description/
1001 | #region 226. 翻转二叉树
1002 | public static TreeNode InvertTree(TreeNode root)
1003 | {
1004 | if (root == null)
1005 | return null;
1006 | var tree = root.left;
1007 | root.left = root.right;
1008 | root.right = tree;
1009 | InvertTree(root.left);
1010 | InvertTree(root.right);
1011 | return root;
1012 | }
1013 | #endregion
1014 |
1015 | //https://leetcode-cn.com/problems/power-of-two/description/
1016 | #region 231. 2的幂
1017 | public static bool IsPowerOfTwo(int n)
1018 | {
1019 | //int c = 0;
1020 | //while(n > 0)
1021 | //{
1022 | // if((n & 1) == 1 && (++c > 1))
1023 | // return false;
1024 | // n >>= 1;
1025 | //}
1026 | //return c > 0;
1027 | return n > 0 && (n & (n - 1)) == 0;
1028 | }
1029 | #endregion
1030 |
1031 | //https://leetcode-cn.com/problems/delete-node-in-a-linked-list/description/
1032 | #region 237. 删除链表的结点
1033 | public static void DeleteNode(ListNode node)
1034 | {
1035 | node.val = node.next.val;
1036 | node.next = node.next.next;
1037 | }
1038 | #endregion
1039 |
1040 | //https://leetcode-cn.com/problems/binary-tree-paths/description/
1041 | #region 257. 二叉树的所有路径
1042 | public static IList BinaryTreePaths(TreeNode root)
1043 | {
1044 | IList res = new List();
1045 | if (root != null)
1046 | BinaryTreePaths(root, res, "");
1047 | return res;
1048 | }
1049 |
1050 | private static void BinaryTreePaths(TreeNode root, IList res, string path)
1051 | {
1052 | if (root.left == null && root.right == null)
1053 | {
1054 | res.Add(path + root.val);
1055 | return;
1056 | }
1057 |
1058 | if (root.left != null)
1059 | BinaryTreePaths(root.left, res, string.Format("{0}{1}->", path, root.val));
1060 |
1061 | if (root.right != null)
1062 | BinaryTreePaths(root.right, res, string.Format("{0}{1}->", path, root.val));
1063 | }
1064 | #endregion
1065 |
1066 | //https://leetcode-cn.com/problems/add-digits/description/
1067 | #region 258. 各位相加
1068 | // 这个题放到Easy里,有点说不过去。
1069 | // 参考这个:https://en.wikipedia.org/wiki/Digital_root#Congruence_formula
1070 | public static int AddDigits(int num)
1071 | {
1072 | return 1 + (num - 1) % 9;
1073 | }
1074 | #endregion
1075 |
1076 | //https://leetcode-cn.com/problems/ugly-number/description/
1077 | #region 263. 丑数
1078 | public static bool IsUgly(int num)
1079 | {
1080 | if (num <= 0)
1081 | return false;
1082 |
1083 | while (num % 2 == 0)
1084 | num /= 2;
1085 |
1086 | while (num % 3 == 0)
1087 | num /= 3;
1088 |
1089 | while (num % 5 == 0)
1090 | num /= 5;
1091 |
1092 | return num == 1;
1093 | }
1094 | #endregion
1095 |
1096 | //https://leetcode-cn.com/problems/missing-number/description/
1097 | #region 268. 缺失数字
1098 | public static int MissingNumber(int[] nums)
1099 | {
1100 | int sum = 0;
1101 | for (int i = 0; i < nums.Length; i++)
1102 | sum += nums[i];
1103 |
1104 | return (nums.Length * (nums.Length + 1)) / 2 - sum;
1105 | }
1106 | #endregion
1107 |
1108 | //https://leetcode-cn.com/problems/first-bad-version/description/
1109 | #region 278. 第一个错误的版本
1110 | /* The isBadVersion API is defined in the parent class VersionControl.
1111 | bool IsBadVersion(int version); */
1112 | public static int FirstBadVersion(int n)
1113 | {
1114 | int left = 1, right = n;
1115 | while (left < right)
1116 | {
1117 | int middle = left + (right - left) / 2;
1118 | if (!IsBadVersion(middle))
1119 | left = middle + 1;
1120 | else
1121 | right = middle;
1122 | }
1123 | return left;
1124 | }
1125 | private static bool IsBadVersion(int version)
1126 | {
1127 | return true;
1128 | }
1129 | #endregion
1130 |
1131 | //https://leetcode-cn.com/problems/move-zeroes/description/
1132 | #region 283. 移动零
1133 | public static void MoveZeroes(int[] nums)
1134 | {
1135 | int i = 0;
1136 | while (i < nums.Length)
1137 | {
1138 | if (nums[i] == 0)
1139 | {
1140 | int total = 0;
1141 | for (int j = i + 1; j < nums.Length; j++)
1142 | {
1143 | total += nums[j];
1144 | if (nums[j] != 0)
1145 | {
1146 | int t = nums[i];
1147 | nums[i] = nums[j];
1148 | nums[j] = t;
1149 | break;
1150 | }
1151 | }
1152 | if (total == 0)
1153 | return;
1154 | }
1155 | else
1156 | {
1157 | i++;
1158 | }
1159 | }
1160 | }
1161 | #endregion
1162 |
1163 | //https://leetcode-cn.com/problems/range-sum-query-immutable/description/
1164 | #region 303. 区域和检索 - 不可变
1165 | public class NumArray
1166 | {
1167 | public int[] Nums { private set; get; }
1168 | private int[] nums;
1169 | public NumArray(int[] nums)
1170 | {
1171 | Nums = new int[nums.Length];
1172 | this.nums = nums;
1173 | if (nums.Length <= 0)
1174 | return;
1175 |
1176 | Nums[0] = nums[0];
1177 | for (int i = 1; i < nums.Length; i++)
1178 | {
1179 | Nums[i] = nums[i];
1180 | this.nums[i] += nums[i - 1];
1181 | }
1182 | }
1183 |
1184 | public int SumRange(int i, int j)
1185 | {
1186 | if (i == 0)
1187 | return nums[j];
1188 |
1189 | return nums[j] - nums[i - 1];
1190 | }
1191 | }
1192 | #endregion
1193 |
1194 | //https://leetcode-cn.com/problems/power-of-three/description/
1195 | #region 326. 3的幂
1196 | ///
1197 | /// 1162261467 is the biggest int number of power 3
1198 | ///
1199 | public static bool IsPowerOfThree(int n)
1200 | {
1201 | return (n > 0 && 1162261467 % n == 0);
1202 | }
1203 | #endregion
1204 |
1205 | //https://leetcode-cn.com/problems/power-of-four/description/
1206 | #region 342. 4的幂
1207 | public static bool IsPowerOfFour(int num)
1208 | {
1209 | return (((num - 1) & num) == 0) && ((num & 0x55555555) != 0);
1210 | }
1211 | #endregion
1212 |
1213 | //https://leetcode-cn.com/problems/reverse-string/description/
1214 | #region 344. 反转字符串
1215 | public static string ReverseString(string s)
1216 | {
1217 | char[] chars = new char[s.Length];
1218 | int start = 0;
1219 | int end = s.Length - 1;
1220 | while (start <= end)
1221 | {
1222 | chars[start] = s[end];
1223 | chars[end] = s[start];
1224 | start++;
1225 | end--;
1226 | }
1227 | return new string(chars);
1228 | }
1229 | #endregion
1230 |
1231 | //https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/
1232 | #region 345. 反转字符串中的元音字母
1233 | public static string ReverseVowels(string s)
1234 | {
1235 | char[] str = s.ToCharArray();
1236 |
1237 | int l = 0, r = str.Length - 1;
1238 | bool lIsVowel = false, rIsVowel = false;
1239 | while (l < r)
1240 | {
1241 | lIsVowel = IsVowel(str[l]);
1242 | rIsVowel = IsVowel(str[r]);
1243 |
1244 | if (!lIsVowel)
1245 | l++;
1246 |
1247 | if (!rIsVowel)
1248 | r--;
1249 |
1250 | if (lIsVowel && rIsVowel)
1251 | {
1252 | char t = str[l];
1253 | str[l] = str[r];
1254 | str[r] = t;
1255 | l++;
1256 | r--;
1257 | }
1258 | }
1259 |
1260 | return new string(str);
1261 | }
1262 |
1263 | private static bool IsVowel(char c)
1264 | {
1265 | return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
1266 | }
1267 | #endregion
1268 |
1269 | //https://leetcode-cn.com/problems/valid-perfect-square/description/
1270 | #region 367. 有效的完全平方数
1271 | ///
1272 | /// Sn = (1+ 2n - 1)* n)/2 = n²
1273 | ///
1274 | public static bool IsPerfectSquare(int num)
1275 | {
1276 | int i = 1;
1277 | // a1 = 2*1 - 1;
1278 | // an = 2n - 1;
1279 | // Sn = (1+ 2n - 1)* n)/2 = n²
1280 | while (num > 0)
1281 | {
1282 | num -= i;
1283 | i += 2;
1284 | }
1285 | return num == 0;
1286 |
1287 | //int left = 1, right = num;
1288 | //while(left < right)
1289 | //{
1290 | // int middle = left + (right - left) / 2;
1291 | // long res = middle * middle;
1292 | // if(res == num)
1293 | // return true;
1294 | // else if(res > num)
1295 | // right = middle -1;
1296 | // else
1297 | // left = middle + 1;
1298 | //}
1299 | //return false;
1300 | }
1301 | #endregion
1302 |
1303 | //https://leetcode-cn.com/problems/sum-of-two-integers/description/
1304 | #region 371. 两整数之和
1305 | public static int GetSum(int a, int b)
1306 | {
1307 | if (b == 0)
1308 | return a;
1309 |
1310 | int sum, carry;
1311 | sum = a ^ b;
1312 | carry = (a & b) << 1;
1313 | return GetSum(sum, carry);
1314 | }
1315 | #endregion
1316 |
1317 | //https://leetcode-cn.com/problems/guess-number-higher-or-lower/description/
1318 | #region 374. 猜数字大小
1319 | public static int GuessNumber(int n)
1320 | {
1321 | int l = 1, r = n;
1322 | while (true)
1323 | {
1324 | int m = (l & r) + ((l ^ r) >> 1);
1325 | int res = Guess(m);
1326 | if (res > 0)
1327 | {
1328 | l = m + 1;
1329 | }
1330 | else if (res < 0)
1331 | {
1332 | r = m - 1;
1333 | }
1334 | else
1335 | {
1336 | return res;
1337 | }
1338 | }
1339 | }
1340 | ///
1341 | /// 原题没有C#版本的,下面这个函数是我自己添加的,当做API以供调用。
1342 | ///
1343 | ///
1344 | ///
1345 | private static int Guess(int num)
1346 | {
1347 | int n = 6;
1348 | if (num > n)
1349 | return -1;
1350 | if (num < n)
1351 | return 1;
1352 | return 0;
1353 | }
1354 |
1355 | #endregion
1356 |
1357 | //https://leetcode-cn.com/problems/ransom-note/description/
1358 | #region 383. 赎金信
1359 | public static bool CanConstruct(string ransomNote, string magazine)
1360 | {
1361 | int[] arr = new int['z' + 1];
1362 | for (int i = 0; i < magazine.Length; i++)
1363 | arr[magazine[i]]++;
1364 |
1365 | for (int i = 0; i < ransomNote.Length; i++)
1366 | if (arr[ransomNote[i]]-- <= 0)
1367 | return false;
1368 |
1369 | return true;
1370 | }
1371 | #endregion
1372 |
1373 | //https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/
1374 | #region 387. 字符串中的第一个唯一字符
1375 | public static int FirstUniqChar(string s)
1376 | {
1377 | int[] asicii = new int['z' + 1];
1378 | int[] chars = new int[s.Length];
1379 | for (int i = 0; i < s.Length; i++)
1380 | {
1381 | char c = s[i];
1382 | if (asicii[c] == 0)
1383 | {
1384 | chars[i] = c;
1385 | asicii[c] = i + 1;
1386 | }
1387 | else
1388 | {
1389 | chars[asicii[c] - 1] = -1;
1390 | }
1391 | }
1392 |
1393 | for (int i = 0; i < chars.Length; i++)
1394 | if (chars[i] > 0)
1395 | return i;
1396 |
1397 | return -1;
1398 | }
1399 | #endregion
1400 |
1401 | //https://leetcode-cn.com/problems/find-the-difference/description/
1402 | #region 389. 找不同
1403 | public static char FindTheDifference(string s, string t)
1404 | {
1405 | char res = default(char);
1406 | for (int i = 0; i < s.Length; i++)
1407 | res ^= s[i];
1408 | for (int i = 0; i < t.Length; i++)
1409 | res ^= t[i];
1410 |
1411 | return res;
1412 | }
1413 | #endregion
1414 |
1415 | //https://leetcode-cn.com/problems/sum-of-left-leaves/description/
1416 | #region 404. 左叶子之和
1417 | public static int SumOfLeftLeaves(TreeNode root)
1418 | {
1419 | if (root == null)
1420 | return 0;
1421 |
1422 | return SumLeft(root.left) + SumRight(root.right);
1423 | }
1424 | private static int SumLeft(TreeNode node)
1425 | {
1426 | if (node == null)
1427 | return 0;
1428 |
1429 | if (node.left == null && node.right == null)
1430 | return node.val;
1431 |
1432 | return SumLeft(node.left) + SumRight(node.right);
1433 | }
1434 |
1435 | private static int SumRight(TreeNode node)
1436 | {
1437 | if (node == null)
1438 | return 0;
1439 |
1440 | return SumLeft(node.left) + SumRight(node.right);
1441 | }
1442 | #endregion
1443 |
1444 | //https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/
1445 | #region 405. 数字转换为十六进制数
1446 | private static char[] hexs = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
1447 | public static string ToHex(int num)
1448 | {
1449 | if (num == 0)
1450 | return "0";
1451 |
1452 | LinkedList res = new LinkedList();
1453 | long n = num & 0xFFFFFFFF;
1454 | while (n != 0)
1455 | {
1456 | long low4 = n & 0xf;
1457 | res.AddFirst(hexs[low4]);
1458 | n >>= 4;
1459 | }
1460 | return new string(res.ToArray());
1461 | }
1462 | #endregion
1463 |
1464 | //https://leetcode-cn.com/problems/fizz-buzz/description/
1465 | #region 412. Fizz Buzz
1466 | public static IList FizzBuzz(int n)
1467 | {
1468 | List strs = new List();
1469 |
1470 | for (int i = 1; i <= n; i++)
1471 | {
1472 | if (i % 3 == 0)
1473 | {
1474 | if (i % 5 == 0)
1475 | {
1476 | strs.Add("FizzBuzz");
1477 | }
1478 | else
1479 | {
1480 | strs.Add("Fizz");
1481 | }
1482 | }
1483 | else if (i % 5 == 0)
1484 | {
1485 | if (i % 3 == 0)
1486 | {
1487 | strs.Add("FizzBuzz");
1488 | }
1489 | else
1490 | {
1491 | strs.Add("Buzz");
1492 | }
1493 | }
1494 | else
1495 | {
1496 | strs.Add(i.ToString());
1497 | }
1498 | }
1499 |
1500 | return strs;
1501 | }
1502 | #endregion
1503 |
1504 | //https://leetcode-cn.com/problems/add-strings/description/
1505 | #region 415. 字符串相加
1506 | public static string AddStrings(string num1, string num2)
1507 | {
1508 | List res = new List();
1509 | int carry = 0, sum = 0;
1510 | int n1 = 0, n2 = 0;
1511 |
1512 | int i = 1;
1513 | while ((num1.Length - i >= 0 || num2.Length - i >= 0) || carry > 0)
1514 | {
1515 | if (num1.Length - i >= 0)
1516 | n1 = num1[num1.Length - i] - '0';
1517 | else
1518 | n1 = 0;
1519 |
1520 | if (num2.Length - i >= 0)
1521 | n2 = num2[num2.Length - i] - '0';
1522 | else
1523 | n2 = 0;
1524 | sum = n1 + n2 + carry;
1525 | carry = sum / 10;
1526 | sum %= 10;
1527 | res.Insert(0, (char)(sum + '0'));
1528 | i++;
1529 | }
1530 |
1531 | return new string(res.ToArray());
1532 | }
1533 | #endregion
1534 |
1535 | //https://leetcode-cn.com/problems/string-compression/description/
1536 | #region 443. 压缩字符串
1537 | public static int Compress(char[] chars)
1538 | {
1539 | if (chars.Length < 2)
1540 | return chars.Length;
1541 |
1542 | char target = chars[0];
1543 | int len = 1;
1544 | int index = 0;
1545 |
1546 | for (int i = 1; i <= chars.Length; i++)
1547 | {
1548 | char v;
1549 | if (i < chars.Length)
1550 | v = chars[i];
1551 | else
1552 | v = (char)0;
1553 | if (target != v)
1554 | {
1555 | chars[index] = target;
1556 | index++;
1557 | if (len > 1)
1558 | {
1559 | string l = len.ToString();
1560 | for (int k = 0; k < l.Length; k++)
1561 | {
1562 | chars[index] = l[k];
1563 | index++;
1564 | }
1565 | }
1566 | target = v;
1567 | len = 1;
1568 | }
1569 | else
1570 | {
1571 | len++;
1572 | }
1573 | }
1574 |
1575 | return index;
1576 | }
1577 | #endregion
1578 |
1579 | //https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/
1580 | #region 448. 找到所有数组中消失的数字
1581 | public static IList FindDisappearedNumbers(int[] nums)
1582 | {
1583 | int[] arr = new int[nums.Length + 1];
1584 | for (int i = 0; i < nums.Length; i++)
1585 | arr[nums[i]] = nums[i];
1586 |
1587 | List res = new List();
1588 | for (int i = 1; i < arr.Length; i++)
1589 | if (arr[i] == 0)
1590 | res.Add(i);
1591 |
1592 | return res;
1593 | }
1594 | #endregion
1595 |
1596 | //https://leetcode-cn.com/problems/hamming-distance/description/
1597 | #region 461. 汉明距离
1598 | public static int HammingDistance(int x, int y)
1599 | {
1600 | int counter = 0;
1601 | int r = x ^ y;
1602 | while (r > 0)
1603 | {
1604 | r = r & (r - 1);
1605 | counter++;
1606 | }
1607 |
1608 | return counter;
1609 | }
1610 | #endregion
1611 |
1612 | //https://leetcode-cn.com/problems/island-perimeter/description/
1613 | #region 463. 岛屿的周长
1614 | public static int IslandPerimeter(int[,] grid)
1615 | {
1616 | int res = 0;
1617 | int maxRow = grid.GetUpperBound(0);
1618 | int maxCol = grid.GetUpperBound(1);
1619 | int b, r;
1620 | for (int row = -1; row <= maxRow; row++)
1621 | {
1622 | for (int col = -1; col <= maxCol; col++)
1623 | {
1624 | int v = row < 0 || col < 0 ? 0 : grid[row, col];
1625 |
1626 | b = col < 0 || row == maxRow ? 0 : grid[row + 1, col];
1627 | r = row < 0 || col == maxCol ? 0 : grid[row, col + 1];
1628 |
1629 | if (v != b)
1630 | res++;
1631 | if (v != r)
1632 | res++;
1633 | }
1634 | }
1635 |
1636 | return res;
1637 | }
1638 | #endregion
1639 |
1640 | //https://leetcode-cn.com/problems/number-complement/description/
1641 | #region 476. 数字的补数
1642 | public static int FindComplement(int num)
1643 | {
1644 | int i = 1;
1645 |
1646 | while (i < num)
1647 | i = i | (i << 1);
1648 |
1649 | return ~num & i;
1650 | }
1651 | #endregion
1652 |
1653 | //https://leetcode-cn.com/problems/max-consecutive-ones/description/
1654 | #region 485. 最大连续1的个数
1655 | public static int FindMaxConsecutiveOnes(int[] nums)
1656 | {
1657 | int max = 0;
1658 | int c = 0;
1659 | foreach (var n in nums)
1660 | {
1661 | if (n == 0)
1662 | {
1663 | max = Math.Max(c, max);
1664 | c = 0;
1665 | }
1666 | else
1667 | {
1668 | c++;
1669 | }
1670 | }
1671 |
1672 | return Math.Max(c, max);
1673 | }
1674 | #endregion
1675 |
1676 | //https://leetcode-cn.com/problems/keyboard-row/description/
1677 | #region 500. 键盘行
1678 | public static string[] FindWords(string[] words)
1679 | {
1680 | int[] keyMap = new int['z' - 'A' + 1];
1681 | string row1 = "QWERTYUIOP";
1682 | string row2 = "ASDFGHJKL";
1683 | string row3 = "ZXCVBNM";
1684 |
1685 | InitMap(row1, keyMap, 1);
1686 | InitMap(row2, keyMap, 2);
1687 | InitMap(row3, keyMap, 3);
1688 | List strs = new List();
1689 | foreach (var word in words)
1690 | {
1691 | if (IsInOneLine(word, keyMap))
1692 | strs.Add(word);
1693 | }
1694 |
1695 | return strs.ToArray();
1696 | }
1697 | private static void InitMap(string str, int[] keyMap, int row)
1698 | {
1699 | for (int i = 0; i < str.Length; i++)
1700 | {
1701 | char c = str[i];
1702 | keyMap[c - 'A'] = row;
1703 | keyMap[c - 'A' + 'a' - 'A'] = row;
1704 | }
1705 | }
1706 | private static bool IsInOneLine(string str, int[] keyMap)
1707 | {
1708 | if (string.IsNullOrWhiteSpace(str))
1709 | return false;
1710 |
1711 | if (str.Length == 1)
1712 | return true;
1713 |
1714 | int basic = keyMap[str[0] - 'A'];
1715 | for (int i = 1; i < str.Length; i++)
1716 | {
1717 | if (keyMap[str[i] - 'A'] != basic)
1718 | return false;
1719 | }
1720 |
1721 | return true;
1722 | }
1723 | #endregion
1724 |
1725 | //https://leetcode-cn.com/problems/base-7/description/
1726 | #region 504. 七进制数
1727 | public static string ConvertToBase7(int num)
1728 | {
1729 | if (num == 0)
1730 | return "0";
1731 |
1732 | Stack res = new Stack();
1733 | bool addSymble = false;
1734 | if (num < 0)
1735 | {
1736 | addSymble = true;
1737 | num = -num;
1738 | }
1739 |
1740 | while (num > 0)
1741 | {
1742 | res.Push((char)(num % 7 + '0'));
1743 | num /= 7;
1744 | }
1745 |
1746 | if (addSymble)
1747 | res.Push('-');
1748 | return new string(res.ToArray());
1749 | }
1750 | #endregion
1751 |
1752 | //https://leetcode-cn.com/problems/detect-capital/description/
1753 | #region 520. 检测大写字母
1754 | public static bool DetectCapitalUse(string word)
1755 | {
1756 | if (word.Length <= 1)
1757 | return true;
1758 |
1759 | if (word.Length <= 2)
1760 | {
1761 | if (IsCaptital(word[0]))
1762 | return true;
1763 |
1764 | if ((!IsCaptital(word[0]) && !IsCaptital(word[1])))
1765 | return true;
1766 |
1767 | if ((IsCaptital(word[0]) && IsCaptital(word[1])))
1768 | return true;
1769 |
1770 | return false;
1771 | }
1772 |
1773 | for (int i = 0; i <= word.Length - 3; i++)
1774 | {
1775 | if (IsCaptital(word[i]) && IsCaptital(word[i + 1]) && IsCaptital(word[i + 2]))
1776 | continue;
1777 |
1778 | if (!IsCaptital(word[i]) && !IsCaptital(word[i + 1]) && !IsCaptital(word[i + 2]))
1779 | continue;
1780 |
1781 | if (IsCaptital(word[i]) && !IsCaptital(word[i + 1]) && !IsCaptital(word[i + 2]))
1782 | continue;
1783 |
1784 | return false;
1785 | }
1786 |
1787 | return true;
1788 | }
1789 | private static bool IsCaptital(char c)
1790 | {
1791 | return c - 'a' < 0;
1792 | }
1793 | #endregion
1794 |
1795 | //https://leetcode-cn.com/problems/reverse-string-ii/description/
1796 | #region 541. 反转字符串 II
1797 | public static string ReverseStr(string s, int k)
1798 | {
1799 | char[] str = s.ToArray();
1800 | int l = s.Length;
1801 | if (l == 0)
1802 | return string.Empty;
1803 |
1804 | if (k > l)
1805 | Swap(str, 0, l);
1806 | else
1807 | for (int i = 0; i < l;)
1808 | {
1809 | int j = Math.Min(i + k, l);
1810 | Swap(str, i, j);
1811 | i += 2 * k;
1812 | }
1813 |
1814 | return new string(str);
1815 | }
1816 | private static void Swap(char[] chars, int start, int end)
1817 | {
1818 | while (start < end)
1819 | {
1820 | char c = chars[start];
1821 | chars[start++] = chars[--end];
1822 | chars[end] = c;
1823 | }
1824 | }
1825 | #endregion
1826 |
1827 | //https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/description/
1828 | #region 557. 反转字符串中的单词 III
1829 | public static string ReverseWords(string s)
1830 | {
1831 | var chars = new char[s.Length];
1832 | int start = 0;
1833 | for (int i = 0; i < s.Length; i++)
1834 | {
1835 | if (s[i].Equals(' '))
1836 | {
1837 | chars[i] = ' ';
1838 | ReverseLetters(s, chars, start, i - 1);
1839 | start = i + 1;
1840 | }
1841 | else if (i == s.Length - 1)
1842 | {
1843 | ReverseLetters(s, chars, start, s.Length - 1);
1844 | }
1845 | }
1846 |
1847 | return new string(chars);
1848 | }
1849 | private static void ReverseLetters(string s, char[] chars, int start, int end)
1850 | {
1851 | while (start <= end)
1852 | {
1853 | chars[start] = s[end];
1854 | chars[end] = s[start];
1855 | start++;
1856 | end--;
1857 | }
1858 | }
1859 | #endregion
1860 |
1861 | //https://leetcode.com/problems/array-partition-i/description/
1862 | #region 561. 数组拆分 I
1863 | public static int ArrayPairSum(int[] nums)
1864 | {
1865 | Array.Sort(nums);
1866 | int sum = 0;
1867 | for (int i = 0; i < nums.Length; i += 2)
1868 | sum += nums[i];
1869 |
1870 | return sum;
1871 | }
1872 | #endregion
1873 |
1874 | //https://leetcode-cn.com/problems/merge-two-binary-trees/description/
1875 | #region 617. 合并二叉树
1876 | public static TreeNode MergeTrees(TreeNode t1, TreeNode t2)
1877 | {
1878 | if (t1 == null && t2 == null)
1879 | return null;
1880 |
1881 | int v = 0;
1882 | if (t1 != null)
1883 | v += t1.val;
1884 | if (t2 != null)
1885 | v += t2.val;
1886 |
1887 | var res = new TreeNode(v);
1888 |
1889 | res.left = MergeTrees(t1 != null ? t1.left : null, t2 != null ? t2.left : null);
1890 | res.right = MergeTrees(t1 != null ? t1.right : null, t2 != null ? t2.right : null);
1891 |
1892 | return res;
1893 | }
1894 | #endregion
1895 |
1896 | //https://leetcode.cn/problems/maximum-product-of-three-numbers/description/
1897 | #region 628. 三个数的最大乘积
1898 | public static int MaximumProduct(int[] nums)
1899 | {
1900 | int l = nums.Length;
1901 | if (l <= 3)
1902 | {
1903 | return nums[0] * nums[1] * nums[2];
1904 | }
1905 | Array.Sort(nums);
1906 | return Math.Max(nums[l - 1] * nums[l - 2] * nums[l - 3], nums[0] * nums[1] * nums[l - 1]);
1907 | }
1908 | #endregion
1909 |
1910 | //https://leetcode-cn.com/problems/sum-of-square-numbers/description/
1911 | #region 633. 平方数之和
1912 | public static bool JudgeSquareSum(int c)
1913 | {
1914 | int left = 0;
1915 | int right = (int)Math.Sqrt(c);
1916 |
1917 | while (left <= right)
1918 | {
1919 | int r = left * left + right * right;
1920 | if (r > c)
1921 | right--;
1922 | else if (r < c)
1923 | left++;
1924 | else
1925 | return true;
1926 | }
1927 |
1928 | return false;
1929 | }
1930 | #endregion
1931 |
1932 | //https://leetcode.cn/problems/average-of-levels-in-binary-tree/
1933 | #region 637. 二叉树的层平均值
1934 | public static IList AverageOfLevels(TreeNode root)
1935 | {
1936 | IList res = new List();
1937 | Queue queue = new Queue();
1938 | queue.Enqueue(root);
1939 | BFSTree(queue, res);
1940 | return res;
1941 | }
1942 |
1943 | private static void BFSTree(Queue levelQueue, IList res)
1944 | {
1945 | double value = 0;
1946 | Queue queue = new Queue();
1947 | int n = levelQueue.Count;
1948 | if (n <= 0)
1949 | {
1950 | return;
1951 | }
1952 | while (levelQueue.Count > 0)
1953 | {
1954 | TreeNode current = levelQueue.Dequeue();
1955 | value += current.val;
1956 | if (current.left != null)
1957 | {
1958 | queue.Enqueue(current.left);
1959 | }
1960 |
1961 | if (current.right != null)
1962 | {
1963 | queue.Enqueue(current.right);
1964 | }
1965 | }
1966 |
1967 | res.Add(value / n);
1968 | BFSTree(queue, res);
1969 | }
1970 | #endregion
1971 |
1972 | //https://leetcode-cn.com/problems/set-mismatch/description/
1973 | #region 645. 错误的集合
1974 | public static int[] FindErrorNums(int[] nums)
1975 | {
1976 | int[] res = new int[2];
1977 | int sum = 0;
1978 | foreach (var n in nums)
1979 | {
1980 | int i = Math.Abs(n);
1981 | if (nums[i - 1] < 0)
1982 | res[0] = i;
1983 | else
1984 | nums[i - 1] *= -1;
1985 | sum += i;
1986 | }
1987 | res[1] = (1 + nums.Length) * nums.Length / 2 - sum + res[0];
1988 |
1989 | return res;
1990 | }
1991 | #endregion
1992 |
1993 | //https://leetcode-cn.com/problems/judge-route-circle/description/
1994 | #region 657. 判断路线成圈
1995 | public static bool JudgeCircle(string moves)
1996 | {
1997 | int res = 0;
1998 | for (int i = 0; i < moves.Length; i++)
1999 | {
2000 | char c = moves[i];
2001 | if (c == 'U')
2002 | res++;
2003 | else if (c == 'D')
2004 | res--;
2005 | else if (c == 'L')
2006 | res += 2;
2007 | else if (c == 'R')
2008 | res -= 2;
2009 | }
2010 | return res == 0;
2011 | }
2012 | #endregion
2013 |
2014 | //https://leetcode-cn.com/problems/non-decreasing-array/description/
2015 | #region 665. 非递减数列
2016 | ///
2017 | /// for more effective, can do not copy array
2018 | /// there copy, because function name is CheckXXX, so it's better don't change original data
2019 | ///
2020 | ///
2021 | ///
2022 | public static bool CheckPossibility(int[] nums)
2023 | {
2024 | int[] arr = new int[nums.Length];
2025 | Array.Copy(nums, arr, nums.Length);
2026 | bool isMabeyInvalid = false;
2027 | int i = 1;
2028 | while (i < arr.Length)
2029 | {
2030 | if (arr[i - 1] > arr[i])
2031 | {
2032 | if (isMabeyInvalid)
2033 | return false;
2034 |
2035 | if (i < 2 || arr[i - 2] < arr[i])
2036 | arr[i - 1] = arr[i];
2037 | else
2038 | arr[i] = arr[i - 1];
2039 | isMabeyInvalid = true;
2040 | }
2041 | else
2042 | i++;
2043 | }
2044 |
2045 | return true;
2046 | }
2047 | #endregion
2048 |
2049 | //https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/
2050 | #region 693. 交替位二进制数
2051 | public static bool HasAlternatingBits(int n)
2052 | {
2053 | return (((long)n + (n >> 1) + 1) & ((long)n + (n >> 1))) == 0;
2054 | }
2055 | #endregion
2056 |
2057 | //https://leetcode-cn.com/problems/binary-search/
2058 |
2059 | #region 704. 二分查找
2060 | public static int Search(int[] nums, int target)
2061 | {
2062 | int s = 0;
2063 | int e = nums.Length - 1;
2064 | int i = nums.Length / 2;
2065 |
2066 | while (s <= e)
2067 | {
2068 | int v = nums[i];
2069 | if (v == target)
2070 | {
2071 | return i;
2072 | }
2073 |
2074 | if (v < target)
2075 | {
2076 | s = i + 1;
2077 | }
2078 | else
2079 | {
2080 | e = i - 1;
2081 | }
2082 |
2083 | i = (e - s) / 2 + s;
2084 | }
2085 |
2086 | return -1;
2087 | }
2088 | #endregion
2089 |
2090 | //https://leetcode-cn.com/problems/to-lower-case/description/
2091 | #region 709. 转换成小写字母
2092 | public static string ToLowerCase(string str)
2093 | {
2094 | var arr = str.ToCharArray();
2095 | for (int i = 0; i < str.Length; i++)
2096 | {
2097 | char c = str[i];
2098 | if ('A' <= c && c <= 'Z')
2099 | c = (char)(c | 0x20);
2100 | arr[i] = c;
2101 | }
2102 |
2103 | return new string(arr);
2104 | }
2105 | #endregion
2106 |
2107 | //https://leetcode-cn.com/problems/self-dividing-numbers/description/
2108 | #region 728. 自除数
2109 | public static IList SelfDividingNumbers(int left, int right)
2110 | {
2111 | IList res = new List();
2112 | for (int i = left; i <= right; i++)
2113 | {
2114 | int j = i;
2115 | for (; j > 0; j /= 10)
2116 | {
2117 | int a = j % 10;
2118 | if (a == 0)
2119 | break;
2120 |
2121 | if (i % a != 0)
2122 | break;
2123 | }
2124 | if (j == 0)
2125 | res.Add(i);
2126 | }
2127 |
2128 | return res;
2129 | }
2130 | #endregion
2131 |
2132 | //https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/description/
2133 | #region 762. 二进制表示中质数个计算置位
2134 | public static int CountPrimeSetBits(int L, int R)
2135 | {
2136 | int c = 0;
2137 | for (int i = L; i <= R; i++)
2138 | if (IsPrime(CountBit(i)))
2139 | c++;
2140 | return c;
2141 | }
2142 | private static bool[] primeArray = new bool[] { false, false, true, true, false, true,
2143 | false, true, false, false, false, true,
2144 | false, true, false, false, false, true,
2145 | false, true, false, false, false, true };
2146 | private static bool IsPrime(int num)
2147 | {
2148 | return primeArray[num];
2149 | }
2150 | private static int CountBit(int num)
2151 | {
2152 | int res = 0;
2153 | while (num > 0)
2154 | {
2155 | num &= (num - 1);
2156 | res++;
2157 | }
2158 | return res;
2159 | }
2160 | #endregion
2161 |
2162 | //https://leetcode-cn.com/problems/toeplitz-matrix/description/
2163 | #region 766. 托普利茨矩阵
2164 | public static bool IsToeplitzMatrix(int[,] matrix)
2165 | {
2166 | for (int i = 1; i < matrix.GetLength(0); i++)
2167 | for (int j = 1; j < matrix.GetLength(1); j++)
2168 | if (matrix[i, j] != matrix[i - 1, j - 1])
2169 | return false;
2170 |
2171 | return true;
2172 | }
2173 | #endregion
2174 |
2175 | //https://leetcode-cn.com/problems/jewels-and-stones/description/
2176 | #region 771. 宝石与石头
2177 | public static int NumJewelsInStones(string J, string S)
2178 | {
2179 | int[] stones = new int['z' + 1];
2180 | for (int i = 0; i < S.Length; i++)
2181 | stones[S[i]]++;
2182 |
2183 | int count = 0;
2184 | for (int i = 0; i < J.Length; i++)
2185 | count += stones[J[i]];
2186 |
2187 | return count;
2188 | }
2189 |
2190 | #endregion
2191 |
2192 | //https://leetcode-cn.com/problems/letter-case-permutation/description/
2193 | #region 784. 字母大小写全排列
2194 | public static IList LetterCasePermutation(string S)
2195 | {
2196 | HashSet res = new HashSet();
2197 | S = S.ToLower();
2198 | res.Add(S);
2199 | Permutation(S, 0, res);
2200 | return res.ToList();
2201 | }
2202 |
2203 | private static void Permutation(string S, int index, HashSet res)
2204 | {
2205 | for (int i = index; i < S.Length; i++)
2206 | {
2207 | char c = S[i];
2208 | if (char.IsLetter(c))
2209 | {
2210 | var s = S.ToCharArray();
2211 | if (char.IsLower(c))
2212 | {
2213 | s[i] = char.ToUpper(c);
2214 | string _s = new string(s);
2215 | res.Add(_s);
2216 | Permutation(_s, index + 1, res);
2217 | }
2218 | }
2219 | }
2220 | }
2221 | #endregion
2222 |
2223 | //https://leetcode-cn.com/problems/rotate-string/description/
2224 | #region 796. 旋转字符串
2225 | public static bool RotateString(string A, string B)
2226 | {
2227 | if (A.Length != B.Length)
2228 | return false;
2229 | return (B + B).Contains(A);
2230 | }
2231 | #endregion
2232 |
2233 | //https://leetcode-cn.com/problems/unique-morse-code-words/description/
2234 | #region 804. 唯一摩尔斯密码词
2235 | public static int UniqueMorseRepresentations(string[] words)
2236 | {
2237 | HashSet morses = new HashSet();
2238 | foreach (var word in words)
2239 | {
2240 | var m = WordToMorse(word);
2241 | if (!morses.Contains(m))
2242 | morses.Add(m);
2243 | }
2244 | return morses.Count;
2245 | }
2246 | private static string[] morse = new string[] { ".-", "-...", "-.-.", "-..", ".",
2247 | "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
2248 | ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
2249 | private static string WordToMorse(string word)
2250 | {
2251 | StringBuilder sb = new StringBuilder();
2252 | for (int i = 0; i < word.Length; i++)
2253 | sb.Append(morse[word[i] - 'a']);
2254 |
2255 | return sb.ToString();
2256 | }
2257 | #endregion
2258 |
2259 | //https://leetcode-cn.com/problems/most-common-word/description/
2260 | #region 819. 最常见的单词
2261 | public static string MostCommonWord(string paragraph, string[] banned)
2262 | {
2263 | var words = paragraph.ToLower().Split(new char[] { '!', '?', '\'', ',', ';', '.', ' ' }, StringSplitOptions.RemoveEmptyEntries);
2264 | Dictionary map = new Dictionary();
2265 | HashSet bannedSet = new HashSet(banned);
2266 |
2267 | foreach (var word in words)
2268 | {
2269 | if (!bannedSet.Contains(word))
2270 | {
2271 | if (map.ContainsKey(word))
2272 | map[word]++;
2273 | else
2274 | map.Add(word, 1);
2275 | }
2276 | }
2277 | int max = 0;
2278 | string w = string.Empty;
2279 | foreach (var kvp in map)
2280 | {
2281 | if (kvp.Value > max)
2282 | {
2283 | w = kvp.Key;
2284 | max = kvp.Value;
2285 | }
2286 | }
2287 |
2288 | return w;
2289 | }
2290 | #endregion
2291 |
2292 | //https://leetcode-cn.com/problems/shortest-distance-to-a-character/description/
2293 | #region 821. 字符的最短距离
2294 | public static int[] ShortestToChar(string S, char C)
2295 | {
2296 | int[] arr = new int[S.Length];
2297 | for (int i = 0; i < S.Length; i++)
2298 | {
2299 | int l = i, r = i;
2300 | while (true)
2301 | {
2302 | if (S[l] == C)
2303 | {
2304 | arr[i] = i - l;
2305 | break;
2306 | }
2307 | if (S[r] == C)
2308 | {
2309 | arr[i] = r - i;
2310 | break;
2311 | }
2312 | l = --l < 0 ? 0 : l;
2313 | r = ++r >= S.Length ? S.Length - 1 : r;
2314 | }
2315 | }
2316 |
2317 | return arr;
2318 | }
2319 | #endregion
2320 |
2321 | //https://leetcode-cn.com/problems/goat-latin/description/
2322 | #region 824. 山羊拉丁文
2323 | public static string ToGoatLatin(string S)
2324 | {
2325 | var arr = S.Split(' ');
2326 | StringBuilder sb = new StringBuilder();
2327 | for (int i = 0; i < arr.Length; i++)
2328 | {
2329 | sb.Append(Transform(arr[i], i + 1));
2330 | if (i != arr.Length - 1)
2331 | sb.Append(" ");
2332 | }
2333 | return sb.ToString();
2334 | }
2335 | private static string Transform(string s, int index)
2336 | {
2337 | if (s[0] == 'A' || s[0] == 'a'
2338 | || s[0] == 'E' || s[0] == 'e'
2339 | || s[0] == 'I' || s[0] == 'i'
2340 | || s[0] == 'O' || s[0] == 'o'
2341 | || s[0] == 'U' || s[0] == 'u')
2342 | {
2343 | char[] r = new char[2 + index];
2344 | r[0] = 'm';
2345 | r[1] = 'a';
2346 | for (int i = 2; i < r.Length; i++)
2347 | r[i] = 'a';
2348 |
2349 | return s + new string(r);
2350 | }
2351 | else
2352 | {
2353 | char[] r = new char[s.Length + 2 + index];
2354 | for (int i = 0; i < s.Length; i++)
2355 | r[i] = s[(i + 1) % s.Length];
2356 | r[r.Length - 1 - index] = 'a';
2357 | r[r.Length - 2 - index] = 'm';
2358 | for (int i = s.Length + 2; i < s.Length + 2 + index; i++)
2359 | r[i] = 'a';
2360 | return new string(r);
2361 | }
2362 | }
2363 | #endregion
2364 |
2365 | //https://leetcode-cn.com/problems/flipping-an-image/description/
2366 | #region 832. 翻转图像
2367 | public static int[][] FlipAndInvertImage(int[][] A)
2368 | {
2369 | for (int i = 0; i < A.Length; i++)
2370 | {
2371 | int l = 0, r = A[i].Length - 1;
2372 | while (l <= r)
2373 | {
2374 | int t = A[i][l];
2375 | A[i][l] = A[i][r] ^ 1;
2376 | A[i][r] = t ^ 1;
2377 | l++;
2378 | r--;
2379 | }
2380 | }
2381 |
2382 | return A;
2383 | }
2384 | #endregion
2385 |
2386 | //https://leetcode-cn.com/problems/rectangle-overlap/description/
2387 | #region 836. 矩形重叠
2388 | public static bool IsRectangleOverlap(int[] rec1, int[] rec2)
2389 | {
2390 | return !(rec2[0] >= rec1[2] || rec2[1] >= rec1[3] || rec1[0] >= rec2[2] || rec1[1] >= rec2[3]);
2391 | }
2392 | #endregion
2393 |
2394 | //https://leetcode-cn.com/problems/backspace-string-compare/description/
2395 | #region 844. 比较含退格的字符串
2396 | public static bool BackspaceCompare(string S, string T)
2397 | {
2398 | return Trim(S).Equals(Trim(T));
2399 | }
2400 | private static string Trim(string s)
2401 | {
2402 | int i = 0;
2403 | Stack res = new Stack();
2404 | while (i < s.Length)
2405 | {
2406 | if (s[i] == '#')
2407 | {
2408 | if (res.Count > 0)
2409 | res.Pop();
2410 | }
2411 | else
2412 | {
2413 | res.Push(s[i]);
2414 | }
2415 | i++;
2416 | }
2417 | return new string(res.ToArray());
2418 | }
2419 | #endregion
2420 |
2421 | //https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/description/
2422 | #region 852. 山脉数组的峰顶索引
2423 | public static int PeakIndexInMountainArray(int[] A)
2424 | {
2425 | for (int i = 1; i < A.Length; i++)
2426 | {
2427 | if (A[i] < A[i - 1])
2428 | return i - 1;
2429 | }
2430 | return A.Length;
2431 | }
2432 | #endregion
2433 |
2434 | //https://leetcode-cn.com/problems/buddy-strings/description/
2435 | #region 859. 亲密字符串
2436 | public static bool BuddyStrings(string A, string B)
2437 | {
2438 | if (A.Length != B.Length)
2439 | return false;
2440 |
2441 | if (A.Equals(B))
2442 | {
2443 | HashSet set = new HashSet();
2444 |
2445 | for (int i = 0; i < A.Length; i++)
2446 | if (!set.Add(A[i]))
2447 | return true;
2448 |
2449 | return false;
2450 | }
2451 |
2452 | int c = 0;
2453 | int xor = 0;
2454 |
2455 | for (int i = 0; i < A.Length; i++)
2456 | {
2457 | char a = A[i], b = B[i];
2458 | xor ^= (a ^ b);
2459 | if (a != b)
2460 | c++;
2461 | }
2462 |
2463 | return xor == 0 && c == 2;
2464 | }
2465 | #endregion
2466 |
2467 | //https://leetcode-cn.com/problems/lemonade-change/description/
2468 | #region 860. 柠檬水找零
2469 | public static bool LemonadeChange(int[] bills)
2470 | {
2471 | int five = 0;
2472 | int ten = 0;
2473 | foreach (var bill in bills)
2474 | {
2475 | if (bill == 5)
2476 | {
2477 | five++;
2478 | }
2479 | else if (bill == 10)
2480 | {
2481 | if (five == 0)
2482 | return false;
2483 | else
2484 | five--;
2485 |
2486 | ten++;
2487 | }
2488 | else if (bill == 20)
2489 | {
2490 | if (five == 0)
2491 | {
2492 | return false;
2493 | }
2494 | else
2495 | {
2496 | if (ten == 0)
2497 | {
2498 | if (five < 3)
2499 | return false;
2500 | else
2501 | five -= 3;
2502 | }
2503 | else
2504 | {
2505 | if (five == 0)
2506 | {
2507 | return false;
2508 | }
2509 | else
2510 | {
2511 | ten--;
2512 | five--;
2513 | }
2514 | }
2515 | }
2516 | }
2517 | }
2518 |
2519 | return true;
2520 | }
2521 | #endregion
2522 |
2523 | //https://leetcode-cn.com/problems/transpose-matrix/description/
2524 | #region 868. 转置矩阵
2525 | public static int[][] Transpose(int[][] A)
2526 | {
2527 | if (A == null || A.Length <= 0)
2528 | return null;
2529 |
2530 | int w = A[0].Length;
2531 | int[][] res = new int[w][];
2532 | for (int i = 0; i < w; i++)
2533 | {
2534 | res[i] = new int[A.Length];
2535 | for (int j = 0; j < A.Length; j++)
2536 | {
2537 | res[i][j] = A[j][i];
2538 | }
2539 | }
2540 |
2541 | return res;
2542 | }
2543 | #endregion
2544 |
2545 | //https://leetcode-cn.com/contest/weekly-contest-94/problems/leaf-similar-trees/
2546 | #region 872. 叶子相似的树
2547 | public static bool LeafSimilar(TreeNode root1, TreeNode root2)
2548 | {
2549 | List leaves1 = new List();
2550 | GetLeaf(root1, leaves1);
2551 | List leaves2 = new List();
2552 | GetLeaf(root2, leaves2);
2553 |
2554 | if (leaves1.Count != leaves2.Count)
2555 | return false;
2556 |
2557 | for (int i = 0; i < leaves1.Count; i++)
2558 | if (leaves1[i] != leaves2[i])
2559 | return false;
2560 |
2561 | return true;
2562 | }
2563 |
2564 | private static void GetLeaf(TreeNode tree, List list)
2565 | {
2566 | if (tree.left == null && tree.right == null)
2567 | {
2568 | list.Add(tree.val);
2569 | return;
2570 | }
2571 | if (tree.left != null)
2572 | GetLeaf(tree.left, list);
2573 | if (tree.right != null)
2574 | GetLeaf(tree.right, list);
2575 | }
2576 |
2577 | #endregion
2578 |
2579 | //https://leetcode-cn.com/problems/middle-of-the-linked-list/description/
2580 | #region 876. 链表的中间结点
2581 | public static ListNode MiddleNode(ListNode head)
2582 | {
2583 | var slower = head;
2584 | var faster = head;
2585 |
2586 | while (faster != null && faster.next != null)
2587 | {
2588 | faster = faster.next.next;
2589 | slower = slower.next;
2590 | }
2591 |
2592 | return slower;
2593 | }
2594 | #endregion
2595 |
2596 | //https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/description/
2597 | #region 884. 两句话中的不常见单词
2598 | public static string[] UncommonFromSentences(string A, string B)
2599 | {
2600 | var a1 = A.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
2601 | var a2 = B.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
2602 | var s1 = RemoveDuplicate(a1);
2603 | var s2 = RemoveDuplicate(a2);
2604 | HashSet res = new HashSet();
2605 | RemoveDuplicate(s1, a2, res);
2606 | RemoveDuplicate(s2, a1, res);
2607 | return res.ToArray();
2608 | }
2609 |
2610 | private static HashSet RemoveDuplicate(string[] arr)
2611 | {
2612 | HashSet res = new HashSet();
2613 | HashSet duplicate = new HashSet();
2614 | foreach (var item in arr)
2615 | {
2616 | if (!duplicate.Contains(item))
2617 | {
2618 | if (!res.Add(item))
2619 | {
2620 | res.Remove(item);
2621 | duplicate.Add(item);
2622 | }
2623 | }
2624 | }
2625 | return res;
2626 | }
2627 |
2628 | private static void RemoveDuplicate(HashSet set, string[] arr, HashSet res)
2629 | {
2630 | HashSet s = new HashSet(arr);
2631 | foreach (var item in set)
2632 | if (!s.Contains(item))
2633 | res.Add(item);
2634 | }
2635 |
2636 | #endregion
2637 |
2638 | //https://leetcode-cn.com/problems/sort-array-by-parity/description/
2639 | #region 905. 按奇偶校验排序数组
2640 | public static int[] SortArrayByParity(int[] A)
2641 | {
2642 | int l = 0, r = A.Length - 1;
2643 | while (l < r)
2644 | {
2645 | int v = A[l];
2646 | if ((v & 1) == 1)
2647 | {
2648 | A[l] = A[r];
2649 | A[r] = v;
2650 | r--;
2651 | }
2652 | else
2653 | {
2654 | l++;
2655 | }
2656 | }
2657 | return A;
2658 | }
2659 | #endregion
2660 |
2661 | //https://leetcode-cn.com/problems/reverse-only-letters/description/
2662 | #region 917. 仅仅反转字母
2663 | public static string ReverseOnlyLetters(string S)
2664 | {
2665 | int l = 0, r = S.Length - 1;
2666 | var arr = S.ToCharArray();
2667 | while (l < r)
2668 | {
2669 | char lc = arr[l], rc = arr[r];
2670 | if (!char.IsLetter(lc))
2671 | {
2672 | l++;
2673 | }
2674 | else if (!char.IsLetter(rc))
2675 | {
2676 | r--;
2677 | }
2678 | else
2679 | {
2680 | arr[l] = rc;
2681 | arr[r] = lc;
2682 | l++;
2683 | r--;
2684 | }
2685 | }
2686 | return new string(arr);
2687 | }
2688 |
2689 | #endregion
2690 |
2691 | //https://leetcode-cn.com/problems/sort-array-by-parity-ii/description/
2692 | #region 922. 按奇偶排序数组 II
2693 | public static int[] SortArrayByParityII(int[] A)
2694 | {
2695 | int[] B = new int[A.Length];
2696 | int m = 0, n = 1;
2697 | for (int i = 0; i < A.Length; i++)
2698 | {
2699 | int x = A[i];
2700 | if ((x & 1) == 0)
2701 | {
2702 | B[m] = x;
2703 | m += 2;
2704 | }
2705 | else
2706 | {
2707 | B[n] = x;
2708 | n += 2;
2709 | }
2710 | }
2711 |
2712 | return B;
2713 | }
2714 | #endregion
2715 |
2716 | //https://leetcode-cn.com/problems/unique-email-addresses/description/
2717 | #region 929. 独特的电子邮件地址
2718 | public static int NumUniqueEmails(string[] emails)
2719 | {
2720 | HashSet mails = new HashSet();
2721 |
2722 | foreach (var mail in emails)
2723 | {
2724 | mails.Add(ParseEmail(mail));
2725 | }
2726 |
2727 | return mails.Count;
2728 | }
2729 |
2730 | private static string ParseEmail(string email)
2731 | {
2732 | List chars = new List(email.Length);
2733 | bool addAll = false;
2734 | bool skip = false;
2735 | for (int i = 0; i < email.Length; i++)
2736 | {
2737 | char c = email[i];
2738 | if (addAll)
2739 | {
2740 | chars.Add(c);
2741 | continue;
2742 | }
2743 |
2744 | if (c == '@')
2745 | {
2746 | addAll = true;
2747 | chars.Add(c);
2748 | continue;
2749 | }
2750 |
2751 | if (skip)
2752 | {
2753 | continue;
2754 | }
2755 |
2756 | if (c == '+')
2757 | {
2758 | skip = true;
2759 | continue;
2760 | }
2761 |
2762 | if (c != '.')
2763 | {
2764 | chars.Add(c);
2765 | }
2766 | }
2767 | return new string(chars.ToArray());
2768 | }
2769 | #endregion
2770 |
2771 | //https://leetcode.cn/problems/complement-of-base-10-integer/
2772 | #region 1009. 十进制整数的反码
2773 | public static int BitwiseComplement(int n)
2774 | {
2775 | if(n == 0)
2776 | {
2777 | return 1;
2778 | }
2779 | int l = 0;
2780 | int m = n;
2781 | while (m > 0)
2782 | {
2783 | m >>= 1;
2784 | l = (l << 1) | 1;
2785 | }
2786 | return l & (~n);
2787 | }
2788 | #endregion
2789 |
2790 | //https://leetcode-cn.com/problems/day-of-the-week/comments/
2791 | #region 1185. 一周中的第几天
2792 | ///
2793 | /// 蔡勒公式
2794 | /// https://zh.wikipedia.org/wiki/%E8%94%A1%E5%8B%92%E5%85%AC%E5%BC%8F
2795 | /// https://en.wikipedia.org/wiki/Zeller%27s_congruence
2796 | /// w:星期(计算所得的数值对应的星期:0 - 星期日;1 - 星期一;2 - 星期二;3 - 星期三;4 - 星期四;5 - 星期五;6 - 星期六)[注 1]
2797 | /// c:年份前两位数
2798 | /// y:年份后两位数
2799 | /// m:月(m的取值范围为3至14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
2800 | /// d:日
2801 | /// []:称作高斯符号,代表向下取整,即,取不大于原数的最大整数。
2802 | /// mod:同余(这里代表括号里的答案除以7后的余数)
2803 | ///
2804 | ///
2805 | ///
2806 | ///
2807 | ///
2808 | public static string DayOfTheWeek(int day, int month, int year)
2809 | {
2810 | string[] array = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
2811 | int m = month;
2812 | int y = year;
2813 | if (month <= 2)
2814 | {
2815 | m += 12;
2816 | y -= 1;
2817 | }
2818 | int c = y / 100;
2819 | y = y % 100;
2820 | int w = (y + y / 4 + c / 4 - 2 * c + (26 * (m + 1) / 10) + day - 1) % 7;
2821 | return array[(w + 7) % 7];
2822 | }
2823 | #endregion
2824 |
2825 | //https://leetcode.cn/problems/rearrange-spaces-between-words/
2826 | #region 1592. 重新排列单词间的空格
2827 | public static string ReorderSpaces(string text)
2828 | {
2829 | if (text.Length <= 1)
2830 | {
2831 | return text;
2832 | }
2833 | string[] array = text.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
2834 | int total = 0;
2835 | foreach (string s in array)
2836 | {
2837 | total += s.Length;
2838 | }
2839 |
2840 | int l = 0;
2841 | if (array.Length > 1)
2842 | {
2843 | l = (text.Length - total) / (array.Length - 1);
2844 | }
2845 |
2846 | string str = string.Join("".PadRight(l, ' '), array);
2847 | return str.PadRight(text.Length, ' ');
2848 | }
2849 | #endregion
2850 |
2851 | //https://leetcode.cn/problems/sum-of-digits-in-base-k/
2852 | #region 1837. K 进制表示下的各位数字总和
2853 | public static int SumBase(int n, int k)
2854 | {
2855 | int v = 0;
2856 | while (n > 0)
2857 | {
2858 | v += n % k;
2859 | n /= k;
2860 | }
2861 |
2862 | return v;
2863 | }
2864 | #endregion
2865 |
2866 | //https://leetcode.cn/problems/sorting-the-sentence/
2867 | #region 1859. 将句子排序
2868 | public static string SortSentence(string s)
2869 | {
2870 | List list = new List();
2871 | string[] sentence = new string[9];
2872 | for (int i = 0; i < s.Length; i++)
2873 | {
2874 | char c = s[i];
2875 | if (c != ' ')
2876 | {
2877 | if (!char.IsDigit(c))
2878 | {
2879 | list.Add(c);
2880 | }
2881 | else
2882 | {
2883 | sentence[c - '0' - 1] = new string(list.ToArray());
2884 | }
2885 | }
2886 | else
2887 | {
2888 | list.Clear();
2889 | }
2890 | }
2891 |
2892 | return string.Join(" ", sentence).Trim();
2893 | }
2894 | #endregion
2895 |
2896 | //https://leetcode.cn/problems/number-of-senior-citizens/description/
2897 | #region 2678. 老人的数目
2898 | public static int CountSeniors(string[] details)
2899 | {
2900 | int count = 0;
2901 | foreach (string detail in details)
2902 | {
2903 | char c1 = detail[11];
2904 | if (c1 > '6' && c1 <= '9')
2905 | {
2906 | count++;
2907 | }
2908 | else
2909 | {
2910 | if(c1 == '6')
2911 | {
2912 | char c2 = detail[12];
2913 | if (c2 > '0')
2914 | {
2915 | count++;
2916 | }
2917 | }
2918 | }
2919 | }
2920 |
2921 | return count;
2922 | }
2923 |
2924 |
2925 |
2926 | #endregion
2927 | }
2928 | }
2929 |
--------------------------------------------------------------------------------
/LeetCode/Hard.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace LeetCode
8 | {
9 | public static class Hard
10 | {
11 | //https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/
12 | #region 4. 两个排序数组的中位数
13 | public static double FindMedianSortedArrays(int[] nums1, int[] nums2)
14 | {
15 | int m = nums1.Length;
16 | int n = nums2.Length;
17 | if(m + n == 0)
18 | return 0;
19 |
20 | bool isOdd = (m + n) % 2 == 1;
21 | int i1 = 0, i2 = 0;
22 | int counter = 0;
23 | int end = isOdd ? (m + n) / 2 : (m + n) / 2 - 1;
24 |
25 | int[] longArray, shortArray;
26 | if(m > n)
27 | {
28 | longArray = nums1;
29 | shortArray = nums2;
30 | }
31 | else
32 | {
33 | longArray = nums2;
34 | shortArray = nums1;
35 | }
36 |
37 | if(m + n == 1)
38 | return longArray[0];
39 |
40 | while(true)
41 | {
42 | int n1 = longArray[i1];
43 | int n2;
44 | if(i2 < shortArray.Length)
45 | n2 = shortArray[i2];
46 | else
47 | n2 = longArray[i1 + 1];
48 |
49 | if(counter == end)
50 | {
51 | if(isOdd)
52 | return n1 > n2 ? n2 : n1;
53 |
54 | int t = int.MaxValue;
55 | if(n1 > n2 && i2 < shortArray.Length - 1)
56 | t = shortArray[i2 + 1];
57 | else if(n1 < n2 && i1 < longArray.Length - 1)
58 | t = longArray[i1 + 1];
59 |
60 | int t1 = n1 > t ? t : n1;
61 | int t2 = n2 > t ? t : n2;
62 | return (t1 + t2) / 2.0;
63 | }
64 | counter++;
65 | if(n1 > n2)
66 | i2++;
67 | else
68 | i1++;
69 | }
70 | }
71 | #endregion
72 |
73 | //https://leetcode-cn.com/problems/sudoku-solver/description/
74 | #region TODO: 37. 解数独
75 | public static void SolveSudoku(char[,] board)
76 | {
77 |
78 | }
79 | #endregion
80 |
81 | //https://leetcode-cn.com/problems/first-missing-positive/description/
82 | #region TODO: 41. 缺失的第一个正数
83 | public static int FirstMissingPositive(int[] nums)
84 | {
85 | if(nums.Length <= 0)
86 | return 1;
87 |
88 | int min = 0, max = 0;
89 |
90 | foreach(var n in nums)
91 | {
92 | if(n > max)
93 | max = n;
94 | else if(n == min + 1)
95 | min++;
96 | }
97 |
98 | return min == max ? max + 1 : min;
99 | }
100 | #endregion
101 |
102 | //https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/
103 | #region 145. 二叉树的后序遍历
104 | public static IList PostorderTraversal(TreeNode root)
105 | {
106 | IList res = new List();
107 | Traversal(root, res);
108 | return res;
109 | }
110 |
111 | private static void Traversal(TreeNode root, IList res)
112 | {
113 | if(root == null)
114 | return;
115 |
116 | Traversal(root.left, res);
117 | Traversal(root.right, res);
118 | res.Add(root.val);
119 | }
120 | #endregion
121 |
122 | //https://leetcode-cn.com/problems/number-of-digit-one/description/
123 | #region 233. 数字1的个数
124 | ///
125 | /// For each position, split the decimal representation into two parts,
126 | /// for example split n=3141592 into a=31415 and b=92 when we're at m=100 for analyzing the hundreds-digit.
127 | /// And then we know that the hundreds-digit of n is 1 for prefixes "" to "3141", i.e., 3142 times.
128 | /// Each of those times is a streak, though. Because it's the hundreds-digit,
129 | /// each streak is 100 long. So (a / 10 + 1) * 100 times, the hundreds-digit is 1.
130 | /// Consider the thousands-digit, i.e., when m = 1000.Then a=3141 and b = 592.
131 | /// The thousands-digit is 1 for prefixes "" to "314", so 315 times.
132 | /// And each time is a streak of 1000 numbers.However, since the thousands-digit is a 1,
133 | /// the very last streak isn't 1000 numbers but only 593 numbers,
134 | /// for the suffixes "000" to "592". So (a / 10 * 1000) + (b + 1) times, the thousands-digit is 1.
135 | /// The case distincton between the current digit/position being 0,
136 | /// 1 and >=2 can easily be done in one expression.With(a + 8) / 10 you get the number of full streaks,
137 | /// and a % 10 == 1 tells you whether to add a partial streak.
138 | /// See More: https://leetcode.com/problems/number-of-digit-one/discuss/64381/4+-lines-O(log-n)-C++JavaPython
139 | ///
140 | ///
141 | ///
142 | public static int CountDigitOne(int n)
143 | {
144 | long ones = 0;
145 | for(long m = 1; m <= n; m *= 10)
146 | ones += (n / m + 8) / 10 * m + (n / m % 10 == 1 ? n % m + 1 : 0);
147 | return (int)ones;
148 | }
149 | #endregion
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {4CAB5BB0-5CDF-4BD8-858B-5EB1B48B6FA9}
8 | Exe
9 | Properties
10 | LeetCode
11 | LeetCode
12 | v4.8.1
13 | 512
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
65 |
--------------------------------------------------------------------------------
/LeetCode/Medium.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace LeetCode
8 | {
9 | public static class Medium
10 | {
11 | //https://leetcode-cn.com/problems/add-two-numbers/description/
12 | #region 2. 两数相加
13 | public static ListNode AddTwoNumbers(ListNode l1, ListNode l2)
14 | {
15 | ListNode res = null;
16 | ListNode current = null;
17 | int carry = 0;
18 | while(l1 != null || l2 != null)
19 | {
20 | int n1 = 0, n2 = 0;
21 | if(l1 != null)
22 | {
23 | n1 = l1.val;
24 | l1 = l1.next;
25 | }
26 | if(l2 != null)
27 | {
28 | n2 = l2.val;
29 | l2 = l2.next;
30 | }
31 | int sum = n1 + n2 + carry;
32 | int val = sum % 10;
33 | carry = sum / 10;
34 |
35 | ListNode node = new ListNode(val);
36 | if(res == null)
37 | {
38 | res = node;
39 | current = res;
40 | }
41 | else
42 | {
43 | current.next = node;
44 | current = current.next;
45 | }
46 | }
47 | if(carry > 0)
48 | {
49 | current.next = new ListNode(carry);
50 | return res;
51 | }
52 |
53 | return res == null ? new ListNode(0) : res;
54 | }
55 | #endregion
56 |
57 | //https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
58 |
59 | #region 17. 电话号码的字母组合
60 | private static string[] PhoneNumbers = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
61 | public static IList LetterCombinations(string digits)
62 | {
63 | List result = new List();
64 | List letters = new List();
65 | for (int i = 0; i < digits.Length; i++)
66 | {
67 | string str = PhoneNumbers[digits[i] - '0'];
68 | if (!string.IsNullOrWhiteSpace(str))
69 | {
70 | letters.Add(str);
71 | }
72 | }
73 |
74 | if (letters.Count > 0)
75 | {
76 | Backtrack(0, new Stack(), letters, result);
77 | }
78 |
79 | return result;
80 | }
81 |
82 | private static void Backtrack(int index, Stack buffer, List letters, List result)
83 | {
84 | if (index == letters.Count)
85 | {
86 | result.Add(new string(buffer.Reverse().ToArray()));
87 | }
88 | else
89 | {
90 | string s = letters[index];
91 | foreach (char c in s)
92 | {
93 | buffer.Push(c);
94 | Backtrack(index + 1, buffer, letters, result);
95 | buffer.Pop();
96 | }
97 | }
98 | }
99 |
100 | #endregion
101 |
102 | //https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/
103 | #region 19. 删除链表的倒数第N个节点
104 | public static ListNode RemoveNthFromEnd(ListNode head, int n)
105 | {
106 | int index = 0;
107 | ListNode current = head;
108 | ListNode cache = head;
109 | ListNode cacheFast = head.next;
110 | while(current.next != null)
111 | {
112 | current = current.next;
113 | index++;
114 |
115 | if(index > n)
116 | {
117 | cache = cache.next;
118 | cacheFast = cache.next;
119 | }
120 | }
121 | if(n <= index)
122 | cache.next = cacheFast.next;
123 | else
124 | head = head.next;
125 |
126 | return head;
127 | }
128 | #endregion
129 |
130 | //https://leetcode-cn.com/problems/multiply-strings/description/
131 | #region 43. 字符串相乘
132 |
133 | private static char[] TrimMultiplyString(string num)
134 | {
135 | Stack nums = new Stack();
136 | bool isBegin = false;
137 | foreach (char c in num)
138 | {
139 | if (!isBegin)
140 | {
141 | if (c != '0')
142 | {
143 | nums.Push(c);
144 | isBegin = true;
145 | }
146 | }
147 | else
148 | {
149 | nums.Push(c);
150 | }
151 | }
152 |
153 | return nums.ToArray();
154 | }
155 |
156 | private static void InsertToList(List list, int index, char value)
157 | {
158 | while (list.Count <= index)
159 | {
160 | list.Add('0');
161 | }
162 |
163 | char v = (char) (list[index] + value);
164 | char carry = (char) ((v - '0') / 10);
165 | list[index] = (char)((v - '0') % 10 + '0');
166 | if (carry > 0)
167 | {
168 | InsertToList(list, index + 1, carry);
169 | }
170 | }
171 |
172 | public static string Multiply(string num1, string num2)
173 | {
174 | char[] nums1 = TrimMultiplyString(num1);
175 | char[] nums2 = TrimMultiplyString(num2);
176 | List result = new List();
177 |
178 | for (int i = 0; i < nums1.Length; i++)
179 | {
180 | int a = nums1[i] - '0';
181 | for (int j = 0; j < nums2.Length; j++)
182 | {
183 | int carry = 0;
184 | int b = nums2[j] - '0';
185 | int res = a * b;
186 | carry = res / 10;
187 | res %= 10;
188 | InsertToList(result, i + j, (char)res);
189 | InsertToList(result, i + j + 1, (char)carry);
190 | }
191 | }
192 |
193 | if (result.Count <= 0)
194 | {
195 | return "0";
196 | }
197 |
198 | List str = new List();
199 | bool isBegin = false;
200 | for (int i = result.Count - 1; i >= 0; i--)
201 | {
202 | char c = result[i];
203 | if (!isBegin)
204 | {
205 | if (c != '0')
206 | {
207 | isBegin = true;
208 | str.Add(c);
209 | }
210 | }
211 | else
212 | {
213 | str.Add(c);
214 | }
215 | }
216 | return new string(str.ToArray());
217 | }
218 | #endregion
219 |
220 | //https://leetcode-cn.com/problems/permutations/description/
221 | #region 46. 全排列
222 | public static IList> Permute(int[] nums)
223 | {
224 | IList> res = new List>();
225 |
226 | P(nums, 0, nums.Length - 1, res);
227 |
228 | return res;
229 | }
230 | private static void P(int[] nums, int start, int end, IList> res)
231 | {
232 | if(start != end)
233 | {
234 | for(int i = start; i <= end; i++)
235 | {
236 | Swap(nums, i, start);
237 | P(nums, start + 1, end, res);
238 | Swap(nums, i, start);
239 | }
240 | }
241 | else
242 | {
243 | IList l = new List();
244 | for(int i = 0; i <= end; i++)
245 | l.Add(nums[i]);
246 | res.Add(l);
247 | }
248 | }
249 | private static void Swap(int[] nums, int i, int j)
250 | {
251 | int t = nums[i];
252 | nums[i] = nums[j];
253 | nums[j] = t;
254 | }
255 | #endregion
256 |
257 | //https://leetcode-cn.com/problems/rotate-image/description/
258 | #region 48. 旋转图像
259 | public static void RotateImage(int[,] matrix)
260 | {
261 | int length = matrix.GetLength(0);
262 | for(int i = 0; i <= length / 2; i++)
263 | for(int j = i; j < length - i - 1; j++)
264 | RotateFrom(i, j, matrix, length - 1);
265 | }
266 | private static void RotateFrom(int x, int y, int[,] matrix, int length)
267 | {
268 | int t = matrix[x, y];
269 | matrix[x, y] = matrix[length - y, x];
270 | matrix[length - y, x] = matrix[length - x, length - y];
271 | matrix[length - x, length - y] = matrix[y, length - x];
272 | matrix[y, length - x] = t;
273 | }
274 | #endregion
275 |
276 | //https://leetcode-cn.com/problems/group-anagrams/description/
277 | #region 49. 字母异位词分组
278 | public static IList> GroupAnagrams(string[] strs)
279 | {
280 | Dictionary> map = new Dictionary>();
281 | foreach(var s in strs)
282 | {
283 | var arr = s.ToList();
284 | arr.Sort();
285 | string key = new string(arr.ToArray());
286 | if(!map.ContainsKey(key))
287 | map[key] = new List();
288 | map[key].Add(s);
289 | }
290 | return new List>(map.Values);
291 | }
292 | #endregion
293 |
294 | //https://leetcode-cn.com/problems/spiral-matrix/description/
295 | #region 54. 螺旋矩阵
296 | public static IList SpiralOrder(int[,] matrix)
297 | {
298 | int width = matrix.GetLength(1);
299 | int height = matrix.GetLength(0);
300 | int size = width * height;
301 |
302 | IList res = new List(size);
303 |
304 | int counter = 0;
305 | int round = 0;
306 | while(counter < size)
307 | {
308 | width--;
309 | height--;
310 | for(int i = round; i <= width; i++)
311 | {
312 | res.Add(matrix[round, i]);
313 | counter++;
314 | }
315 |
316 | for(int i = round + 1; i <= height; i++)
317 | {
318 | res.Add(matrix[i, width]);
319 | counter++;
320 | }
321 |
322 | for(int i = width - 1; i >= round && height > round; i--)
323 | {
324 | res.Add(matrix[height, i]);
325 | counter++;
326 | }
327 |
328 | for(int i = height - 1; i > round && width > round; i--)
329 | {
330 | res.Add(matrix[i, round]);
331 | counter++;
332 | }
333 |
334 | round++;
335 | }
336 |
337 | return res;
338 | }
339 | #endregion
340 |
341 | //https://leetcode.cn/problems/merge-intervals/
342 | #region 56. 合并区间
343 | public static int[][] Merge(int[][] intervals)
344 | {
345 | Array.Sort(intervals, (a, b) =>
346 | {
347 | return a[0] < b[0] ? -1 : 1;
348 | });
349 |
350 | List res = new List();
351 | int i = 0;
352 | while (i < intervals.Length - 1)
353 | {
354 | int[] current = intervals[i];
355 | int[] next = intervals[i + 1];
356 | if (current[1] >= next[0])
357 | {
358 | next[0] = current[0];
359 | if (current[1] > next[1])
360 | {
361 | next[1] = current[1];
362 | }
363 | }
364 | else
365 | {
366 | res.Add(current);
367 | }
368 | i++;
369 | }
370 | res.Add(intervals[intervals.Length - 1]);
371 | return res.ToArray();
372 | }
373 | #endregion
374 |
375 | //https://leetcode-cn.com/problems/spiral-matrix-ii/description/
376 | #region 59. 螺旋矩阵 II
377 | public static int[,] GenerateMatrix(int n)
378 | {
379 | int[,] arr = new int[n, n];
380 |
381 | int index = 1;
382 |
383 | int lt = 0, rt = n - 1, lb = 0, rb = n - 1;
384 |
385 | while(index <= n * n)
386 | {
387 | for(int i = lb; i <= rt; i++)
388 | arr[lt, i] = index++;
389 |
390 | lt++;
391 | for(int i = lt; i <= rb; i++)
392 | arr[i, rt] = index++;
393 |
394 | rb--;
395 | for(int i = rb; i >= lb; i--)
396 | arr[rt, i] = index++;
397 |
398 | rt--;
399 | for(int i = rt; i >= lt; i--)
400 | arr[i, lb] = index++;
401 |
402 | lb++;
403 | }
404 |
405 | return arr;
406 | }
407 | #endregion
408 |
409 | //https://leetcode-cn.com/problems/rotate-list/description/
410 | #region 61. 旋转链表
411 | public static ListNode RotateRight(ListNode head, int k)
412 | {
413 | if(k <= 0 || head == null)
414 | return head;
415 |
416 | int l = 1;
417 | var dummy = head;
418 | while(dummy.next != null)
419 | {
420 | dummy = dummy.next;
421 | l++;
422 | }
423 | dummy.next = head;
424 |
425 | k %= l;
426 | if(k > 0)
427 | for(int i = 0; i < l - k; i++)
428 | dummy = dummy.next;
429 | var newHead = dummy.next;
430 | dummy.next = null;
431 | return newHead;
432 | }
433 | #endregion
434 |
435 | //https://leetcode-cn.com/problems/set-matrix-zeroes/description/
436 | #region 73. 矩阵置零
437 | public static void SetZeroes(int[,] matrix)
438 | {
439 | int r = matrix.GetLength(0);
440 | int c = matrix.GetLength(1);
441 | int c0 = 1;
442 |
443 | for(int i = 0; i < r; i++)
444 | {
445 | if(matrix[i, 0] == 0)
446 | c0 = 0;
447 | for(int j = 1; j < c; j++)
448 | {
449 | if(matrix[i, j] == 0)
450 | {
451 | matrix[i, 0] = 0;
452 | matrix[0, j] = 0;
453 | }
454 | }
455 | }
456 | for(int i = r - 1; i >= 0; i--)
457 | {
458 | for(int j = c - 1; j >= 1; j--)
459 | {
460 | if(matrix[i, 0] == 0 || matrix[0, j] == 0)
461 | {
462 | matrix[i, j] = 0;
463 | }
464 | }
465 | if(c0 == 0)
466 | matrix[i, 0] = 0;
467 | }
468 |
469 | }
470 | #endregion
471 |
472 | //https://leetcode-cn.com/problems/search-a-2d-matrix/description/
473 | #region 74. 搜索二维矩阵
474 | ///
475 | /// ╔════════════════════════════════╗
476 | /// ║ MORE CLEANER CODE TO SEE NO.240 ║
477 | /// ║ Medium.SearchMatrixII ║
478 | /// ╚════════════════════════════════╝
479 | /// the core of this code is binary search.
480 | ///
481 | ///
482 | ///
483 | ///
484 | public static bool SearchMatrix(int[,] matrix, int target)
485 | {
486 | int row = matrix.GetLength(0);
487 | int column = matrix.GetLength(1);
488 |
489 | int i = -1;
490 | int l = 0, r = row - 1;
491 | if(column > 0)
492 | {
493 | while(l <= r)
494 | {
495 | i = (r + l) / 2;
496 | int n = matrix[i, 0];
497 | if(n == target)
498 | return true;
499 |
500 | if(n > target)
501 | {
502 | if(i == 0 || matrix[i - 1, 0] < target)
503 | {
504 | i--;
505 | break;
506 | }
507 | else
508 | r = i - 1;
509 | }
510 | else
511 | {
512 | if(i == row - 1 || matrix[i + 1, 0] > target)
513 | break;
514 | else
515 | l = i + 1;
516 | }
517 | }
518 | }
519 | int j = -1;
520 | l = 0;
521 | r = column - 1;
522 | if(i >= 0)
523 | {
524 | while(l <= r)
525 | {
526 | j = (r + l) / 2;
527 | int n = matrix[i, j];
528 | if(n == target)
529 | return true;
530 |
531 | if(n > target)
532 | r = j - 1;
533 | else
534 | l = j + 1;
535 | }
536 | }
537 |
538 | return i > 0 && j > 0 && matrix[i, j] == target;
539 | }
540 | #endregion
541 |
542 | //https://leetcode-cn.com/problems/sort-colors/description/
543 | #region 75. 分类颜色
544 | public static void SortColors(int[] nums)
545 | {
546 | QuickSort(nums);
547 | }
548 |
549 | public static void QuickSort(int[] nums)
550 | {
551 | Sort(nums, 0, nums.Length - 1);
552 | }
553 | private static void Sort(int[] nums, int left, int right)
554 | {
555 | if(left >= right)
556 | return;
557 |
558 | int index = Partition(nums, left, right);
559 | Sort(nums, left, index - 1);
560 | Sort(nums, index + 1, right);
561 | }
562 | private static int Partition(int[] nums, int left, int right)
563 | {
564 | int center = left;
565 | int pivot = nums[right];
566 |
567 | for(int i = left; i < right; i++)
568 | {
569 | if(nums[i] <= pivot)
570 | {
571 | Swap(nums, i, center++);
572 | }
573 | }
574 | Swap(nums, center, right);
575 |
576 | return center;
577 | }
578 | //private static void Swap(int[] nums, int i, int j)
579 | //{
580 | // if(i == j)
581 | // return;
582 |
583 | // int t = nums[i];
584 | // nums[i] = nums[j];
585 | // nums[j] = t;
586 | //}
587 | #endregion
588 |
589 | //https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/description/
590 | #region 82. 删除排序链表中的重复元素 II
591 | public static ListNode DeleteDuplicates(ListNode head)
592 | {
593 | if(head == null)
594 | return null;
595 |
596 | ListNode dummy = new ListNode(head.val - 1);
597 | dummy.next = head;
598 | ListNode node = head;
599 | ListNode nextNode = head.next;
600 | ListNode preNode = dummy;
601 | while(nextNode != null)
602 | {
603 | if(node.val != nextNode.val)
604 | {
605 | preNode = node;
606 | node = node.next;
607 | nextNode = nextNode.next;
608 | }
609 | else
610 | {
611 | while(nextNode != null && node.val == nextNode.val)
612 | {
613 | node = node.next;
614 | nextNode = nextNode.next;
615 | }
616 |
617 | preNode.next = nextNode;
618 | node = nextNode;
619 | nextNode = nextNode != null ? nextNode.next : null;
620 | }
621 | }
622 |
623 | return dummy.next;
624 | }
625 | #endregion
626 |
627 | //https://leetcode-cn.com/problems/partition-list/description/
628 | #region 86. 分隔链表
629 | public static ListNode Partition(ListNode head, int x)
630 | {
631 | ListNode biggerHead = new ListNode(0);
632 | ListNode smallerHead = new ListNode(0);
633 | ListNode smaller = smallerHead;
634 | ListNode bigger = biggerHead;
635 | while(head != null)
636 | {
637 | if(head.val < x)
638 | {
639 | smaller.next = head;
640 | smaller = head;
641 | }
642 | else
643 | {
644 | bigger.next = head;
645 | bigger = head;
646 | }
647 | head = head.next;
648 | }
649 | bigger.next = null; // avoid cycle in linked list
650 | smaller.next = biggerHead.next;
651 | return smallerHead.next;
652 | }
653 | #endregion
654 |
655 | //https://leetcode-cn.com/problems/reverse-linked-list-ii/description/
656 | #region 92. 反转链表 II
657 | public static ListNode ReverseBetween(ListNode head, int m, int n)
658 | {
659 | if(m == n)
660 | return head;
661 |
662 | n -= m;
663 | ListNode dummy = new ListNode(0);
664 | dummy.next = head;
665 |
666 | ListNode p1 = dummy;
667 | while(--m > 0)
668 | p1 = p1.next;
669 | ListNode p2 = p1.next;
670 | while(n-- > 0)
671 | {
672 | ListNode p = p2.next;
673 | p2.next = p.next;
674 | p.next = p1.next;
675 | p1.next = p;
676 | }
677 |
678 | return dummy.next;
679 | }
680 | #endregion
681 |
682 | //https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/
683 | #region 94. 二叉树的中序遍历
684 | public static IList InorderTraversal(TreeNode root)
685 | {
686 | IList res = new List();
687 | Traversal2(root, res);
688 | return res;
689 | }
690 | private static void Traversal2(TreeNode root, IList res)
691 | {
692 | if(root == null)
693 | return;
694 |
695 | Traversal2(root.left, res);
696 | res.Add(root.val);
697 | Traversal2(root.right, res);
698 | }
699 | #endregion
700 |
701 | //https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/
702 | #region 114. 二叉树展开为链表
703 | private static TreeNode prev = null;
704 | public static void Flatten(TreeNode root)
705 | {
706 | if(root == null)
707 | return;
708 | Flatten(root.right);
709 | Flatten(root.left);
710 | root.right = prev;
711 | root.left = null;
712 | prev = root;
713 | }
714 | #endregion
715 |
716 | //https://leetcode-cn.com/problems/word-break/description/
717 | #region TODO: 139. 单词拆分
718 | public static bool WordBreak(string s, IList wordDict)
719 | {
720 | throw new Exception("TODO:WordBreak");
721 | }
722 |
723 | #endregion
724 |
725 | //https://leetcode-cn.com/problems/linked-list-cycle-ii/description/
726 | #region 142. 环形链表 II
727 | public static ListNode DetectCycle(ListNode head)
728 | {
729 | if(head == null)
730 | return null;
731 |
732 | var walker = head;
733 | var runner = head;
734 |
735 | ListNode point = null;
736 | while(runner.next != null && runner.next.next != null)
737 | {
738 | walker = walker.next;
739 | runner = runner.next.next;
740 | if(walker == runner)
741 | {
742 | point = walker;
743 | break;
744 | }
745 | }
746 |
747 | if(point != null)
748 | {
749 | ListNode entrance = head;
750 | while(point.next != null)
751 | {
752 | if(point == entrance)
753 | return entrance;
754 |
755 | point = point.next;
756 | entrance = entrance.next;
757 | }
758 |
759 | }
760 | return null;
761 | }
762 | #endregion
763 |
764 | // https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/
765 | #region 144. 二叉树的前序遍历
766 | public static IList PreorderTraversal(TreeNode root)
767 | {
768 | IList res = new List();
769 | Traversal(root, res);
770 | return res;
771 | }
772 |
773 | private static void Traversal(TreeNode root, IList res)
774 | {
775 | if(root == null)
776 | return;
777 |
778 | res.Add(root.val);
779 | Traversal(root.left, res);
780 | Traversal(root.right, res);
781 | }
782 | #endregion
783 |
784 | //https://leetcode-cn.com/problems/compare-version-numbers/description/
785 | #region 165. 比较版本号
786 | public static int CompareVersion(string version1, string version2)
787 | {
788 | var v1 = Split(version1);
789 | var v2 = Split(version2);
790 |
791 | int i = 0, j = 0;
792 | while(i < v1.Count || j < v2.Count)
793 | {
794 | int m = i < v1.Count ? v1[i] : 0;
795 | int n = j < v2.Count ? v2[j] : 0;
796 | if(m > n)
797 | return 1;
798 | else if(m < n)
799 | return -1;
800 |
801 | i++;
802 | j++;
803 | }
804 |
805 | return 0;
806 | }
807 |
808 | private static IList Split(string s)
809 | {
810 | IList res = new List();
811 | int i = 0;
812 | while(i < s.Length)
813 | {
814 | char c = s[i];
815 | if(c != '.')
816 | {
817 | int n = 0;
818 | while(c != '.')
819 | {
820 | n = (c - '0') + n * 10;
821 | i++;
822 | if(i >= s.Length)
823 | break;
824 | c = s[i];
825 | }
826 | res.Add(n);
827 | }
828 | i++;
829 | }
830 | return res;
831 | }
832 | #endregion
833 |
834 | //https://leetcode-cn.com/problems/number-of-islands/description/
835 | #region TODO: 200. 岛屿的个数
836 | public static int NumIslands(char[,] grid)
837 | {
838 | for(int i = 0; i < grid.GetLength(0); i++)
839 | {
840 | for(int j = 0; j < grid.GetLength(1); j++)
841 | {
842 |
843 | }
844 | }
845 |
846 |
847 | return 0;
848 | }
849 | #endregion
850 |
851 | //https://leetcode-cn.com/problems/basic-calculator-ii/description/
852 | #region 227. 基本计算器II
853 | public static int Calculate(string s)
854 | {
855 | if(string.IsNullOrWhiteSpace(s))
856 | return 0;
857 |
858 | s += "+0";
859 | Stack nums = new Stack();
860 |
861 | int n = 0;
862 | char symbol = '+';
863 | for(int i = 0; i < s.Length; i++)
864 | {
865 | char c = s[i];
866 | if(c == ' ')
867 | continue;
868 |
869 | if(char.IsDigit(c))
870 | n = (c - '0') + n * 10;
871 | else
872 | {
873 | if(symbol == '+')
874 | nums.Push(n);
875 | else if(symbol == '-')
876 | nums.Push(-n);
877 | else if(symbol == '*')
878 | nums.Push(nums.Pop() * n);
879 | else if(symbol == '/')
880 | nums.Push(nums.Pop() / n);
881 | n = 0;
882 | symbol = c;
883 | }
884 | }
885 |
886 | int res = 0;
887 | foreach(var num in nums)
888 | res += num;
889 |
890 | return res;
891 | }
892 | #endregion
893 |
894 | //https://leetcode-cn.com/problems/product-of-array-except-self/description/
895 | #region 238. 除自身以外数组的乘积
896 | public static int[] ProductExceptSelf(int[] nums)
897 | {
898 | int[] result = new int[nums.Length];
899 | // t is one step slower than i, first loop t's purpose is calculate [0,i-1]'s product, and cache in result
900 | int t = 1;
901 | for(int i = 0; i < nums.Length; i++)
902 | {
903 | result[i] = t;
904 | t *= nums[i];
905 | }
906 | // second loop, reverse sequence cycle nums, t's purpose is calculate [nums.Length,i-1]'s product, and product the cache in result[i]
907 | t = 1;
908 | for(int i = nums.Length - 1; i >= 0; i--)
909 | {
910 | result[i] *= t;
911 | t *= nums[i];
912 | }
913 | // this two loop, t is all one step slower than index i, so never product self.
914 | return result;
915 | }
916 | #endregion
917 |
918 | //https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/
919 | #region 240. 搜索二维矩阵 II
920 | public static bool SearchMatrixII(int[,] matrix, int target)
921 | {
922 | int i = 0, j = matrix.GetLength(1) - 1;
923 | int row = matrix.GetLength(0);
924 | while(i < row && j >= 0)
925 | {
926 | int n = matrix[i, j];
927 | if(n == target)
928 | return true;
929 |
930 | if(n > target)
931 | j--;
932 | else
933 | i++;
934 | }
935 |
936 | return false;
937 | }
938 | #endregion
939 |
940 | //https://leetcode-cn.com/problems/find-the-duplicate-number/description/
941 | #region 287. 寻找重复数
942 | public static int FindDuplicate(int[] nums)
943 | {
944 | if(nums.Length > 1)
945 | {
946 | int slow = nums[0];
947 | int fast = nums[nums[0]];
948 | while(slow != fast)
949 | {
950 | slow = nums[slow];
951 | fast = nums[nums[fast]];
952 | }
953 |
954 | fast = 0;
955 | while(fast != slow)
956 | {
957 | fast = nums[fast];
958 | slow = nums[slow];
959 | }
960 | return slow;
961 | }
962 | return -1;
963 | //int res = 0;
964 | //for(int i = 0; i < nums.Length; i++)
965 | //{
966 | // res ^= nums[i];
967 | // res ^= i;
968 | //}
969 | //return res;
970 | //--------------------------------------------------------
971 | //int[] arr = new int[nums.Length];
972 | //for(int i = 0; i < nums.Length; i++)
973 | //{
974 | // int t = nums[i];
975 | // if(arr[t] == 0)
976 | // arr[t] = nums[i];
977 | // else
978 | // arr[t] = -1;
979 | //}
980 |
981 | //for(int i = 0; i < arr.Length; i++)
982 | //{
983 | // if(arr[i] == -1)
984 | // return i;
985 | //}
986 | //return 0;
987 | }
988 | #endregion
989 |
990 | //https://leetcode-cn.com/problems/bulb-switcher/description/
991 | #region 319. 灯泡开关
992 | public static int BulbSwitch(int n)
993 | {
994 | return (int)Math.Sqrt(n);
995 | }
996 | #endregion
997 |
998 | //https://leetcode-cn.com/problems/odd-even-linked-list/description/
999 | #region 328. 奇偶链表
1000 | public static ListNode OddEvenList(ListNode head)
1001 | {
1002 | if(head == null)
1003 | return null;
1004 | ListNode odd = head;
1005 | ListNode even = head.next;
1006 | ListNode evenHead = even;
1007 | while(even != null && even.next != null)
1008 | {
1009 | odd.next = odd.next.next;
1010 | even.next = even.next.next;
1011 | odd = odd.next;
1012 | even = even.next;
1013 | }
1014 | odd.next = evenHead;
1015 | return head;
1016 | }
1017 | #endregion
1018 |
1019 | //https://leetcode-cn.com/problems/counting-bits/description/
1020 | #region 338. Bit位计数
1021 | public static int[] CountBits(int num)
1022 | {
1023 | int[] res = new int[num + 1];
1024 |
1025 | for(int i = 1; i <= num; i++)
1026 | res[i] = CountBit(i);
1027 |
1028 | return res;
1029 | }
1030 | private static int CountBit(int n)
1031 | {
1032 | int i = 0;
1033 | while(n > 0)
1034 | {
1035 | n &= n - 1;
1036 | i++;
1037 | }
1038 | return i;
1039 | }
1040 | #endregion
1041 |
1042 | //https://leetcode-cn.com/problems/utf-8-validation/description/
1043 | #region 393. UTF-8 编码验证
1044 | public static bool ValidUtf8(int[] data)
1045 | {
1046 | int count = 0;
1047 | foreach(var d in data)
1048 | {
1049 | if(count == 0)
1050 | {
1051 | if(d >> 5 == 6)
1052 | count = 1;
1053 | else if(d >> 4 == 14)
1054 | count = 2;
1055 | else if(d >> 3 == 30)
1056 | count = 3;
1057 | else if(d >> 7 != 0)
1058 | return false;
1059 | }
1060 | else
1061 | {
1062 | if(d >> 6 == 2)
1063 | count--;
1064 | else
1065 | return false;
1066 | }
1067 | }
1068 |
1069 | return count == 0;
1070 | }
1071 | #endregion
1072 |
1073 | //https://leetcode-cn.com/problems/decode-string/description/
1074 | #region 394. 字符串解码
1075 | public static string DecodeString(string s)
1076 | {
1077 | int k = 0;
1078 | StringBuilder sb = new StringBuilder();
1079 | for(int i = 0; i < s.Length; i++)
1080 | {
1081 | if(s[i] >= '0' && s[i] <= '9')
1082 | {
1083 | k = k * 10 + (s[i] - '0');
1084 | continue;
1085 | }
1086 | else if(s[i] == '[')
1087 | {
1088 | int c = 1;
1089 | int j = i + 1;
1090 | while(c > 0)
1091 | {
1092 | if(s[j] == '[')
1093 | c++;
1094 | else if(s[j] == ']')
1095 | c--;
1096 | j++;
1097 | }
1098 | string str = DecodeString(s.Substring(i + 1, j - i - 2));
1099 | for(int m = 0; m < k; m++)
1100 | {
1101 | sb.Append(str);
1102 | }
1103 | i = j - 1;
1104 | k = 0;
1105 | }
1106 | else
1107 | sb.Append(s[i]);
1108 | }
1109 |
1110 | return sb.ToString();
1111 | }
1112 | #endregion
1113 |
1114 | //https://leetcode-cn.com/problems/rotate-function/description/
1115 | #region 396. 旋转函数
1116 | ///
1117 | /// idea copy from https://leetcode.com/problems/rotate-function/discuss/87853/Java-O(n)-solution-with-explanation
1118 | ///
1119 | ///
1120 | ///
1121 | public static int MaxRotateFunction(int[] A)
1122 | {
1123 | //if(A.Length <= 0)
1124 | // return 0;
1125 |
1126 | //int res = int.MinValue;
1127 | //for (int i = 0; i < A.Length; i++)
1128 | //{
1129 | // int r = 0;
1130 | // int k = 0;
1131 | // while (k < A.Length)
1132 | // {
1133 | // r += A[(i + k) % A.Length] * k;
1134 | // k++;
1135 | // }
1136 | // res = Math.Max(r, res);
1137 | //}
1138 |
1139 | //return res;
1140 | int allSum = 0;
1141 | int len = A.Length;
1142 | int F = 0;
1143 | for(int i = 0; i < len; i++)
1144 | {
1145 | F += i * A[i];
1146 | allSum += A[i];
1147 | }
1148 | int max = F;
1149 | for(int i = len - 1; i >= 1; i--)
1150 | {
1151 | F = F + allSum - len * A[i];
1152 | max = Math.Max(F, max);
1153 | }
1154 | return max;
1155 | }
1156 | #endregion
1157 |
1158 | //https://leetcode-cn.com/problems/integer-replacement/description/
1159 | #region 397. 整数替换
1160 | ///
1161 | /// obviously, even number is more likeable than odd number, because even number we can divide 2 direct
1162 | /// but odd number we need turn it to even number(plus 1 or subtract 1).
1163 | /// so we need to decide whether to add 1 or subtract 1 when is an odd number.
1164 | /// in binary,
1165 | /// * 1011 -> 1100 is effective than 1011 -> 1010,
1166 | /// because 1100 >> 1 (equal to divide 2) we can get an even number again,
1167 | /// but 1010 >> 1 we get an odd number and need turn it to even number again.
1168 | /// * 1101 -> 1100 is efectie than 1101 -> 1110, same reason as above.
1169 | /// so we can see the pattern, if penultimate bit is 1 then plus 1 else minus 1 (if x & 0B10 = 0 => x-- else m++).
1170 | /// it should be noted that, 3(0B11) is a speciall number, because 11 -> 10 -> 1 ,but 11 -> 100 -> 10 -> 1.
1171 | ///
1172 | ///
1173 | ///
1174 | public static int IntegerReplacement(int n)
1175 | {
1176 | int c = 0;
1177 | ulong m = (ulong)n;
1178 | while(m != 1)
1179 | {
1180 | if((m & 1) == 0)
1181 | m >>= 1;
1182 | else if(m == 3 || (m & 2) == 0)
1183 | m--;
1184 | else
1185 | m++;
1186 | c++;
1187 | }
1188 | return c;
1189 | }
1190 | #endregion
1191 |
1192 | //https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/description/
1193 | #region 423. 从英文中重建数字
1194 | public static string OriginalDigits(string s)
1195 | {
1196 | char[] letters = new char['z' + 1];
1197 | for (int i = 0; i < s.Length; i++)
1198 | {
1199 | letters[s[i]]++;
1200 | }
1201 |
1202 | int[] counts = new int[10];
1203 | counts[0] = letters['z'];
1204 | counts[2] = letters['w'];
1205 | counts[4] = letters['u'];
1206 | counts[6] = letters['x'];
1207 | counts[8] = letters['g'];
1208 | counts[5] = letters['f'] - counts[4];
1209 | counts[7] = letters['v'] - counts[5];
1210 | counts[3] = letters['r'] - counts[0] - counts[4];
1211 | counts[1] = letters['o'] - counts[0] - counts[2] - counts[4];
1212 | counts[9] = letters['i'] - counts[5] - counts[6] - counts[8];
1213 |
1214 | char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
1215 | int l = counts[0] + counts[1] + counts[2] + counts[3] + counts[4] + counts[5] + counts[6] + counts[7] + counts[8] + counts[9];
1216 | char[] res = new char[l];
1217 | int k = 0;
1218 | for (int i = 0; i < counts.Length; i++)
1219 | {
1220 | char v = chars[i];
1221 | for (int j = 0; j < counts[i]; j++)
1222 | {
1223 | res[k++] = v;
1224 | }
1225 | }
1226 |
1227 | return new string(res);
1228 | }
1229 | #endregion
1230 |
1231 | //https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/
1232 | #region 442. 数组中重复的数据
1233 | public static IList FindDuplicates(int[] nums)
1234 | {
1235 | List res = new List();
1236 | for(int i = 0; i < nums.Length; i++)
1237 | {
1238 | int index = Math.Abs(nums[i]) - 1;
1239 | if(nums[index] < 0)
1240 | res.Add(Math.Abs(index + 1));
1241 | nums[index] *= -1;
1242 | }
1243 | return res;
1244 | }
1245 | #endregion
1246 |
1247 | //https://leetcode.com/problems/132-pattern/description/
1248 | #region 456. 132 Pattern
1249 | public static bool Find132pattern(int[] nums)
1250 | {
1251 | int p3 = int.MinValue;
1252 | Stack stack = new Stack();
1253 | for(int i = nums.Length - 1; i >= 0; i--)
1254 | {
1255 | if(nums[i] < p3)
1256 | {
1257 | return true;
1258 | }
1259 | else
1260 | {
1261 | while(stack.Count > 0 && nums[i] > stack.Peek())
1262 | {
1263 | p3 = stack.Pop();
1264 | }
1265 | }
1266 | stack.Push(nums[i]);
1267 | }
1268 |
1269 | return false;
1270 | }
1271 | #endregion
1272 |
1273 | //https://leetcode-cn.com/problems/total-hamming-distance/description/
1274 | #region 477. 汉明距离总和
1275 | public static int TotalHammingDistance(int[] nums)
1276 | {
1277 | int total = 0, length = nums.Length;
1278 | for(int j = 0; j < 32; j++)
1279 | {
1280 | int bitCount = 0;
1281 | for(int i = 0; i < length; i++)
1282 | bitCount += (nums[i] >> j) & 1;
1283 | total += bitCount * (length - bitCount);
1284 | }
1285 | return total;
1286 | }
1287 | #endregion
1288 |
1289 | //https://leetcode-cn.com/problems/validate-ip-address/description/
1290 | #region 468. 验证IP地址
1291 | public static string ValidIPAddress(string IP)
1292 | {
1293 | if(IP.Length <= 0)
1294 | return "Neither";
1295 |
1296 | if(IsIPV4(IP))
1297 | return "IPv4";
1298 |
1299 | if(IsIPV6(IP))
1300 | return "IPv6";
1301 |
1302 | return "Neither";
1303 | }
1304 |
1305 | private static bool IsIPV4(string IP)
1306 | {
1307 | int v = 0;
1308 | bool isDot = true;
1309 | int counter = 0;
1310 | int l = 0;
1311 | for(int i = 0; i < IP.Length; i++)
1312 | {
1313 | char c = IP[i];
1314 | if(isDot)
1315 | {
1316 | if(c == '.')
1317 | return false;
1318 | isDot = false;
1319 | }
1320 | if(char.IsNumber(c))
1321 | {
1322 | if(l > 0)
1323 | {
1324 | if(v == 0)
1325 | return false;
1326 | }
1327 | v = v * 10 + c - '0';
1328 | l++;
1329 | if(v > 255)
1330 | return false;
1331 | }
1332 | else if(c == '.')
1333 | {
1334 | l = 0;
1335 | v = 0;
1336 | isDot = true;
1337 | counter++;
1338 | }
1339 | else
1340 | return false;
1341 | }
1342 | return !isDot && counter == 3;
1343 | }
1344 |
1345 | private static bool IsIPV6(string IP)
1346 | {
1347 | bool isColon = true;
1348 | int counter = 0;
1349 | int l = 0;
1350 | for(int i = 0; i < IP.Length; i++)
1351 | {
1352 | char c = IP[i];
1353 | if(isColon)
1354 | {
1355 | if(c == ':')
1356 | return false;
1357 |
1358 | isColon = false;
1359 | }
1360 | if(c == ':')
1361 | {
1362 | isColon = true;
1363 | counter++;
1364 | l = 0;
1365 | }
1366 | else
1367 | {
1368 | if(!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
1369 | return false;
1370 | if(l >= 4)
1371 | return false;
1372 | l++;
1373 | }
1374 | }
1375 |
1376 | return !isColon && counter == 7;
1377 | }
1378 | #endregion
1379 |
1380 | //https://leetcode-cn.com/problems/complex-number-multiplication/description/
1381 | #region 537. 复数乘法
1382 | public static string ComplexNumberMultiply(string a, string b)
1383 | {
1384 | int i, j;
1385 | int m, n;
1386 | Parse(a, out i, out j);
1387 | Parse(b, out m, out n);
1388 | int real = i * m - j * n;
1389 | int imaginary = j * m + i * n;
1390 | return $"{real}+{imaginary}i";
1391 | }
1392 | private static void Parse(string input, out int a, out int b)
1393 | {
1394 | var arr = input.Split('+');
1395 | a = int.Parse(arr[0]);
1396 | b = int.Parse(arr[1].Substring(0, arr[1].Length - 1));
1397 | }
1398 | #endregion
1399 |
1400 | //https://leetcode-cn.com/problems/single-element-in-a-sorted-array/description/
1401 | #region 540. 有序数组中的单一元素
1402 | public static int SingleNonDuplicate(int[] nums)
1403 | {
1404 | for(int i = 0; i < nums.Length; i += 2)
1405 | {
1406 | if(i + 1 >= nums.Length)
1407 | return nums[i];
1408 | if(nums[i] != nums[i + 1])
1409 | return nums[i];
1410 | }
1411 | return 0;
1412 | }
1413 | #endregion
1414 |
1415 | //https://leetcode-cn.com/problems/daily-temperatures/description/
1416 | #region 739. 每日温度
1417 | public static int[] DailyTemperatures(int[] temperatures)
1418 | {
1419 | Stack stack = new Stack();
1420 | int[] ret = new int[temperatures.Length];
1421 | for(int i = 0; i < temperatures.Length; i++)
1422 | {
1423 | while(stack.Count > 0 && temperatures[i] > temperatures[stack.Peek()])
1424 | {
1425 | int index = stack.Pop();
1426 | ret[index] = i - index;
1427 | }
1428 | stack.Push(i);
1429 | }
1430 | return ret;
1431 | }
1432 | //public static int[] DailyTemperatures(int[] temperatures)
1433 | //{
1434 | // int[] res = new int[temperatures.Length];
1435 | // for(int i = 0; i < temperatures.Length; i++)
1436 | // {
1437 | // res[i] = GetDay(temperatures, i);
1438 | // }
1439 |
1440 | // return res;
1441 | //}
1442 |
1443 | //private static int GetDay(int[] temperatures, int start)
1444 | //{
1445 | // int v = temperatures[start];
1446 | // for(int i = start + 1; i < temperatures.Length; i++)
1447 | // if(temperatures[i] > v)
1448 | // return i - start;
1449 | // return 0;
1450 | //}
1451 |
1452 | #endregion
1453 |
1454 | //https://leetcode-cn.com/problems/reorganize-string/description/
1455 | #region TODO: 767. 重构字符串
1456 | public static string ReorganizeString(string S)
1457 | {
1458 | var arr = S.ToArray();
1459 | int i = 0, j = 1;
1460 | while(j < arr.Length)
1461 | {
1462 | if(arr[i] != arr[j])
1463 | {
1464 | i++;
1465 | j++;
1466 | }
1467 | else
1468 | {
1469 | int k = j + 1;
1470 | while(true)
1471 | {
1472 | if(k == arr.Length)
1473 | return string.Empty;
1474 |
1475 | if(arr[k] != arr[i])
1476 | {
1477 | var t = arr[k];
1478 | arr[k] = arr[j];
1479 | arr[j] = t;
1480 | i++;
1481 | j++;
1482 | break;
1483 | }
1484 | k++;
1485 | }
1486 | }
1487 | }
1488 |
1489 | if(i == j - 1)
1490 | return new string(arr);
1491 | else
1492 | return string.Empty;
1493 | }
1494 |
1495 | #endregion
1496 |
1497 | //https://leetcode-cn.com/problems/custom-sort-string/description/
1498 | #region 791. 自定义字符串排序
1499 | public static string CustomSortString(string S, string T)
1500 | {
1501 | int[] priority = new int['z' + 1];
1502 | for(int i = 0; i < S.Length; i++)
1503 | priority[S[i]] = i + 1;
1504 |
1505 | char[] res = T.ToCharArray();
1506 |
1507 | for(int i = 0; i < res.Length; i++)
1508 | {
1509 | for(int j = i; j < res.Length; j++)
1510 | {
1511 | if(priority[res[j]] < priority[res[i]])
1512 | {
1513 | char t = res[i];
1514 | res[i] = res[j];
1515 | res[j] = t;
1516 | }
1517 | }
1518 | }
1519 |
1520 | return new string(res);
1521 | }
1522 | #endregion
1523 |
1524 | //https://leetcode-cn.com/contest/weekly-contest-91/problems/all-nodes-distance-k-in-binary-tree/
1525 | #region TODO: 863. 二叉树中所有距离为 K 的结点
1526 | public static IList DistanceK(TreeNode root, TreeNode target, int K)
1527 | {
1528 |
1529 | return null;
1530 | }
1531 |
1532 | #endregion
1533 |
1534 | //https://leetcode-cn.com/contest/weekly-contest-96/problems/decoded-string-at-index/
1535 | #region 884. 索引处的解码字符串
1536 | public static string DecodeAtIndex(string S, int K)
1537 | {
1538 | int i = 0;
1539 | while(i < S.Length)
1540 | {
1541 | char c = S[i];
1542 | if(c <= '9' && c >= '0')
1543 | {
1544 | if(i >= K)
1545 | {
1546 | return new string(new char[] { S[K - 1] });
1547 | }
1548 | else
1549 | {
1550 | char[] arr = new char[(S.Length - i - 1) + i * (c - '0')];
1551 | int j = 0;
1552 | for(j = 0; j < c - '0'; j++)
1553 | {
1554 | for(int k = 0; k < i; k++)
1555 | {
1556 | arr[j * i + k] = S[k];
1557 | }
1558 | }
1559 |
1560 | for(j = i * (c - '0'); i < S.Length - 1; j++)
1561 | {
1562 | arr[j] = S[++i];
1563 | }
1564 | return DecodeAtIndex(new string(arr), K);
1565 | }
1566 | }
1567 | i++;
1568 | }
1569 | return new string(new char[] { S[K - 1] });
1570 | }
1571 | #endregion
1572 | }
1573 | }
1574 |
--------------------------------------------------------------------------------
/LeetCode/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace LeetCode
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 |
14 |
15 |
16 | Console.WriteLine();
17 | Console.ReadLine();
18 | }
19 | }
20 |
21 | public class ListNode
22 | {
23 | public int val;
24 | public ListNode next;
25 | public ListNode(int x) { val = x; }
26 |
27 | public ListNode(int[] arr)
28 | {
29 | val = arr[0];
30 | int i = 1;
31 | ListNode current = this;
32 | while(i < arr.Length)
33 | {
34 | current.next = new ListNode(arr[i]);
35 | current = current.next;
36 | i++;
37 | }
38 | }
39 |
40 | public override string ToString()
41 | {
42 | StringBuilder sb = new StringBuilder();
43 |
44 | var l = this;
45 | while(true)
46 | {
47 | sb.Append(l.val);
48 | if(l.next != null)
49 | {
50 | sb.Append(" --> ");
51 | l = l.next;
52 | }
53 | else
54 | {
55 | break;
56 | }
57 | }
58 |
59 | return sb.ToString();
60 | }
61 | }
62 |
63 | public class TreeNode
64 | {
65 | public int val;
66 | public TreeNode left;
67 | public TreeNode right;
68 | public TreeNode(int x) { val = x; }
69 | }
70 | }
71 |
72 |
73 |
--------------------------------------------------------------------------------
/LeetCode/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("LeetCode")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Microsoft")]
12 | [assembly: AssemblyProduct("LeetCode")]
13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("4cab5bb0-5cdf-4bd8-858b-5eb1b48b6fa9")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/LeetCode/Sudoku/Sudoku.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace LeetCode.Sudoku
8 | {
9 | public class Sudoku
10 | {
11 | private Grid[] originalSudoku = new Grid[] { new Grid(), new Grid(), new Grid(), new Grid(), new Grid(), new Grid(), new Grid(), new Grid(), new Grid() };
12 |
13 | private bool isCalculated
14 | {
15 | get
16 | {
17 | foreach(var grid in originalSudoku)
18 | if(grid.IsDone() == false)
19 | return false;
20 | return true;
21 | }
22 | }
23 |
24 | public Sudoku(char[,] board)
25 | {
26 | for(int i = 0; i < board.GetLength(0); i++)
27 | {
28 | for(int j = 0; j < board.GetLength(1); j++)
29 | {
30 | int gridIndex = i / 3 * 3 + j / 3;
31 | int index = j % 3 + 3 * (i % 3);
32 | char c = board[i, j];
33 | originalSudoku[gridIndex][index] = new Slot(c, c != '.');
34 | }
35 | }
36 |
37 | bool trimmed = true;
38 | while(trimmed)
39 | {
40 | trimmed = false;
41 | foreach(var grid in originalSudoku)
42 | trimmed |= grid.Trim();
43 |
44 | for(int i = 0; i < originalSudoku.Length; i += 3)
45 | {
46 | for(int j = 0; j < 3; j++)
47 | {
48 | var s1 = originalSudoku[i].GetRow(j);
49 | var s2 = originalSudoku[i + 1].GetRow(j);
50 | var s3 = originalSudoku[i + 2].GetRow(j);
51 | Line line = new Line(s1, s2, s3);
52 | trimmed |= line.Trim();
53 | }
54 | }
55 |
56 | for(int i = 0; i < 3; i++)
57 | {
58 | for(int j = 0; j < 3; j++)
59 | {
60 | var s1 = originalSudoku[i].GetColumn(j);
61 | var s2 = originalSudoku[i + 3].GetColumn(j);
62 | var s3 = originalSudoku[i + 6].GetColumn(j);
63 | Line line = new Line(s1, s2, s3);
64 | trimmed |= line.Trim();
65 | }
66 | }
67 | }
68 |
69 | if(!isCalculated)
70 | {
71 | // using backtracking to solve?
72 | }
73 | }
74 |
75 | public char[,] TransformDataFormat()
76 | {
77 | char[,] res = new char[9, 9];
78 |
79 | for(int i = 0; i < 9; i++)
80 | {
81 | int r = i / 3 * 3;
82 | var r1 = originalSudoku[r].GetRow(i % 3);
83 | for(int j = 0; j < r1.Length; j++)
84 | res[i, j] = r1[j].Value;
85 |
86 | var r2 = originalSudoku[r + 1].GetRow(i % 3);
87 | for(int j = 0; j < r2.Length; j++)
88 | res[i, j + 3] = r2[j].Value;
89 |
90 | var r3 = originalSudoku[r + 2].GetRow(i % 3);
91 | for(int j = 0; j < r3.Length; j++)
92 | res[i, j + 6] = r3[j].Value;
93 | }
94 |
95 | return res;
96 | }
97 | public override string ToString()
98 | {
99 | var res = TransformDataFormat();
100 | StringBuilder sb = new StringBuilder();
101 | for(int i = 0; i < res.GetLength(0); i++)
102 | {
103 | for(int j = 0; j < res.GetLength(1); j++)
104 | sb.Append(res[i, j].ToString().PadLeft( j % 3 == 0 ? 3: 2));
105 |
106 | sb.AppendLine();
107 | if(i % 3 == 2)
108 | sb.AppendLine();
109 | }
110 | return sb.ToString();
111 | }
112 |
113 | }
114 |
115 | public class Line
116 | {
117 | private Slot[] slots;
118 | public Line(Slot[] slots)
119 | {
120 | this.slots = slots;
121 | }
122 |
123 | public Line(Slot[] s1, Slot[] s2, Slot[] s3)
124 | {
125 | slots = new Slot[9];
126 |
127 | Array.Copy(s1, 0, slots, 0, 3);
128 | Array.Copy(s2, 0, slots, 3, 3);
129 | Array.Copy(s3, 0, slots, 6, 3);
130 | }
131 |
132 | public bool Trim()
133 | {
134 | bool changed = false;
135 | for(int i = 0; i < slots.Length; i++)
136 | if(slots[i].Confirmed)
137 | for(int j = 0; j < slots.Length; j++)
138 | changed |= slots[j].RemoveInvalid(slots[i].Value);
139 | return changed;
140 | }
141 | }
142 |
143 | public class Slot
144 | {
145 | public Slot(char v, bool confirmed = false)
146 | {
147 | Value = v;
148 | Confirmed = confirmed;
149 | PossibleValues.Remove(v);
150 | }
151 | public char Value { get; set; }
152 | public bool Confirmed { get; private set; }
153 | public HashSet PossibleValues = new HashSet(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' });
154 |
155 | public bool RemoveInvalid(char c)
156 | {
157 | if(Confirmed)
158 | return false;
159 |
160 | bool changed = PossibleValues.Remove(c);
161 |
162 | if(PossibleValues.Count <= 0)
163 | throw new Exception("This sudoku have no result, please check in the input");
164 |
165 | if(PossibleValues.Count == 1)
166 | {
167 | Confirmed = true;
168 | Value = PossibleValues.First();
169 | }
170 |
171 | return changed;
172 | }
173 | public override string ToString()
174 | {
175 | return Value.ToString();
176 | }
177 | }
178 |
179 | public class Grid
180 | {
181 | private Slot[] slots = new Slot[9];
182 | public Grid() { }
183 |
184 | public Slot this[int index]
185 | {
186 | get
187 | {
188 | if(index < 0 || index >= 9)
189 | throw new Exception();
190 |
191 | return slots[index];
192 | }
193 | set
194 | {
195 | if(index < 0 || index >= 9)
196 | throw new Exception();
197 |
198 | slots[index] = value;
199 | }
200 | }
201 |
202 | public bool Trim()
203 | {
204 | bool changed = false;
205 | for(int i = 0; i < slots.Length; i++)
206 | if(slots[i].Confirmed)
207 | for(int j = 0; j < slots.Length; j++)
208 | changed |= slots[j].RemoveInvalid(slots[i].Value);
209 |
210 | return changed;
211 | }
212 |
213 | public Slot[] GetRow(int row)
214 | {
215 | Slot[] arr = new Slot[3];
216 | Array.Copy(slots, row * 3, arr, 0, 3);
217 | return arr;
218 | }
219 |
220 | public Slot[] GetColumn(int column)
221 | {
222 | Slot[] arr = new Slot[3];
223 | for(int i = 0; i < 3; i++)
224 | arr[i] = slots[column + i * 3];
225 |
226 | return arr;
227 | }
228 |
229 | public bool IsDone()
230 | {
231 | foreach(var slot in slots)
232 | if(slot.Confirmed == false)
233 | return false;
234 |
235 | return true;
236 | }
237 |
238 | public override string ToString()
239 | {
240 | StringBuilder sb = new StringBuilder();
241 | for(int i = 0; i < slots.Length; i += 3)
242 | sb.AppendLine(string.Format("{0} {1} {2} ", slots[i], slots[i + 1], slots[i + 2]));
243 |
244 | return sb.ToString();
245 | }
246 | }
247 | }
248 |
--------------------------------------------------------------------------------
/LeetCode/Tools.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace LeetCode
8 | {
9 | public static class Tools
10 | {
11 | public static void UpdateReadMe()
12 | {
13 | string basePath = "https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/";
14 | try
15 | {
16 | string readMe = Path.Combine(Environment.CurrentDirectory, "../../../README.md");
17 | string[] lines = File.ReadAllLines(readMe);
18 | string[] easy = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "../../Easy.cs"));
19 | string[] medium = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "../../Medium.cs"));
20 | string[] hard = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "../../Hard.cs"));
21 |
22 |
23 | string[] fileLines = null;
24 | string fileName = null;
25 | for (int i = 0; i < lines.Length; i++)
26 | {
27 | string line = lines[i];
28 | string str = line.Trim();
29 | if (str.StartsWith("###"))
30 | {
31 | if (str.EndsWith("Easy"))
32 | {
33 | fileLines = easy;
34 | fileName = "Easy.cs";
35 | }
36 | else if (str.EndsWith("Medium"))
37 | {
38 | fileLines = medium;
39 | fileName = "Medium.cs";
40 | }
41 | else if (str.EndsWith("Hard"))
42 | {
43 | fileLines = hard;
44 | fileName = "Hard.cs";
45 | }
46 | }
47 |
48 | if (fileLines != null)
49 | {
50 | if (str.StartsWith("*"))
51 | {
52 | Regex regex = new Regex(@"(?<=[\[])[^]]*");
53 | MatchCollection collection = regex.Matches(str);
54 | if (collection.Count > 0)
55 | {
56 | string name = collection[0].Value;
57 | int l = GetCodeLine(fileLines, name);
58 |
59 | Regex linkRegex = new Regex(@"(([^()]+))");
60 | var match = linkRegex.Matches(regex.Replace(str, ""));
61 | if (match.Count > 1)
62 | {
63 | lines[i] = $"* [{name}]({match[1].Value}) ( [答案跳转]({basePath}{fileName}#L{l}) )";
64 | }
65 | }
66 | }
67 | }
68 | }
69 | File.WriteAllLines(readMe, lines, Encoding.UTF8);
70 | }
71 | catch (Exception e)
72 | {
73 | Console.WriteLine(e);
74 | throw;
75 | }
76 | }
77 |
78 | private static int GetCodeLine(string[] group, string name)
79 | {
80 | for (int i = 0; i < group.Length; i++)
81 | {
82 | if (group[i].Contains(name))
83 | {
84 | return i + 1;
85 | }
86 | }
87 |
88 | return 1;
89 | }
90 |
91 | public static char ToUpper(char c)
92 | {
93 | if('a' <= c && c <= 'z')
94 | c = (char)(c & ~0x20);
95 | return c;
96 | }
97 |
98 | public static char ToLower(char c)
99 | {
100 | if('A' <= c && c <= 'Z')
101 | c = (char)(c | 0x20);
102 | return c;
103 | }
104 |
105 | public static string Print(this int[,] m)
106 | {
107 | StringBuilder sb = new StringBuilder();
108 |
109 | for (int i = 0; i < m.GetLength(0); i++)
110 | {
111 | for (int j = 0; j < m.GetLength(1); j++)
112 | sb.Append((m[i, j]).ToString().PadLeft(4));
113 |
114 | sb.AppendLine();
115 | }
116 |
117 | return sb.ToString();
118 | }
119 |
120 | public static string Print(this IList l)
121 | {
122 | StringBuilder sb = new StringBuilder();
123 | foreach (var item in l)
124 | {
125 | sb.AppendFormat("{0} ", item);
126 | }
127 | return sb.ToString();
128 | }
129 |
130 | public static string Print(this string[] l)
131 | {
132 | StringBuilder sb = new StringBuilder();
133 | foreach(var item in l)
134 | {
135 | sb.AppendFormat("{0} ", item);
136 | }
137 |
138 | return sb.ToString();
139 | }
140 | public static string Print(this int[][] arr)
141 | {
142 | StringBuilder sb = new StringBuilder();
143 |
144 | for (int i = 0; i < arr.Length; i++)
145 | {
146 | for (int j = 0; j < arr[i].Length; j++)
147 | sb.Append(arr[i][j].ToString().PadLeft(4));
148 | sb.AppendLine();
149 | }
150 |
151 | return sb.ToString();
152 | }
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://996.icu) [](https://github.com/996icu/996.ICU/blob/master/LICENSE)
2 |
3 | ## LeetCode Algorithms
4 | * 这是一个关于LeetCode上算法题目的解法集合,主要是用C#语言(基本上没有用到C#的复杂库函数,所以代码基本上可以移植到其他语言)。后续会不定期的添加。下面列表列出的均为跑通的代码,如果有更好的算法解答,欢迎添加。
5 | * URL: [https://github.com/YaoYilin/LeetCode](https://github.com/YaoYilin/LeetCode)
6 | ---
7 | ### Easy
8 | * [1. 两数之和](https://leetcode-cn.com/problems/two-sum/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L12) )
9 | * [7. 颠倒整数](https://leetcode-cn.com/problems/reverse-integer/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L33) )
10 | * [9. 回文数](https://leetcode.cn/problems/palindrome-number/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L54) )
11 | * [13. 罗马数字转整数](https://leetcode.cn/problems/roman-to-integer/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L116) )
12 | * [14. 最长公共前缀](https://leetcode.cn/problems/longest-common-prefix/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L75) )
13 | * [20. 有效的括号](https://leetcode-cn.com/problems/valid-parentheses/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L148) )
14 | * [21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L171) )
15 | * [26. 删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L220) )
16 | * [27. 移除元素](https://leetcode-cn.com/problems/remove-element/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L243) )
17 | * [28. 实现 strStr()](https://leetcode-cn.com/problems/implement-strstr/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L266) )
18 | * [35. 搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L303) )
19 | * [58. 最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L318) )
20 | * [66. 加一](https://leetcode-cn.com/problems/plus-one/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L338) )
21 | * [67. 二进制求和](https://leetcode-cn.com/problems/add-binary/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L359) )
22 | * [69. x 的平方根](https://leetcode-cn.com/problems/sqrtx/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L392) )
23 | * [70. 爬楼梯](https://leetcode.cn/problems/climbing-stairs/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L438) )
24 | * [83. 删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L458) )
25 | * [88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L482) )
26 | * [100. 相同的树](https://leetcode-cn.com/problems/same-tree/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L507) )
27 | * [101. 对称二叉树](https://leetcode-cn.com/problems/symmetric-tree/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L530) )
28 | * [104. 二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L552) )
29 | * [111. 二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L569) )
30 | * [118. 帕斯卡三角形(杨辉三角)](https://leetcode-cn.com/problems/pascals-triangle/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L581) )
31 | * [119. 帕斯卡三角形(杨辉三角)II](https://leetcode-cn.com/problems/pascals-triangle-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L593) )
32 | * [125. 验证回文串](https://leetcode-cn.com/problems/valid-palindrome/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L615) )
33 | * [136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L671) )
34 | * [160. 相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L684) )
35 | * [167. 两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L702) )
36 | * [168. Excel表列名称](https://leetcode-cn.com/problems/excel-sheet-column-title/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L734) )
37 | * [171. Excel表列序号](https://leetcode-cn.com/problems/excel-sheet-column-number/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L750) )
38 | * [172. 阶乘后的零](https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L770) )
39 | * [189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L786) )
40 | * [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L798) )
41 | * [191. 位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L833) )
42 | * [202. 快乐数](https://leetcode-cn.com/problems/happy-number) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L847) )
43 | * [203. 删除链表中的节点](https://leetcode-cn.com/problems/remove-linked-list-elements/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L875) )
44 | * [204. 计数质数](https://leetcode-cn.com/problems/count-primes/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L896) )
45 | * [205. 同构字符串](https://leetcode-cn.com/problems/isomorphic-strings/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L922) )
46 | * [206. 反转链表](https://leetcode-cn.com/problems/reverse-linked-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L949) )
47 | * [217. 存在重复元素](https://leetcode-cn.com/problems/contains-duplicate/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L966) )
48 | * [219. 存在重复元素 II](https://leetcode-cn.com/problems/contains-duplicate-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L978) )
49 | * [226. 翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1001) )
50 | * [231. 2的幂](https://leetcode-cn.com/problems/power-of-two/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1016) )
51 | * [237. 删除链表的结点](https://leetcode-cn.com/problems/delete-node-in-a-linked-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1032) )
52 | * [257. 二叉树的所有路径](https://leetcode-cn.com/problems/binary-tree-paths/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1041) )
53 | * [258. 各位相加](https://leetcode-cn.com/problems/add-digits/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1067) )
54 | * [263. 丑数](https://leetcode-cn.com/problems/ugly-number/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1077) )
55 | * [268. 缺失数字](https://leetcode-cn.com/problems/missing-number/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1097) )
56 | * [278. 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1109) )
57 | * [283. 移动零](https://leetcode-cn.com/problems/move-zeroes/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1132) )
58 | * [303. 区域和检索 - 不可变](https://leetcode-cn.com/problems/range-sum-query-immutable/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1164) )
59 | * [326. 3的幂](https://leetcode-cn.com/problems/power-of-three/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1195) )
60 | * [342. 4的幂](https://leetcode-cn.com/problems/power-of-four/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1206) )
61 | * [344. 反转字符串](https://leetcode-cn.com/problems/reverse-string/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1214) )
62 | * [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1232) )
63 | * [367. 有效的完全平方数](https://leetcode-cn.com/problems/valid-perfect-square/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1270) )
64 | * [371. 两整数之和](https://leetcode-cn.com/problems/sum-of-two-integers/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1304) )
65 | * [374. 猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1318) )
66 | * [383. 赎金信](https://leetcode-cn.com/problems/ransom-note/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1358) )
67 | * [387. 字符串中的第一个唯一字符](https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1374) )
68 | * [389. 找不同](https://leetcode-cn.com/problems/find-the-difference/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1402) )
69 | * [396. 旋转函数](https://leetcode-cn.com/problems/rotate-function/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1) )
70 | * [404. 左叶子之和](https://leetcode-cn.com/problems/sum-of-left-leaves/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1416) )
71 | * [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1445) )
72 | * [412. Fizz Buzz](https://leetcode-cn.com/problems/fizz-buzz/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1465) )
73 | * [415. 字符串相加](https://leetcode-cn.com/problems/add-strings/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1505) )
74 | * [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1536) )
75 | * [448. 找到所有数组中消失的数字](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1580) )
76 | * [461. 汉明距离](https://leetcode-cn.com/problems/hamming-distance/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1597) )
77 | * [463. 岛屿的周长](https://leetcode-cn.com/problems/island-perimeter/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1613) )
78 | * [476. 数字的补数](https://leetcode-cn.com/problems/number-complement/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1641) )
79 | * [485. 最大连续1的个数](https://leetcode-cn.com/problems/max-consecutive-ones/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1654) )
80 | * [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1677) )
81 | * [504. 七进制数](https://leetcode-cn.com/problems/base-7/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1726) )
82 | * [520. 检测大写字母](https://leetcode-cn.com/problems/detect-capital/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1753) )
83 | * [541. 反转字符串 II](https://leetcode-cn.com/problems/reverse-string-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1796) )
84 | * [557. 反转字符串中的单词 III](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1828) )
85 | * [561. 数组拆分 I](https://leetcode.com/problems/array-partition-i/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1862) )
86 | * [617. 合并二叉树](https://leetcode-cn.com/problems/merge-two-binary-trees/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1875) )
87 | * [628. 三个数的最大乘积](https://leetcode.cn/problems/maximum-product-of-three-numbers/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1897) )
88 | * [633. 平方数之和](https://leetcode-cn.com/problems/sum-of-square-numbers/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1911) )
89 | * [637. 二叉树的层平均值](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1933) )
90 | * [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1973) )
91 | * [657. 判断路线成圈](https://leetcode-cn.com/problems/judge-route-circle/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L1994) )
92 | * [665. 非递减数列](https://leetcode-cn.com/problems/non-decreasing-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2015) )
93 | * [693. 交替位二进制数](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2050) )
94 | * [704. 二分查找](https://leetcode-cn.com/problems/binary-search/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2059) )
95 | * [709. 转换成小写字母](https://leetcode-cn.com/problems/to-lower-case/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2091) )
96 | * [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2108) )
97 | * [762. 二进制表示中质数个计算置位](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2133) )
98 | * [766. 托普利茨矩阵](https://leetcode-cn.com/problems/toeplitz-matrix/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2163) )
99 | * [771. 宝石与石头](https://leetcode-cn.com/problems/jewels-and-stones/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2176) )
100 | * [784. 字母大小写全排列](https://leetcode-cn.com/problems/letter-case-permutation/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2193) )
101 | * [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2224) )
102 | * [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2234) )
103 | * [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2260) )
104 | * [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2293) )
105 | * [824. 山羊拉丁文](https://leetcode-cn.com/problems/goat-latin/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2322) )
106 | * [832. 翻转图像](https://leetcode-cn.com/problems/flipping-an-image/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2366) )
107 | * [836. 矩形重叠](https://leetcode-cn.com/problems/rectangle-overlap/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2387) )
108 | * [844. 比较含退格的字符串](https://leetcode-cn.com/problems/backspace-string-compare/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2395) )
109 | * [852. 山脉数组的峰顶索引](https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2422) )
110 | * [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2435) )
111 | * [860. 柠檬水找零](https://leetcode-cn.com/problems/lemonade-change/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2468) )
112 | * [868. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2524) )
113 | * [872. 叶子相似的树](https://leetcode-cn.com/contest/weekly-contest-94/problems/leaf-similar-trees/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2546) )
114 | * [876. 链表的中间结点](https://leetcode-cn.com/problems/middle-of-the-linked-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2580) )
115 | * [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2597) )
116 | * [905. 按奇偶校验排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2639) )
117 | * [917. 仅仅反转字母](https://leetcode-cn.com/problems/reverse-only-letters/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2662) )
118 | * [922. 按奇偶排序数组 II](https://leetcode-cn.com/problems/sort-array-by-parity-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2692) )
119 | * [929. 独特的电子邮件地址](https://leetcode-cn.com/problems/unique-email-addresses/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2717) )
120 | * [1009. 十进制整数的反码](https://leetcode.cn/problems/complement-of-base-10-integer/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2772) )
121 | * [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/comments/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2791) )
122 | * [1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2826) )
123 | * [1837. K 进制表示下的各位数字总和](https://leetcode.cn/problems/sum-of-digits-in-base-k/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2852) )
124 | * [1859. 将句子排序](https://leetcode.cn/problems/sorting-the-sentence/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2867) )
125 | * [2678. 老人的数目](https://leetcode.cn/problems/number-of-senior-citizens/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Easy.cs#L2897) )
126 | ---
127 | ### Medium
128 | * [2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L12) )
129 | * [17. 电话号码的字母组合](https://leetcode.cn/problems/letter-combinations-of-a-phone-number/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L59) )
130 | * [19. 删除链表的倒数第N个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L103) )
131 | * [43. 字符串相乘](https://leetcode.cn/problems/multiply-strings/submissions/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L131) )
132 | * [46. 全排列](https://leetcode-cn.com/problems/permutations/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L221) )
133 | * [48. 旋转图像](https://leetcode-cn.com/problems/rotate-image/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L258) )
134 | * [49. 字母异位词分组](https://leetcode-cn.com/problems/group-anagrams/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L277) )
135 | * [54. 螺旋矩阵](https://leetcode-cn.com/problems/spiral-matrix/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L295) )
136 | * [56. 合并区间](https://leetcode.cn/problems/merge-intervals/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L342) )
137 | * [59. 螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L376) )
138 | * [61. 旋转链表](https://leetcode-cn.com/problems/rotate-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L410) )
139 | * [73. 矩阵置零](https://leetcode-cn.com/problems/set-matrix-zeroes/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L436) )
140 | * [74. 搜索二维矩阵](https://leetcode-cn.com/problems/search-a-2d-matrix/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L473) )
141 | * [75. 分类颜色](https://leetcode-cn.com/problems/sort-colors/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L543) )
142 | * [82. 删除排序链表中的重复元素 II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L590) )
143 | * [86. 分隔链表](https://leetcode-cn.com/problems/partition-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L628) )
144 | * [92. 反转链表 II](https://leetcode-cn.com/problems/reverse-linked-list-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L656) )
145 | * [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L683) )
146 | * [114. 二叉树展开为链表](https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L702) )
147 | * [142. 环形链表 II](https://leetcode-cn.com/problems/linked-list-cycle-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L726) )
148 | * [144. 二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L765) )
149 | * [165. 比较版本号](https://leetcode-cn.com/problems/compare-version-numbers/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L785) )
150 | * [227. 基本计算器II](https://leetcode-cn.com/problems/basic-calculator-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L852) )
151 | * [238. 除自身以外数组的乘积](https://leetcode-cn.com/problems/product-of-array-except-self/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L895) )
152 | * [240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L919) )
153 | * [287. 寻找重复数](https://leetcode-cn.com/problems/find-the-duplicate-number/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L941) )
154 | * [319. 灯泡开关](https://leetcode-cn.com/problems/bulb-switcher/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L991) )
155 | * [328. 奇偶链表](https://leetcode-cn.com/problems/odd-even-linked-list/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L999) )
156 | * [338. Bit位计数](https://leetcode-cn.com/problems/counting-bits/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1020) )
157 | * [393. UTF-8 编码验证](https://leetcode-cn.com/problems/utf-8-validation/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1043) )
158 | * [394. 字符串解码](https://leetcode-cn.com/problems/decode-string/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1074) )
159 | * [397. 整数替换](https://leetcode-cn.com/problems/integer-replacement/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1159) )
160 | * [423. 从英文中重建数字](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1193) )
161 | * [442. 数组中重复的数据](https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1232) )
162 | * [456. 132 Pattern](https://leetcode-cn.com/problems/132-pattern/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1248) )
163 | * [468. 验证IP地址](https://leetcode-cn.com/problems/validate-ip-address/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1290) )
164 | * [477. 汉明距离总和](https://leetcode-cn.com/problems/total-hamming-distance/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1274) )
165 | * [537. 复数乘法](https://leetcode-cn.com/problems/complex-number-multiplication/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1381) )
166 | * [540. 有序数组中的单一元素](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1401) )
167 | * [739. 每日温度](https://leetcode-cn.com/problems/daily-temperatures/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1416) )
168 | * [791. 自定义字符串排序](https://leetcode-cn.com/problems/custom-sort-string/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Medium.cs#L1498) )
169 | ---
170 | ### Hard
171 | * [4. 两个排序数组的中位数](https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Hard.cs#L12) )
172 | * [145. 二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Hard.cs#L103) )
173 | * [233. 数字1的个数](https://leetcode-cn.com/problems/number-of-digit-one/description/) ( [答案跳转](https://github.com/YaoYilin/LeetCode/blob/master/LeetCode/Hard.cs#L123) )
174 |
175 |
--------------------------------------------------------------------------------