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 class ListNode | |
{ | |
public int val; | |
public ListNode next; | |
public ListNode(int x) { val = x; next = null; } | |
} | |
public bool HasCycle(ListNode head) | |
{ | |
if (head == 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
public int RomanToInt(string s) | |
{ | |
int result = 0; | |
int count = s.Count(); | |
char setChar = '\b'; | |
for (int i = count - 1; i >= 0; i--) | |
{ | |
switch (s[i]) | |
{ | |
case '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
public int RomanToInt(string s) | |
{ | |
int result = 0; | |
switch (s) | |
{ | |
case "I": | |
result += 1; | |
break; | |
case "V": | |
result += 5; |
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
[TestClass] | |
public class No206Test_ReverseLinkedList | |
{ | |
private No206_ReverseLinkedList solution; | |
[TestInitialize] | |
public void Init() | |
{ | |
solution = new No206_ReverseLinkedList(); | |
} |
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 ListNode ReverseList(ListNode head) | |
{ | |
if (head == null || head.next == null) | |
return head; | |
ListNode tail = ReverseList(head.next); | |
head.next.next = head; | |
// 無這行對中間的node無影響,但需指定這行才可使原本的head最終指向null | |
head.next = null; | |
return tail; | |
} |
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 ListNode ReverseList(ListNode head) | |
{ | |
if (head == null || head.next == null) | |
return head; | |
ListNode curr = head; | |
ListNode pre = null; | |
ListNode next = curr.next; | |
head.next = null; | |
while(next.next != 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
[TestClass] | |
public class No206Test_ReverseLinkedList | |
{ | |
private No206_ReverseLinkedList solution; | |
[TestInitialize] | |
public void Init() | |
{ | |
solution = new No206_ReverseLinkedList(); | |
} |
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 ArrayContain2SameHugeNumber_ReturnTrue() | |
{ | |
int[] array = new int[2] { -1200000005, -1200000005 }; | |
bool isDuplicate = solution.ContainsDuplicate(array); | |
Assert.IsTrue(isDuplicate); | |
} | |
[TestMethod] | |
public void Array2_14_18_22_22_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 bool ContainsDuplicate(int[] nums) | |
{ | |
int distinctNums = nums.Distinct().Count(); | |
int originalNums = nums.Count(); | |
return distinctNums != originalNums; | |
} |
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 ArrayContain0andNagativeNoDuplicate_ReturnFalse() | |
{ | |
int[] array = new int[5] { 1, 5, -2, -4, 0 }; | |
bool isDuplicate = solution.ContainsDuplicate(array); | |
Assert.IsFalse(isDuplicate); | |
} |