-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Recursive solutions to AddTwoNumbers
- Loading branch information
Showing
6 changed files
with
152 additions
and
23 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Sources/Solutions/002 - Add Two Numbers/AddTwoNumbersRecursiveHelperSolution.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// AddTwoNumbersRecursiveHelperSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
import Core | ||
import Foundation | ||
import Problems | ||
|
||
final class AddTwoNumbersRecursiveHelperSolution: AddTwoNumbersDefinition { | ||
|
||
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { | ||
addTwoNumbers(l1, l2, 0) | ||
} | ||
|
||
private func addTwoNumbers( | ||
_ l1: ListNode?, | ||
_ l2: ListNode?, | ||
_ carry: Int | ||
) -> ListNode? { | ||
|
||
guard l1 != nil || l2 != nil || carry > 0 else { return nil } | ||
|
||
let sum = (l1?.val ?? 0) + (l2?.val ?? 0) + carry | ||
let resultNode = ListNode(sum % 10) | ||
resultNode.next = addTwoNumbers(l1?.next, l2?.next, sum / 10) | ||
|
||
return resultNode | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Sources/Solutions/002 - Add Two Numbers/AddTwoNumbersRecursiveSolution.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// AddTwoNumbersRecursiveSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
import Core | ||
import Foundation | ||
import Problems | ||
|
||
final class AddTwoNumbersRecursiveSolution: AddTwoNumbersDefinition { | ||
|
||
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { | ||
guard let l1 else { return l2 } | ||
guard let l2 else { return l1 } | ||
|
||
let sum = l1.val + l2.val | ||
let resultNode = ListNode(sum % 10) | ||
let nextNode = addTwoNumbers(l1.next, l2.next) | ||
if sum > 9 { | ||
resultNode.next = addTwoNumbers(nextNode, ListNode(1)) | ||
} else { | ||
resultNode.next = nextNode | ||
} | ||
|
||
return resultNode | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
Tests/SolutionsTests/002 - Add Two Numbers/AddTwoNumbersRecursiveHelperSolutionTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// AddTwoNumbersRecursiveHelperSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class AddTwoNumbersRecursiveHelperSolutionTests: XCTestCase { | ||
|
||
let solution: AddTwoNumbersDefinition = AddTwoNumbersRecursiveHelperSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.addTwoNumbers(input.l1, input.l2) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension AddTwoNumbersRecursiveHelperSolutionTests: AddTwoNumbersTestCaseProvider { } |
28 changes: 28 additions & 0 deletions
28
Tests/SolutionsTests/002 - Add Two Numbers/AddTwoNumbersRecursiveSolutionTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// AddTwoNumbersRecursiveSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class AddTwoNumbersRecursiveSolutionTests: XCTestCase { | ||
|
||
let solution: AddTwoNumbersDefinition = AddTwoNumbersRecursiveSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.addTwoNumbers(input.l1, input.l2) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension AddTwoNumbersRecursiveSolutionTests: AddTwoNumbersTestCaseProvider { } |
34 changes: 34 additions & 0 deletions
34
Tests/SolutionsTests/002 - Add Two Numbers/AddTwoNumbersTestCaseProvider.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// AddTwoNumbersTestCaseProvider.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
@testable import Core | ||
@testable import TestSupport | ||
|
||
protocol AddTwoNumbersTestCaseProvider: TestCaseProviding { } | ||
|
||
extension AddTwoNumbersTestCaseProvider { | ||
|
||
func validateInput(_ input: (l1: ListNode?, l2: ListNode?)) -> Bool { | ||
// TODO: Implement validation | ||
true | ||
} | ||
|
||
var data: [TestData<(l1: ListNode?, l2: ListNode?), ListNode?>] { [ | ||
TestData( | ||
input: ([2, 4, 3].listNode, [5, 6, 4].listNode), | ||
expectedOutput: [7, 0, 8].listNode | ||
), | ||
TestData( | ||
input: ([0].listNode, [0].listNode), | ||
expectedOutput: [0].listNode | ||
), | ||
TestData( | ||
input: ([9, 9, 9, 9, 9, 9, 9].listNode, [9, 9, 9, 9].listNode), | ||
expectedOutput: [8, 9, 9, 9, 0, 0, 0, 1].listNode | ||
) | ||
] } | ||
} |