mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 15:56:05 +00:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -2,67 +2,66 @@
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.include
|
||||
namespace hello_algo.include;
|
||||
|
||||
/// <summary>
|
||||
/// Definition for a singly-linked list node
|
||||
/// </summary>
|
||||
public class ListNode
|
||||
{
|
||||
public int val;
|
||||
public ListNode? next;
|
||||
|
||||
/// <summary>
|
||||
/// Definition for a singly-linked list node
|
||||
/// Generate a linked list with an array
|
||||
/// </summary>
|
||||
public class ListNode
|
||||
/// <param name="x"></param>
|
||||
public ListNode(int x)
|
||||
{
|
||||
public int val;
|
||||
public ListNode? next;
|
||||
|
||||
/// <summary>
|
||||
/// Generate a linked list with an array
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
public ListNode(int x)
|
||||
{
|
||||
val = x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a linked list with an array
|
||||
/// </summary>
|
||||
/// <param name="arr"></param>
|
||||
/// <returns></returns>
|
||||
public static ListNode? ArrToLinkedList(int[] arr)
|
||||
{
|
||||
ListNode dum = new ListNode(0);
|
||||
ListNode head = dum;
|
||||
foreach (int val in arr)
|
||||
{
|
||||
head.next = new ListNode(val);
|
||||
head = head.next;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list node with specific value from a linked list
|
||||
/// </summary>
|
||||
/// <param name="head"></param>
|
||||
/// <param name="val"></param>
|
||||
/// <returns></returns>
|
||||
public static ListNode? GetListNode(ListNode? head, int val)
|
||||
{
|
||||
while (head != null && head.val != val)
|
||||
{
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
public override string? ToString()
|
||||
{
|
||||
List<string> list = new();
|
||||
var head = this;
|
||||
while (head != null)
|
||||
{
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
}
|
||||
return string.Join("->", list);
|
||||
}
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a linked list with an array
|
||||
/// </summary>
|
||||
/// <param name="arr"></param>
|
||||
/// <returns></returns>
|
||||
public static ListNode? ArrToLinkedList(int[] arr)
|
||||
{
|
||||
ListNode dum = new ListNode(0);
|
||||
ListNode head = dum;
|
||||
foreach (int val in arr)
|
||||
{
|
||||
head.next = new ListNode(val);
|
||||
head = head.next;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list node with specific value from a linked list
|
||||
/// </summary>
|
||||
/// <param name="head"></param>
|
||||
/// <param name="val"></param>
|
||||
/// <returns></returns>
|
||||
public static ListNode? GetListNode(ListNode? head, int val)
|
||||
{
|
||||
while (head != null && head.val != val)
|
||||
{
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
public override string? ToString()
|
||||
{
|
||||
List<string> list = new();
|
||||
var head = this;
|
||||
while (head != null)
|
||||
{
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
}
|
||||
return string.Join("->", list);
|
||||
}
|
||||
}
|
||||
|
||||
+107
-109
@@ -4,121 +4,119 @@
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.include
|
||||
namespace hello_algo.include;
|
||||
|
||||
public class Trunk
|
||||
{
|
||||
public class Trunk
|
||||
public Trunk? prev;
|
||||
public String str;
|
||||
|
||||
public Trunk(Trunk? prev, String str)
|
||||
{
|
||||
public Trunk? prev;
|
||||
public String str;
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
public Trunk(Trunk? prev, String str)
|
||||
{
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
public class PrintUtil
|
||||
public class PrintUtil
|
||||
{
|
||||
/**
|
||||
* Print a linked list
|
||||
* @param head
|
||||
*/
|
||||
public static void PrintLinkedList(ListNode head)
|
||||
{
|
||||
/**
|
||||
* Print a linked list
|
||||
* @param head
|
||||
*/
|
||||
public static void PrintLinkedList(ListNode head)
|
||||
List<String> list = new();
|
||||
while (head != null)
|
||||
{
|
||||
List<String> list = new();
|
||||
while (head != null)
|
||||
{
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
}
|
||||
Console.Write(String.Join(" -> ", list));
|
||||
}
|
||||
|
||||
/**
|
||||
* The interface of the tree printer
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
* @param root
|
||||
*/
|
||||
public static void PrintTree(TreeNode? root)
|
||||
{
|
||||
PrintTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
public static void PrintTree(TreeNode? root, Trunk? prev, bool isLeft)
|
||||
{
|
||||
if (root == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String prev_str = " ";
|
||||
Trunk trunk = new Trunk(prev, prev_str);
|
||||
|
||||
PrintTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null)
|
||||
{
|
||||
trunk.str = "———";
|
||||
}
|
||||
else if (isLeft)
|
||||
{
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else
|
||||
{
|
||||
trunk.str = "\\———";
|
||||
prev.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
Console.WriteLine(" " + root.val);
|
||||
|
||||
if (prev != null)
|
||||
{
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = " |";
|
||||
|
||||
PrintTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to print branches of the binary tree
|
||||
* @param p
|
||||
*/
|
||||
public static void showTrunks(Trunk? p)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
Console.Write(p.str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a hash map
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
* @param map
|
||||
*/
|
||||
public static void printHashMap<K, V>(Dictionary<K, V> map) where K : notnull
|
||||
{
|
||||
foreach (var kv in map.Keys)
|
||||
{
|
||||
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
|
||||
}
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
}
|
||||
Console.Write(String.Join(" -> ", list));
|
||||
}
|
||||
|
||||
/**
|
||||
* The interface of the tree printer
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
* @param root
|
||||
*/
|
||||
public static void PrintTree(TreeNode? root)
|
||||
{
|
||||
PrintTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
public static void PrintTree(TreeNode? root, Trunk? prev, bool isLeft)
|
||||
{
|
||||
if (root == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String prev_str = " ";
|
||||
Trunk trunk = new Trunk(prev, prev_str);
|
||||
|
||||
PrintTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null)
|
||||
{
|
||||
trunk.str = "———";
|
||||
}
|
||||
else if (isLeft)
|
||||
{
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else
|
||||
{
|
||||
trunk.str = "\\———";
|
||||
prev.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
Console.WriteLine(" " + root.val);
|
||||
|
||||
if (prev != null)
|
||||
{
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = " |";
|
||||
|
||||
PrintTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to print branches of the binary tree
|
||||
* @param p
|
||||
*/
|
||||
public static void showTrunks(Trunk? p)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
Console.Write(p.str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a hash map
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
* @param map
|
||||
*/
|
||||
public static void printHashMap<K, V>(Dictionary<K, V> map) where K : notnull
|
||||
{
|
||||
foreach (var kv in map.Keys)
|
||||
{
|
||||
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,95 +4,94 @@
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.include
|
||||
namespace hello_algo.include;
|
||||
|
||||
public class TreeNode
|
||||
{
|
||||
public class TreeNode
|
||||
public int val; // 结点值
|
||||
public int height; // 结点高度
|
||||
public TreeNode? left; // 左子结点引用
|
||||
public TreeNode? right; // 右子结点引用
|
||||
|
||||
public TreeNode(int x)
|
||||
{
|
||||
public int val; // 结点值
|
||||
public int height; // 结点高度
|
||||
public TreeNode? left; // 左子结点引用
|
||||
public TreeNode? right; // 右子结点引用
|
||||
val = x;
|
||||
}
|
||||
|
||||
public TreeNode(int x)
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? ArrToTree(int?[] arr)
|
||||
{
|
||||
if (arr.Length == 0 || arr[0] == null)
|
||||
return null;
|
||||
|
||||
TreeNode root = new TreeNode((int)arr[0]);
|
||||
Queue<TreeNode> queue = new Queue<TreeNode>();
|
||||
queue.Enqueue(root);
|
||||
int i = 0;
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? ArrToTree(int?[] arr)
|
||||
{
|
||||
if (arr.Length == 0 || arr[0] == null)
|
||||
return null;
|
||||
|
||||
TreeNode root = new TreeNode((int) arr[0]);
|
||||
Queue<TreeNode> queue = new Queue<TreeNode>();
|
||||
queue.Enqueue(root);
|
||||
int i = 0;
|
||||
while (queue.Count != 0)
|
||||
TreeNode node = queue.Dequeue();
|
||||
if (++i >= arr.Length) break;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
TreeNode node = queue.Dequeue();
|
||||
if (++i >= arr.Length) break;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
node.left = new TreeNode((int) arr[i]);
|
||||
queue.Enqueue(node.left);
|
||||
}
|
||||
if (++i >= arr.Length) break;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
node.right = new TreeNode((int) arr[i]);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
node.left = new TreeNode((int)arr[i]);
|
||||
queue.Enqueue(node.left);
|
||||
}
|
||||
if (++i >= arr.Length) break;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
node.right = new TreeNode((int)arr[i]);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public static List<int?> TreeToList(TreeNode root)
|
||||
{
|
||||
List<int?> list = new();
|
||||
if (root == null) return list;
|
||||
Queue<TreeNode?> queue = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
TreeNode? node = queue.Dequeue();
|
||||
if (node != null)
|
||||
{
|
||||
list.Add(node.val);
|
||||
queue.Enqueue(node.left);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? GetTreeNode(TreeNode? root, int val)
|
||||
{
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public static List<int?> TreeToList(TreeNode root)
|
||||
{
|
||||
List<int?> list = new();
|
||||
if (root == null) return list;
|
||||
Queue<TreeNode?> queue = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
TreeNode? node = queue.Dequeue();
|
||||
if (node != null)
|
||||
{
|
||||
list.Add(node.val);
|
||||
queue.Enqueue(node.left);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? GetTreeNode(TreeNode? root, int val)
|
||||
{
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
TreeNode? left = GetTreeNode(root.left, val);
|
||||
TreeNode? right = GetTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
TreeNode? left = GetTreeNode(root.left, val);
|
||||
TreeNode? right = GetTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user