This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private No217_ContainsDuplicate solution; | |
[TestInitialize] | |
public void Init() | |
{ | |
solution = new No217_ContainsDuplicate(); | |
} | |
[TestMethod] | |
public void Array1231_ReturnTrue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void NoNode_Return0() | |
{ | |
int depth = solution.MaxDepth(null); | |
Assert.AreEqual(0, depth); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int MaxDepth(TreeNode root) | |
{ | |
if (root.left == null && root.right == null) | |
return 1; | |
else | |
throw new NotImplementedException(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void OneNode_Return1() | |
{ | |
No104_MaximumDepthofBinaryTree.TreeNode root = new No104_MaximumDepthofBinaryTree.TreeNode(1); | |
int depth = solution.MaxDepth(root); | |
Assert.AreEqual(1, depth); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int LargestPalindrome(int n) | |
{ | |
// 相乘的最大位數(最小位數就再減一即可) | |
int maxDigits = n + n; | |
// 單一數字最大及最小值 | |
long maxValueOneNumber = (long)Math.Pow(10, n); | |
long minValueOneNumber = maxValueOneNumber / 10; | |
maxValueOneNumber -= 1; |