mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2361 from eeee0717/master
Update0108.将有序数组转换为二叉搜索树,添加C#
This commit is contained in:
@ -732,6 +732,38 @@ def backtracking(result, letter_map, digits, path, index)
|
||||
end
|
||||
end
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
public class Solution
|
||||
{
|
||||
public IList<string> res = new List<string>();
|
||||
public string s;
|
||||
public string[] letterMap = new string[10] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
|
||||
public IList<string> LetterCombinations(string digits)
|
||||
{
|
||||
if (digits.Length == 0)
|
||||
return res;
|
||||
BackTracking(digits, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(string digits, int index)
|
||||
{
|
||||
if (index == digits.Length)
|
||||
{
|
||||
res.Add(s);
|
||||
return;
|
||||
}
|
||||
int digit = digits[index] - '0';
|
||||
string letters = letterMap[digit];
|
||||
for (int i = 0; i < letters.Length; i++)
|
||||
{
|
||||
s += letters[i];
|
||||
BackTracking(digits, index + 1);
|
||||
s = s.Substring(0, s.Length - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -598,6 +598,66 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> CombinationSum(int[] candidates, int target)
|
||||
{
|
||||
BackTracking(candidates, target, 0, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int[] candidates, int target, int start, int sum)
|
||||
{
|
||||
if (sum > target) return;
|
||||
if (sum == target)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i < candidates.Length; i++)
|
||||
{
|
||||
sum += candidates[i];
|
||||
path.Add(candidates[i]);
|
||||
BackTracking(candidates, target, i, sum);
|
||||
sum -= candidates[i];
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 剪枝优化
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> CombinationSum(int[] candidates, int target)
|
||||
{
|
||||
Array.Sort(candidates);
|
||||
BackTracking(candidates, target, 0, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int[] candidates, int target, int start, int sum)
|
||||
{
|
||||
if (sum > target) return;
|
||||
if (sum == target)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++)
|
||||
{
|
||||
sum += candidates[i];
|
||||
path.Add(candidates[i]);
|
||||
BackTracking(candidates, target, i, sum);
|
||||
sum -= candidates[i];
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
@ -792,7 +792,59 @@ def backtracking(result, path, n, j, k)
|
||||
end
|
||||
|
||||
```
|
||||
|
||||
### C#
|
||||
```C#
|
||||
// 暴力
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> Combine(int n, int k)
|
||||
{
|
||||
BackTracking(n, k, 1);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int n, int k, int start)
|
||||
{
|
||||
if (path.Count == k)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= n; i++)
|
||||
{
|
||||
path.Add(i);
|
||||
BackTracking(n, k, i + 1);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 剪枝
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> Combine(int n, int k)
|
||||
{
|
||||
BackTracking(n, k, 1);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int n, int k, int start)
|
||||
{
|
||||
if (path.Count == k)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= n - (k - path.Count) + 1; i++)
|
||||
{
|
||||
path.Add(i);
|
||||
BackTracking(n, k, i + 1);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -530,6 +530,23 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
// 递归
|
||||
public TreeNode SortedArrayToBST(int[] nums)
|
||||
{
|
||||
return Traversal(nums, 0, nums.Length - 1);
|
||||
}
|
||||
public TreeNode Traversal(int[] nums, int left, int right)
|
||||
{
|
||||
if (left > right) return null;
|
||||
int mid = left + (right - left) / 2;
|
||||
TreeNode node = new TreeNode(nums[mid]);
|
||||
node.left = Traversal(nums, left, mid - 1);
|
||||
node.right = Traversal(nums, mid + 1, right);
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
@ -633,7 +633,66 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> CombinationSum3(int k, int n)
|
||||
{
|
||||
BackTracking(k, n, 0, 1);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int k, int n, int sum, int start)
|
||||
{
|
||||
if (path.Count == k)
|
||||
{
|
||||
if (sum == n)
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= 9; i++)
|
||||
{
|
||||
sum += i;
|
||||
path.Add(i);
|
||||
BackTracking(k, n, sum, i + 1);
|
||||
sum -= i;
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 剪枝
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> CombinationSum3(int k, int n)
|
||||
{
|
||||
BackTracking(k, n, 0, 1);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int k, int n, int sum, int start)
|
||||
{
|
||||
if (sum > n)
|
||||
return;
|
||||
if (path.Count == k)
|
||||
{
|
||||
if (sum == n)
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= 9 - (k - path.Count) + 1; i++)
|
||||
{
|
||||
sum += i;
|
||||
path.Add(i);
|
||||
BackTracking(k, n, sum, i + 1);
|
||||
sum -= i;
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -529,6 +529,23 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
// 递归
|
||||
public class Solution
|
||||
{
|
||||
int pre = 0;
|
||||
public TreeNode ConvertBST(TreeNode root)
|
||||
{
|
||||
if (root == null) return null;
|
||||
ConvertBST(root.right);
|
||||
root.val += pre;
|
||||
pre = root.val;
|
||||
ConvertBST(root.left);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
Reference in New Issue
Block a user