Skip to content

Commit

Permalink
018-4sum
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 11, 2018
1 parent ff5a476 commit f30231a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 15 deletions.
30 changes: 15 additions & 15 deletions 015-3sum.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,6 @@ func threeSum(_ nums: [Int]) -> [[Int]] {
return Array(set).map { $0.toArray() }
}

class Tests: XCTestCase {
func testExample() {
let nums = [-1, 0, 1, 2, -1, -4]
let expected = [
[-1, 0, 1],
[-1, -1, 2],
]
let solutions = threeSum(nums)
XCTAssertEqual(solutions.count, expected.count)
XCTAssertEqual(Set(solutions), Set(expected))
}
}

Tests.defaultTestSuite.run()

func threeSum4_2(_ nums: [Int]) -> [[Int]] {
let nums = nums.sorted()
var set: Set<[Int]> = []
Expand All @@ -84,3 +69,18 @@ func threeSum4_2(_ nums: [Int]) -> [[Int]] {
}
return Array(set)
}

class Tests: XCTestCase {
func testExample() {
let nums = [-1, 0, 1, 2, -1, -4]
let expected = [
[-1, 0, 1],
[-1, -1, 2],
]
let solutions = threeSum(nums)
XCTAssertEqual(solutions.count, expected.count)
XCTAssertEqual(Set(solutions), Set(expected))
}
}

Tests.defaultTestSuite.run()
94 changes: 94 additions & 0 deletions 018-4sum.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// 18. 4Sum
/// Given an array nums of n integers and an integer target, are there elements a, b, c, and d in
/// nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the
/// sum of target.
///
/// - Note: The solution set must not contain duplicate quadruplets.

import XCTest

// Approach: Two Pointers
func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
// [Int] is not Hashable in LeetCode runtime (Requires Swift 4.2).
// This is a workaround. The Swift 4.2 solution is attached below.
guard nums.count > 3 else { return [] }

struct Four: Hashable {
let a: Int
let b: Int
let c: Int
let d: Int

init(_ a: Int, _ b: Int, _ c: Int, _ d: Int) {
self.a = a
self.b = b
self.c = c
self.d = d
}

func toArray() -> [Int] {
return [a, b, c, d]
}
}
let nums = nums.sorted()
var set: Set<Four> = []
for i in 0...nums.count - 4 {
for j in i+1...nums.count - 3 {
var head = j + 1
var tail = nums.count - 1
while head < tail {
let sum = nums[i] + nums[j] + nums[head] + nums[tail]
if sum == target {
set.insert(Four(nums[i], nums[j], nums[head], nums[tail]))
head += 1
tail -= 1
} else if sum > target {
tail -= 1
} else {
head += 1
}
}
}
}
return Array(set).map { $0.toArray() }
}

func fourSum4_2(_ nums: [Int], _ target: Int) -> [[Int]] {
let nums = nums.sorted()
var set: Set<[Int]> = []
for i in 0...nums.count - 3 {
for j in i+1...nums.count - 3 {
var head = j + 1
var tail = nums.count - 1
while head < tail {
let sum = nums[i] + nums[j] + nums[head] + nums[tail]
if sum == target {
set.insert([nums[i], nums[j], nums[head], nums[tail]])
head += 1
tail -= 1
} else if sum > target {
tail -= 1
} else {
head += 1
}
}
}
}
return Array(set)
}

class Tests: XCTestCase {
func testExample() {
let nums = [1, 0, -1, 0, -2, 2]
let expected = [
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2],
]
let solutions = fourSum(nums, 0)
XCTAssertEqual(solutions.count, expected.count)
XCTAssertEqual(Set(solutions), Set(expected))
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 018-4sum.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
15 | [Three Sum](https://leetcode.com/problems/3sum/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/015-3sum.playground/Contents.swift)
16 | [Three Sum Closest](https://leetcode.com/problems/3sum-closest/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/016-3sum-closest.playground/Contents.swift)
17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/017-letter-combinations-of-a-phone-number.playground/Contents.swift)
18 | [Four Sum](https://leetcode.com/problems/4sum/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/018-4sum.playground/Contents.swift)

0 comments on commit f30231a

Please sign in to comment.