Skip to content

Instantly share code, notes, and snippets.

public bool ContainsDuplicate(int[] nums)
{
int count = nums.Count();
if (count == 0) return false;
int max = nums.Max();
int min = nums.Min();
int[] positiveRecord = new int[max + 1];
int[] negativeRecord = new int[(min < 0) ? Math.Abs(min) + 1 : 0];
for (int i = 0; i < count; i++)
{
private No217_ContainsDuplicate solution;
[TestInitialize]
public void Init()
{
solution = new No217_ContainsDuplicate();
}
[TestMethod]
public void Array1231_ReturnTrue()
public int MaxDepth(TreeNode root)
{
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
int maxDepthLeft = 0, maxDepthRight = 0;
if (root.left != null)
maxDepthLeft = 1 + MaxDepth(root.left);
if (root.right != null)
[TestMethod]
public void NoNode_Return0()
{
int depth = solution.MaxDepth(null);
Assert.AreEqual(0, depth);
}
[TestMethod]
public void Tree11101010010_Return4()
{
No104_MaximumDepthofBinaryTree.TreeNode root = new No104_MaximumDepthofBinaryTree.TreeNode(1);
root.left = new No104_MaximumDepthofBinaryTree.TreeNode(1);
root.right = new No104_MaximumDepthofBinaryTree.TreeNode(1);
root.left.right = new No104_MaximumDepthofBinaryTree.TreeNode(1);
root.right.right = new No104_MaximumDepthofBinaryTree.TreeNode(1);
root.left.right.left = new No104_MaximumDepthofBinaryTree.TreeNode(1);
int depth = solution.MaxDepth(root);
public int MaxDepth(TreeNode root)
{
if (root.left == null && root.right == null)
return 1;
int maxDepthLeft = 0, maxDepthRight = 0;
if (root.left != null)
maxDepthLeft = 1 + MaxDepth(root.left);
if (root.right != null)
maxDepthRight = 1 + MaxDepth(root.right);
if (maxDepthLeft > maxDepthRight)
[TestMethod]
public void RootHasLeftChild_Return2()
{
No104_MaximumDepthofBinaryTree.TreeNode root = new No104_MaximumDepthofBinaryTree.TreeNode(1);
root.left = new No104_MaximumDepthofBinaryTree.TreeNode(1);
int depth = solution.MaxDepth(root);
Assert.AreEqual(2, depth);
}
[TestMethod]
public int MaxDepth(TreeNode root)
{
if (root.left == null && root.right == null)
return 1;
else
throw new NotImplementedException();
}
[TestMethod]
public void OneNode_Return1()
{
No104_MaximumDepthofBinaryTree.TreeNode root = new No104_MaximumDepthofBinaryTree.TreeNode(1);
int depth = solution.MaxDepth(root);
Assert.AreEqual(1, depth);
}
public int LargestPalindrome(int n)
{
// 相乘的最大位數(最小位數就再減一即可)
int maxDigits = n + n;
// 單一數字最大及最小值
long maxValueOneNumber = (long)Math.Pow(10, n);
long minValueOneNumber = maxValueOneNumber / 10;
maxValueOneNumber -= 1;