File tree Expand file tree Collapse file tree 1 file changed +36
-1
lines changed
SwiftyLeetCode.playground Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Original file line number Diff line number Diff line change 1
- //: Playground - noun: a place where people can play
1
+ // Leetcode solutions in Swift.
2
+ // Some solutions are from:
3
+ // https://github.com/soapyigu/LeetCode_Swift
2
4
3
5
import UIKit
4
6
5
7
class Solution { }
6
8
9
+ /**
10
+ * 1. Two Sum
11
+ * https://leetcode.com/problems/two-sum/
12
+ */
13
+
14
+ extension Solution {
15
+ class func twoSum( nums: [ Int ] , _ target: Int ) -> [ Int ] {
16
+
17
+ guard nums. count > 1 else { return [ ] }
18
+
19
+ var map = [ Int: Int] ( )
20
+
21
+ for i in 0 ..< nums. count {
22
+ if let value = map [ nums [ i] ] {
23
+ return [ value + 1 , i + 1 ]
24
+ }
25
+
26
+ map [ target - nums[ i] ] = i
27
+ }
28
+
29
+ return [ ]
30
+ }
31
+ }
32
+
33
+ /*:
34
+ ### Usage
35
+ */
36
+
37
+ let twoSumNums = [ 2 , 7 , 11 , 15 ]
38
+ let target = 9
39
+
40
+ Solution . twoSum ( nums: twoSumNums, target)
41
+
7
42
/**
8
43
* 217. Contains Duplicate
9
44
* https://leetcode.com/problems/contains-duplicate/
You can’t perform that action at this time.
0 commit comments