-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented 'Find the Array Concatenation Value' challenge
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
LeetCode/LeetCode/Challenges/FindTheArrayConcatenationValue.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,45 @@ | ||
// | ||
// FindTheArrayConcatenationValue.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 11/06/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/find-the-array-concatenation-value/ | ||
struct FindTheArrayConcatenationValue { | ||
|
||
//Time: O(n) where n is the number of elements in `nums` | ||
//Space: O(1) | ||
//array | ||
//two pointers | ||
//string | ||
// | ||
//Solution Description: | ||
//Using two pointers we work out->in. We take the int values at each pointer and concatenate them into a string and then | ||
//into a single int value which we then add to `result`. We repeat this process until `p1` crosses `p2`, if `p1` is equal | ||
//to `p2` then it means that `nums` has an odd number of elements and we take that final element and add it `result`. | ||
func findTheArrayConcVal(_ nums: [Int]) -> Int { | ||
var result = 0 | ||
|
||
var p1 = 0 | ||
var p2 = nums.count - 1 | ||
|
||
while p1 < p2 { | ||
let strValue = "\(nums[p1])\(nums[p2])" | ||
let intValue = Int(strValue)! | ||
|
||
result += intValue | ||
|
||
p1 += 1 | ||
p2 -= 1 | ||
} | ||
|
||
if p1 == p2 { | ||
result += nums[p1] | ||
} | ||
|
||
return result | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
LeetCode/LeetCodeTests/Tests/FindTheArrayConcatenationValueTests.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,31 @@ | ||
// | ||
// FindTheArrayConcatenationValueTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 11/06/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class FindTheArrayConcatenationValueTests: XCTestCase { | ||
|
||
// MARK: - Tests | ||
|
||
func test_A() { | ||
let nums = [7,52,2,4] | ||
|
||
let result = FindTheArrayConcatenationValue().findTheArrayConcVal(nums) | ||
|
||
XCTAssertEqual(result, 596) | ||
} | ||
|
||
func test_B() { | ||
let nums = [5,14,13,8,12] | ||
|
||
let result = FindTheArrayConcatenationValue().findTheArrayConcVal(nums) | ||
|
||
XCTAssertEqual(result, 673) | ||
} | ||
} |