[Swift]LeetCode1 .兩數之和 | Two Sum

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-kmtveess-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array of integers, return indices of the two numbers such that they add up to a specific target.git

You may assume that each input would have exactly one solution, and you may not use the same element twice.github

Example:數組

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

給定一個整數數組和一個目標值,找出數組中和爲目標值的兩個數。你能夠假設每一個輸入只對應一種答案,且一樣的元素不能被重複利用。

示例:微信

給定 nums = [2, 7, 11, 15], target = 9
由於 nums[0] + nums[1] = 2 + 7 = 9
因此返回 [0, 1]

一遍哈希表

咱們能夠一次完成。在進行迭代並將元素插入到表中的同時,咱們還會回過頭來檢查表中是否已經存在當前元素所對應的目標元素。若是它存在,那咱們已經找到了對應解,並當即將其返回。app

Runtime: 32 ms
Memory Usage: 19.3 MB
 1 class Solution {
 2     func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
 3         var table:[Int:Int] = [Int:Int]()        
 4         for (firstIndex, num) in nums.enumerated() {
 5             var res: Int = target - num
 6             //可選連接
 7             if let secondIndex = table[res] 
 8             {
 9                 return [secondIndex,firstIndex]
10             } 
11             else 
12             {
13                 table[num] = firstIndex
14             }
15         }
16         return [-1,-1]
17     }
18 }

36ms 
 1 class Solution {
 2     func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
 3         var out: [Int] = []
 4        
 5         var dict:[Int: Int] = [:]     
 6         for (index,num) in nums.enumerated() {
 7             if let sum = dict[target - num] {
 8                 out.append(index)
 9                 out.append(sum)
10             } 
11             dict[num] = index
12         }
13         return out
14     }
15 }

 1 class Solution {
 2     func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
 3         for i in 0...nums.count-1 {
 4             if i == nums.count-1 { break; }
 5             for j in i+1...nums.count-1 {
 6                 let sum = nums[i] + nums[j]
 7                 if sum == target {
 8                     return [i, j]
 9                 }
10             }            
11         }
12         return [Int]()
13     }
14 }

 1 class Solution {
 2     typealias ArrayIndex = (array: [Int], startIndex: Int)
 3     
 4     func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
 5         var arrayTest = [ArrayIndex]()
 6         arrayTest.append(ArrayIndex(nums, 0))
 7         
 8         while arrayTest.count != 0 {
 9             let values = arrayTest.remove(at: 0)
10             let splitValues = splitArray(nums: values)
11             if let result = scanTwoArray(nums1: splitValues.nums1, nums2: splitValues.nums2, target: target) {
12                 return result
13             } else {
14                 arrayTest.append(splitValues.nums1)
15                 arrayTest.append(splitValues.nums2)
16             }
17         }
18         return [0,1]
19     }
20     
21     func splitArray(nums: ArrayIndex) -> (nums1: ArrayIndex, nums2: ArrayIndex) {
22         let ct = nums.array.count
23         let half = ct / 2
24         let leftSplit = nums.array[0 ..< half]
25         let rightSplit = nums.array[half ..< ct]
26         return ((Array(leftSplit), nums.startIndex), (Array(rightSplit), nums.startIndex + half))
27     }
28     
29     func scanTwoArray(nums1: ArrayIndex, nums2: ArrayIndex, target: Int) -> [Int]? {
30         for i in 0..<nums1.array.count {
31             for j in 0..<nums2.array.count {
32                 if nums1.array[i] + nums2.array[j] == target {
33                     return [i + nums1.startIndex, j + nums2.startIndex]
34                 }
35             }
36         }
37         
38         return nil
39     }
40 }
相關文章
相關標籤/搜索