Reformat the C# codes.

Disable creating new line before open brace.
This commit is contained in:
krahets
2023-04-23 03:03:12 +08:00
parent ac6eece4f3
commit 73dcb4cea9
49 changed files with 561 additions and 1135 deletions

View File

@@ -10,19 +10,15 @@ using System.IO;
namespace hello_algo.chapter_backtracking;
public class preorder_traversal_i_compact
{
public class preorder_traversal_i_compact {
static List<TreeNode> res;
/* 前序遍历:例题一 */
static void preOrder(TreeNode root)
{
if (root == null)
{
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
if (root.val == 7)
{
if (root.val == 7) {
// 记录解
res.Add(root);
}
@@ -31,8 +27,7 @@ public class preorder_traversal_i_compact
}
[Test]
public void Test()
{
public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root);
@@ -44,4 +39,4 @@ public class preorder_traversal_i_compact
Console.WriteLine("\n输出所有值为 7 的节点");
PrintUtil.PrintList(res.Select(p => p.val).ToList());
}
}
}

View File

@@ -9,22 +9,18 @@ using NUnit.Framework;
namespace hello_algo.chapter_backtracking;
public class preorder_traversal_ii_compact
{
public class preorder_traversal_ii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* 前序遍历:例题二 */
static void preOrder(TreeNode root)
{
if (root == null)
{
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
// 尝试
path.Add(root);
if (root.val == 7)
{
if (root.val == 7) {
// 记录解
res.Add(new List<TreeNode>(path));
}
@@ -35,8 +31,7 @@ public class preorder_traversal_ii_compact
}
[Test]
public void Test()
{
public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root);
@@ -47,9 +42,8 @@ public class preorder_traversal_ii_compact
preOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径");
foreach (List<TreeNode> path in res)
{
foreach (List<TreeNode> path in res) {
PrintUtil.PrintList(path.Select(p => p.val).ToList());
}
}
}
}

View File

@@ -9,23 +9,19 @@ using NUnit.Framework;
namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_compact
{
public class preorder_traversal_iii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* 前序遍历:例题三 */
static void preOrder(TreeNode root)
{
static void preOrder(TreeNode root) {
// 剪枝
if (root == null || root.val == 3)
{
if (root == null || root.val == 3) {
return;
}
// 尝试
path.Add(root);
if (root.val == 7)
{
if (root.val == 7) {
// 记录解
res.Add(new List<TreeNode>(path));
}
@@ -36,8 +32,7 @@ public class preorder_traversal_iii_compact
}
[Test]
public void Test()
{
public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root);
@@ -48,9 +43,8 @@ public class preorder_traversal_iii_compact
preOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res)
{
foreach (List<TreeNode> path in res) {
PrintUtil.PrintList(path.Select(p => p.val).ToList());
}
}
}
}

View File

@@ -9,54 +9,44 @@ using NUnit.Framework;
namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_template
{
public class preorder_traversal_iii_template {
/* 判断当前状态是否为解 */
static bool isSolution(List<TreeNode> state)
{
static bool isSolution(List<TreeNode> state) {
return state.Count != 0 && state[^1].val == 7;
}
/* 记录解 */
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res)
{
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.Add(new List<TreeNode>(state));
}
/* 判断在当前状态下,该选择是否合法 */
static bool isValid(List<TreeNode> state, TreeNode choice)
{
static bool isValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
/* 更新状态 */
static void makeChoice(List<TreeNode> state, TreeNode choice)
{
static void makeChoice(List<TreeNode> state, TreeNode choice) {
state.Add(choice);
}
/* 恢复状态 */
static void undoChoice(List<TreeNode> state, TreeNode choice)
{
static void undoChoice(List<TreeNode> state, TreeNode choice) {
state.RemoveAt(state.Count - 1);
}
/* 回溯算法:例题三 */
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res)
{
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// 检查是否为解
if (isSolution(state))
{
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
foreach (TreeNode choice in choices)
{
foreach (TreeNode choice in choices) {
// 剪枝:检查选择是否合法
if (isValid(state, choice))
{
if (isValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
List<TreeNode> nextChoices = new List<TreeNode>() { choice.left, choice.right };
@@ -68,8 +58,7 @@ public class preorder_traversal_iii_template
}
[Test]
public void Test()
{
public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root);
@@ -80,9 +69,8 @@ public class preorder_traversal_iii_template
backtrack(new List<TreeNode>(), choices, res);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res)
{
foreach (List<TreeNode> path in res) {
PrintUtil.PrintList(path.Select(p => p.val).ToList());
}
}
}
}