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[] GetSolution(int[] nums, int target) | |
{ | |
// First int means the excepted second value, the second int means the index of first value | |
Dictionary<int, int> exceptValueMap = new Dictionary<int, int>(); | |
for (int index1 = 0; index1 < nums.Count(); index1++) | |
{ | |
// the value in array matches the excepted value | |
if (exceptValueMap.ContainsKey(nums[index1])) | |
{ | |
return new int[] { exceptValueMap[nums[index1]], index1 }; |
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 No2_AddTwoNumbers | |
{ | |
/// <summary> | |
/// Definition for singly-linked list. | |
/// </summary> | |
public class ListNode | |
{ | |
public int val; | |
public ListNode next; | |
public ListNode(int x) { val = x; } |
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 No2Test_AddTwoNumbers | |
{ | |
private No2_AddTwoNumbers solution; | |
[TestInitialize] | |
public void Init() | |
{ | |
solution = new No2_AddTwoNumbers(); | |
} |
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 OneDigit_AddOneDigit_ReturnValueLessThan10_1_Plus_1_Equal2() | |
{ | |
No2_AddTwoNumbers.ListNode firstNumber = new No2_AddTwoNumbers.ListNode(1); | |
No2_AddTwoNumbers.ListNode secondNumber = new No2_AddTwoNumbers.ListNode(1); | |
No2_AddTwoNumbers.ListNode result = solution.AddTwoNumbers(firstNumber, secondNumber); | |
Assert.AreEqual(2, result.val); | |
Assert.IsTrue(result.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
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) | |
{ | |
int result = l1.val + l2.val; | |
return new ListNode(result); | |
} |
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 OneDigit_AddOneDigit_ReturnValueGreaterThan10_3_Plus_9_Equal12() | |
{ | |
No2_AddTwoNumbers.ListNode firstNumber = new No2_AddTwoNumbers.ListNode(3); | |
No2_AddTwoNumbers.ListNode secondNumber = new No2_AddTwoNumbers.ListNode(9); | |
No2_AddTwoNumbers.ListNode result = solution.AddTwoNumbers(firstNumber, secondNumber); | |
// result should be 12, first digit in linked list is 2, the second digit is 1 | |
Assert.AreEqual(2, result.val); |
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 AddTwoNumbers(ListNode l1, ListNode l2) | |
{ | |
int result = l1.val + l2.val; | |
if(result >= 10) | |
{ | |
return new ListNode(result % 10) | |
{ | |
next = new ListNode(result / 10) | |
}; | |
} |
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 OneDigit_AddTwoDigit_3_Plus_325_Equa328() | |
{ | |
No2_AddTwoNumbers.ListNode firstNumber = new No2_AddTwoNumbers.ListNode(3); | |
No2_AddTwoNumbers.ListNode secondNumber = | |
new No2_AddTwoNumbers.ListNode(5) | |
{ | |
next = new No2_AddTwoNumbers.ListNode(2) | |
{ | |
next = new No2_AddTwoNumbers.ListNode(3) |
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 AddTwoNumbers(ListNode l1, ListNode l2) | |
{ | |
// first node of resultStartPoint is a start point(not real value) | |
ListNode resultStartPoint = new ListNode(-1); | |
ListNode resultPointer = resultStartPoint; | |
ListNode firstNumPointer = l1; | |
ListNode secondNumPointer = l2; | |
// both input lists shouldn't be null(at least contain one node), so we use do..while | |
do |
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 AddTwoNumbers(ListNode l1, ListNode l2) | |
{ | |
// first node of resultStartPoint is a start point(not real value) | |
ListNode resultStartPoint = new ListNode(-1); | |
ListNode resultPointer = resultStartPoint; | |
ListNode firstNumPointer = l1; | |
ListNode secondNumPointer = l2; | |
// restore carry value | |
int carryDigit = 0; |
OlderNewer