1. 程式人生 > >[Swift]LeetCode15. 三數之和 | 3Sum

[Swift]LeetCode15. 三數之和 | 3Sum

Given an array nums of n integers, are there elements abc in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。

注意:答案中不可以包含重複的三元組。

例如, 給定陣列 nums = [-1, 0, 1, 2, -1, -4],

滿足要求的三元組集合為:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

116ms
 1 class Solution {
 2     func threeSum(_ nums: [Int]) -> [[Int]] {
 3         guard nums.count > 2 else { return [] }
 4         var solutions = [[Int]]();
 5         let sorted = nums.sorted() { $0 < $1 }
 6         let count = sorted.count
 7         var i = 0
 8         
 9
while (i < count - 2) { 10 if (i == 0 || (i > 0 && sorted[i] != sorted[i - 1])) { 11 var left = i + 1 12 var right = count - 1 13 let num = sorted[i] 14 15 while (left < right) { 16 let currentSum = sorted[left] + sorted[right] + num 17 18 if (currentSum == 0) { 19 solutions.append([sorted[left], sorted[right], num]) 20 21 while (left < right && sorted[left] == sorted[left + 1]) { 22 left += 1 23 } 24 25 while (left < right && sorted[right] == sorted[right - 1]) { 26 right -= 1 27 } 28 left += 1 29 right -= 1 30 } else if (currentSum < 0) { 31 left += 1 32 } else { 33 right -= 1 34 } 35 } 36 } 37 38 i += 1 39 } 40 41 return solutions 42 } 43 }

120ms

 1 class Solution {
 2     func threeSum(_ nums: [Int]) -> [[Int]] {
 3     guard nums.count > 2 else {
 4         return []
 5     }
 6 
 7     var results = [[Int]]()
 8     let sortedNums = nums.sorted()
 9 
10     for i in 0..<sortedNums.count-1 {
11         if i > 0 && sortedNums[i] == sortedNums[i-1] {
12             continue
13         }
14         let target = 0 - sortedNums[i]
15         var low = i + 1
16         var high = nums.count - 1
17 
18         while low < high {
19             let sum = sortedNums[low] + sortedNums[high]
20             if sum == target {
21                 let result = [sortedNums[i], sortedNums[low], sortedNums[high]]
22                 results.append(result)
23 
24                 while (low < high && sortedNums[low] == sortedNums[low+1]) {
25                     low += 1
26                 }
27                 while (low < high && sortedNums[high] == sortedNums[high-1]) {
28                     high -= 1
29                 }
30                 low += 1
31                 high -= 1
32             } else if sum < target {
33                 low += 1
34             } else {
35                 high -= 1
36             }
37         }
38     }
39 
40     return results
41 }
42 }

124ms

 1 class Solution {
 2     func threeSum(_ nums: [Int]) -> [[Int]] {
 3         if nums.count < 3 {
 4             return []
 5         }
 6         
 7         var result = [[Int]]()
 8         var snums = nums.sorted()
 9         
10         for i in 0...snums.count-2 {
11             if i > 0 && snums[i] == snums[i-1] {
12                 continue
13             }
14             
15             var l = i + 1
16             var r = snums.count - 1
17             
18             while l < r {
19                 let s = snums[i] + snums[l] + snums[r]
20                 
21                 if s == 0 {
22                     result.append([snums[i], snums[l], snums[r]])
23                     l += 1
24                     r -= 1
25                     
26                     while l < r && snums[l] == snums[l-1] {
27                         l += 1
28                     }
29                     
30                     while l < r && snums[r] == snums[r+1] {
31                         r -= 1
32                     }
33                 } else if s < 0 {
34                     l += 1
35                 } else {
36                     r -= 1
37                 }
38             }
39         }
40         
41         return result
42     }
43 }

136ms

 1 class Solution {
 2     func threeSum(_ nums: [Int]) -> [[Int]] {
 3         var nums = nums.sorted()
 4         var result = [[Int]]()
 5         
 6         for i in 0..<nums.count {
 7             if i == 0 || i > 0 && nums[i] != nums[i-1] {
 8                 var left = i + 1, right = nums.count - 1
 9                 var sum = 0 - nums[i]
10                 print(sum)
11                 while left < right {
12                    if nums[left] + nums[right] == sum {
13                        result.append([nums[left], nums[right], nums[i]])
14                        while left < right && nums[left] == nums[left + 1]{
15                                left += 1
16                        }
17                         while left < right && nums[right] == nums[right - 1]{
18                                right -= 1
19                        }
20                        left += 1
21                        right -= 1
22                    } else if nums[left] + nums[right] < sum {
23                        left += 1
24                    } else {
25                        right -= 1
26                    }
27                 }   
28             }
29         }
30         return result
31     }
32 }

148ms

 1 import Foundation
 2 class Solution {
 3     func threeSum(_ nums: [Int]) -> [[Int]] {
 4         
 5         var n = nums
 6         n.sort()
 7         
 8         var aIndex1: Int = 0
 9         var aIndexLow: Int = 0
10         var aIndexHi: Int = 0
11         
12         var aResult = [[Int]]()
13         
14         var aTargetSum: Int = 0
15         
16         while aIndex1 < (n.count - 2) {
17             
18             //The sandwich principle.
19             aIndexLow = aIndex1 + 1
20             aIndexHi = n.count - 1
21             
22             while (aIndexLow < aIndexHi) && (n[aIndex1] + n[aIndexLow]) <= aTargetSum {
23                 
24                 var aSum = n[aIndex1] + n[aIndexLow] + n[aIndexHi]
25                 if aSum == aTargetSum {
26                     var aArr = [n[aIndex1], n[aIndexLow], n[aIndexHi]]
27                     aResult.append(aArr)
28                     
29                     aIndexHi -= 1
30                     //Prevent dupes on hi
31                     while aIndexLow < aIndexHi && n[aIndexHi + 1] == n[aIndexHi] {
32                         aIndexHi -= 1
33                     }
34                     
35                     //Prevent dupes on low
36                     aIndexLow += 1
37                     while aIndexLow < aIndexHi && n[aIndexLow - 1] == n[aIndexLow] {
38                         aIndexLow += 1
39                     }  
40                 } else if aSum > aTargetSum {
41                     aIndexHi -= 1
42                 } else {
43                     aIndexLow += 1   
44                 }
45             }
46             
47             //prevent dupes with first index.
48             aIndex1 += 1
49             while aIndex1 < n.count && n[aIndex1 - 1] == n[aIndex1] {
50                 aIndex1 += 1
51             }
52         }
53         
54         return aResult
55     }
56 }

164ms

 1 class Solution {
 2     func twoSum(_ array: [Int], ignoreIndex i: Int, result: inout [[Int]]) {
 3         
 4         var l = i + 1
 5         
 6         let a = array[i]
 7         
 8         var r = array.count - 1
 9         
10         if i > 0 && a == array[i - 1] {
11             return 
12         }
13         
14         while l < r {
15             
16             let b = array[l]
17         
18             if a + b > 0 {
19                 return  
20             }
21             
22             let c = array[r]
23             
24             var sum = a + b + c
25             
26             if sum == 0 {
27                 let indexes = [a, b, c]
28                 
29                 result.append(indexes)
30                 
31                 l += 1
32                                 
33                 while l < r && array[l] == b {
34                     l += 1
35                 }
36                 
37             } else if sum < 0 {
38                 l += 1
39                                 
40                 while l < r && array[l] == b {
41                     l += 1
42                 }
43             } else if sum > 0 {  
44                 r -= 1
45                 
46                 while l < r && array[r] == c {
47                     r -= 1
48                 }
49             }
50         }
51         
52     }
53     
54     func threeSum(_ nums: [Int]) -> [[Int]] {
55         
56         var result = [[Int]]()
57         
58         if nums.count < 3 {
59             return result
60         }
61         
62         let array = nums.sorted {
63             return $0 < $1
64         }
65         
66         for i in 0..<array.count - 2 {
67             if array[i] > 0 {
68                 break
69             }
70             
71             twoSum(array, ignoreIndex: i, result: &result)
72         }
73         
74         return result
75     }
76 }