mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
@ -618,7 +618,7 @@ char * longestPalindrome(char * s){
|
||||
### C#:
|
||||
|
||||
動態規則:
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution {
|
||||
|
||||
public string LongestPalindrome(string s) {
|
||||
@ -648,7 +648,7 @@ public class Solution {
|
||||
```
|
||||
|
||||
雙指針:
|
||||
```C#
|
||||
```csharp
|
||||
public class Solution {
|
||||
int maxlenth = 0;
|
||||
int left = 0;
|
||||
|
@ -733,7 +733,7 @@ def backtracking(result, letter_map, digits, path, index)
|
||||
end
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<string> res = new List<string>();
|
||||
|
@ -462,7 +462,7 @@ impl Solution {
|
||||
```
|
||||
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 虚拟头结点
|
||||
public ListNode SwapPairs(ListNode head)
|
||||
{
|
||||
|
@ -1359,7 +1359,7 @@ impl Solution {
|
||||
```
|
||||
|
||||
>前缀表统一不减一
|
||||
```C#
|
||||
```csharp
|
||||
public int StrStr(string haystack, string needle)
|
||||
{
|
||||
if (string.IsNullOrEmpty(needle))
|
||||
|
@ -331,7 +331,7 @@ class Solution {
|
||||
|
||||
### C#
|
||||
|
||||
```c#
|
||||
```csharp
|
||||
public int[] SearchRange(int[] nums, int target) {
|
||||
|
||||
var leftBorder = GetLeftBorder(nums, target);
|
||||
|
@ -336,6 +336,32 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int[][] Merge(int[][] intervals)
|
||||
{
|
||||
if (intervals.Length == 0)
|
||||
return intervals;
|
||||
Array.Sort(intervals, (a, b) => a[0] - b[0]);
|
||||
List<List<int>> res = new List<List<int>>();
|
||||
res.Add(intervals[0].ToList());
|
||||
for (int i = 1; i < intervals.Length; i++)
|
||||
{
|
||||
if (res[res.Count - 1][1] >= intervals[i][0])
|
||||
{
|
||||
res[res.Count - 1][1] = Math.Max(res[res.Count - 1][1], intervals[i][1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
res.Add(intervals[i].ToList());
|
||||
}
|
||||
}
|
||||
return res.Select(x => x.ToArray()).ToArray();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -537,7 +537,7 @@ object Solution {
|
||||
|
||||
### c#
|
||||
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int UniquePaths(int m, int n)
|
||||
|
@ -468,7 +468,7 @@ object Solution {
|
||||
|
||||
### C#
|
||||
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution {
|
||||
public int ClimbStairs(int n) {
|
||||
if(n<=2) return n;
|
||||
|
@ -793,7 +793,7 @@ end
|
||||
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 暴力
|
||||
public class Solution
|
||||
{
|
||||
|
@ -641,7 +641,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
|
@ -792,7 +792,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public long val = Int64.MinValue;
|
||||
public bool IsValidBST(TreeNode root)
|
||||
|
@ -898,7 +898,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public bool IsSymmetric(TreeNode root)
|
||||
{
|
||||
|
@ -463,7 +463,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public IList<IList<int>> LevelOrder(TreeNode root)
|
||||
{
|
||||
var res = new List<IList<int>>();
|
||||
@ -825,7 +825,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public IList<IList<int>> LevelOrderBottom(TreeNode root)
|
||||
{
|
||||
var res = new List<IList<int>>();
|
||||
|
@ -1033,7 +1033,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归法
|
||||
public int MaxDepth(TreeNode root) {
|
||||
if(root == null) return 0;
|
||||
@ -1044,7 +1044,7 @@ public int MaxDepth(TreeNode root) {
|
||||
return 1 + Math.Max(leftDepth, rightDepth);
|
||||
}
|
||||
```
|
||||
```C#
|
||||
```csharp
|
||||
// 前序遍历
|
||||
int result = 0;
|
||||
public int MaxDepth(TreeNode root)
|
||||
@ -1065,7 +1065,7 @@ public void GetDepth(TreeNode root, int depth)
|
||||
return;
|
||||
}
|
||||
```
|
||||
```C#
|
||||
```csharp
|
||||
// 迭代法
|
||||
public int MaxDepth(TreeNode root)
|
||||
{
|
||||
|
@ -1229,7 +1229,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public TreeNode BuildTree(int[] inorder, int[] postorder)
|
||||
{
|
||||
if (inorder.Length == 0 || postorder.Length == null) return null;
|
||||
|
@ -909,7 +909,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public bool IsBalanced(TreeNode root)
|
||||
{
|
||||
return GetHeight(root) == -1 ? false : true;
|
||||
|
@ -709,7 +709,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public int MinDepth(TreeNode root)
|
||||
{
|
||||
@ -725,7 +725,7 @@ public int MinDepth(TreeNode root)
|
||||
return res;
|
||||
}
|
||||
```
|
||||
```C#
|
||||
```csharp
|
||||
// 迭代
|
||||
public int MinDepth(TreeNode root)
|
||||
{
|
||||
|
@ -1513,7 +1513,7 @@ impl Solution {
|
||||
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 0112.路径总和
|
||||
// 递归
|
||||
public bool HasPathSum(TreeNode root, int targetSum)
|
||||
|
@ -973,7 +973,7 @@ char * reverseWords(char * s){
|
||||
```
|
||||
|
||||
### C#
|
||||
```C# LINQ高级方法
|
||||
```csharp LINQ高级方法
|
||||
public string ReverseWords(string s) {
|
||||
return string.Join(' ', s.Trim().Split(' ',StringSplitOptions.RemoveEmptyEntries).Reverse());
|
||||
}
|
||||
|
@ -868,7 +868,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public int CountNodes(TreeNode root)
|
||||
{
|
||||
|
@ -521,7 +521,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
|
||||
{
|
||||
|
@ -432,7 +432,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
|
||||
{
|
||||
if (root == null || root == p || root == q) return root;
|
||||
|
@ -901,7 +901,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public IList<string> BinaryTreePaths(TreeNode root)
|
||||
{
|
||||
List<int> path = new();
|
||||
|
@ -652,7 +652,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public int SumOfLeftLeaves(TreeNode root)
|
||||
{
|
||||
|
@ -396,6 +396,29 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int[][] ReconstructQueue(int[][] people)
|
||||
{
|
||||
Array.Sort(people, (a, b) =>
|
||||
{
|
||||
if (a[0] == b[0])
|
||||
{
|
||||
return a[1] - b[1];
|
||||
}
|
||||
return b[0] - a[0];
|
||||
});
|
||||
var res = new List<int[]>();
|
||||
for (int i = 0; i < people.Length; i++)
|
||||
{
|
||||
res.Insert(people[i][1], people[i]);
|
||||
}
|
||||
return res.ToArray();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
@ -441,6 +441,27 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int EraseOverlapIntervals(int[][] intervals)
|
||||
{
|
||||
if (intervals.Length == 0) return 0;
|
||||
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1]));
|
||||
int res = 1, end = intervals[0][1];
|
||||
for (int i = 1; i < intervals.Length; i++)
|
||||
{
|
||||
if (end <= intervals[i][0])
|
||||
{
|
||||
end = intervals[i][1];
|
||||
res++;
|
||||
}
|
||||
}
|
||||
return intervals.Length - res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
@ -772,7 +772,7 @@ impl Solution {
|
||||
### C#
|
||||
|
||||
> 递归法:
|
||||
```C#
|
||||
```csharp
|
||||
public TreeNode DeleteNode(TreeNode root, int key) {
|
||||
// 第一种情况:没找到删除的节点,遍历到空节点直接返回了
|
||||
if (root == null) return null;
|
||||
|
@ -332,6 +332,24 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int FindMinArrowShots(int[][] points)
|
||||
{
|
||||
if (points.Length == 0) return 0;
|
||||
Array.Sort(points, (a, b) => a[0].CompareTo(b[0]));
|
||||
int count = 1;
|
||||
for (int i = 1; i < points.Length; i++)
|
||||
{
|
||||
if (points[i][0] > points[i - 1][1]) count++;
|
||||
else points[i][1] = Math.Min(points[i][1], points[i - 1][1]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -682,7 +682,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 前缀表不减一
|
||||
public bool RepeatedSubstringPattern(string s)
|
||||
{
|
||||
|
@ -1010,7 +1010,7 @@ pub fn find_mode(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public class Solution
|
||||
{
|
||||
|
@ -439,7 +439,7 @@ object Solution {
|
||||
|
||||
动态规划:
|
||||
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int Fib(int n)
|
||||
@ -459,7 +459,7 @@ public class Solution
|
||||
|
||||
递归:
|
||||
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int Fib(int n)
|
||||
|
@ -685,7 +685,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
//递归
|
||||
int maxDepth = -1;
|
||||
int res = 0;
|
||||
|
@ -648,7 +648,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public class Solution
|
||||
{
|
||||
|
@ -530,7 +530,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public class Solution
|
||||
{
|
||||
|
@ -789,7 +789,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public TreeNode MergeTrees(TreeNode root1, TreeNode root2)
|
||||
{
|
||||
if (root1 == null) return root2;
|
||||
|
@ -583,7 +583,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public TreeNode ConstructMaximumBinaryTree(int[] nums)
|
||||
{
|
||||
if (nums.Length == 0) return null;
|
||||
|
@ -568,7 +568,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public TreeNode TrimBST(TreeNode root, int low, int high)
|
||||
{
|
||||
|
@ -465,7 +465,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 递归
|
||||
public TreeNode SearchBST(TreeNode root, int val)
|
||||
{
|
||||
|
@ -1486,7 +1486,7 @@ impl MyLinkedList {
|
||||
```
|
||||
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
class ListNode
|
||||
{
|
||||
public int val;
|
||||
|
@ -392,6 +392,30 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int MonotoneIncreasingDigits(int n)
|
||||
{
|
||||
char[] s = n.ToString().ToCharArray();
|
||||
int flag = s.Length;
|
||||
for (int i = s.Length - 1; i > 0; i--)
|
||||
{
|
||||
if (s[i - 1] > s[i])
|
||||
{
|
||||
flag = i;
|
||||
s[i - 1]--;
|
||||
}
|
||||
}
|
||||
for (int i = flag; i < s.Length; i++)
|
||||
{
|
||||
s[i] = '9';
|
||||
}
|
||||
return int.Parse(new string(s));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -500,7 +500,7 @@ object Solution {
|
||||
|
||||
### C#
|
||||
|
||||
```c#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int MinCostClimbingStairs(int[] cost)
|
||||
|
@ -404,6 +404,32 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<int> PartitionLabels(string s)
|
||||
{
|
||||
int[] location = new int[27];
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
location[s[i] - 'a'] = i;
|
||||
}
|
||||
List<int> res = new List<int>();
|
||||
int left = 0, right = 0;
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
right = Math.Max(right, location[s[i] - 'a']);
|
||||
if (i == right)
|
||||
{
|
||||
res.Add(right - left + 1);
|
||||
left = i + 1;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -726,6 +726,31 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int res = 0;
|
||||
public int MinCameraCover(TreeNode root)
|
||||
{
|
||||
if (Traversal(root) == 0) res++;
|
||||
return res;
|
||||
}
|
||||
public int Traversal(TreeNode cur)
|
||||
{
|
||||
if (cur == null) return 2;
|
||||
int left = Traversal(cur.left);
|
||||
int right = Traversal(cur.right);
|
||||
if (left == 2 && right == 2) return 0;
|
||||
else if (left == 0 || right == 0)
|
||||
{
|
||||
res++;
|
||||
return 1;
|
||||
}
|
||||
else return 2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
|
@ -742,7 +742,7 @@ impl Solution{
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 前序遍历
|
||||
public IList<int> PreorderTraversal(TreeNode root)
|
||||
{
|
||||
@ -772,7 +772,7 @@ public IList<int> PreorderTraversal(TreeNode root)
|
||||
return res;
|
||||
}
|
||||
```
|
||||
```C#
|
||||
```csharp
|
||||
// 中序遍历
|
||||
public IList<int> InorderTraversal(TreeNode root)
|
||||
{
|
||||
@ -803,7 +803,7 @@ public IList<int> InorderTraversal(TreeNode root)
|
||||
}
|
||||
```
|
||||
|
||||
```C#
|
||||
```csharp
|
||||
// 后序遍历
|
||||
public IList<int> PostorderTraversal(TreeNode root)
|
||||
{
|
||||
|
@ -696,7 +696,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 前序遍历
|
||||
public IList<int> PreorderTraversal(TreeNode root)
|
||||
{
|
||||
|
@ -566,7 +566,7 @@ impl Solution {
|
||||
```
|
||||
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
// 前序遍历
|
||||
public IList<int> PreorderTraversal(TreeNode root)
|
||||
{
|
||||
@ -584,7 +584,7 @@ public void Traversal(TreeNode cur, IList<int> res)
|
||||
Traversal(cur.right, res);
|
||||
}
|
||||
```
|
||||
```C#
|
||||
```csharp
|
||||
// 中序遍历
|
||||
public IList<int> InorderTraversal(TreeNode root)
|
||||
{
|
||||
@ -601,7 +601,7 @@ public void Traversal(TreeNode cur, IList<int> res)
|
||||
Traversal(cur.right, res);
|
||||
}
|
||||
```
|
||||
```C#
|
||||
```csharp
|
||||
// 后序遍历
|
||||
public IList<int> PostorderTraversal(TreeNode root)
|
||||
{
|
||||
|
@ -503,7 +503,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
```csharp
|
||||
public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
|
||||
{
|
||||
if (headA == null || headB == null) return null;
|
||||
|
Reference in New Issue
Block a user