Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift warmup 2 sum & 3 sum

Johnlin
November 22, 2016

Swift warmup 2 sum & 3 sum

Johnlin

November 22, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. 1. Two Sum • https://leetcode.com/problems/two-sum/ • Given an array of

    integers, return indices of the two numbers such that they add up to a specific target. • څ㟬Ұݸ Array త੔ᏐɼଖதၷݸᏐࣈՃىိ။౳ԙ 㠥֎ҰݸಛఆᏐࣈɼճၚಹၷݸᏐࣈత Index
  2. • You may assume that each input would have exactly

    one solution. • 㟬ՄҎ၊ઃ༌ೖతArray ୞။༗߶޷Ұݸ౴Ҋɻ
  3. • ༌ೖ: nums = [2, 7, 11, 15], target =

    9 • ༌ग़: [0,1] • nums[0] = 2, nums[1] = 7, 2 + 7 = 9
  4. class Solution {
 func twoSum(_ nums: [Int], _ target: Int)

    -> [Int] { for (i, a) in nums.enumerated() { for (j, b) in nums.enumerated() { guard i != j else { continue } if a+b == target { return [i,j] } } } return [] }
 }
  5. ԋࢉ๏ • Ұݸݸ૊߹ຫຫࢼ => ᙛ༗ N ݸᏐࣈత࣌ީɼबཁ ࢼ N *

    N ݸ૊߹ • ೺ଞ၇Ճىိɼ؃؃༗ᔒ༗౳ԙ໨ඪᏐࣈ • ૬౳త࿩बճၚ౴Ҋ
  6. ԋࢉ๏ v2 • Ұݸݸ૊߹ຫຫࢼ • ೺Ꮠࣈ࿨૬ሣጯతindex ଘ౸ dictionary ཫ •

    ሣԙ㑌ҰݸᏐࣈ Xɼፙ؃؃ (໨ඪᏐࣈ - X) ༗ᔒ༗ࡏ dictionary ཫɼࣕ׌㠥ҰݸᏐࣈత index ࿨ X త index ෆಉɻ • ੒ޭత࿩बճၚ౴Ҋ • ༗ N ݸᏐࣈత࿩୞ཁࢉ N + N = 2N ࣍
  7. class Solution { func twoSum(_ nums: [Int], _ target: Int)

    -> [Int] { var m:[Int:Int] = [:] for (i,n) in nums.enumerated() { m[n] = i } for (i, x) in nums.enumerated() { guard let j = m[target-x] else { continue } guard j != i else { continue } return [i,j] } return [] } }
  8. 15. 3Sum • https://leetcode.com/problems/3sum/ • Given an array S of

    n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. • څ㟬Ұݸ੔Ꮠ Arrayɼፙग़ཫ໘༗ᔒ༗ࡾݸᏐࣈՃى ိ߶޷ੋ 0ɼճၚॴ༗ՄೳతᏐࣈ૊߹ʢෆੋ index)
  9. • Note: The solution set must not contain duplicate triplets.

    • උḼɿղ౴ෆೳ༗ॏෳత૊߹
  10. • ༌ೖ: nums = [-1, 0, 1, 2, -1, -4]

    • ༌ग़: [
 [-1,0,1],
 [-1,-1,2]
 ] • -1+0+1 = 0 , -1 + (-1) + 2 = 0
  11. • [ -4, -1, -1, 0, 1, 2 ] -4

    + (-1) + 2 = -3 < 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  12. • [ -4, -1, -1, 0, 1, 2 ] -4

    + 0 + 2 = -2 < 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  13. • [ -4, -1, -1, 0, 1, 2 ] -4

    + 1 + 2 = -1 < 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  14. • [ -4, -1, -1, 0, 1, 2 ] -1

    + (-1) + 2 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  15. • [ -4, -1, -1, 0, 1, 2 ] -1

    + 0 + 1 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  16. • [ -4, -1, -1, 0, 1, 2 ] 0

    + 1 + 2 = 3 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  17. • [-5, 0, -1, 2, -1, 6, 1]
 => [

    -5, -1, -1, 0, 1, 2, 6]
  18. • [ -5, -1, -1, 0, 1, 2, 6] -5

    + (-1) + 6 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  19. • [ -5, -1, -1, 0, 1, 2, 6] -5

    + 0 + 6 = 1 > 0 ჩߟᴍ খᏐࣈେᏐࣈ
  20. • [ -5, -1, -1, 0, 1, 2, 6] -5

    + 0 + 2 = -3 < 0 ჩߟᴍ খᏐࣈେᏐࣈ
  21. • [ -5, -1, -1, 0, 1, 2, 6] -5

    + 1 + 2 = -2 < 0 ჩߟᴍ খᏐࣈେᏐࣈ
  22. • [ -5, -1, -1, 0, 1, 2, 6] -1+

    (-1) + 6 = 4 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  23. • [ -5, -1, -1, 0, 1, 2, 6] -1+

    (-1) + 2 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  24. • [ -5, -1, -1, 0, 1, 2, 6] -1+

    0+ 1 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  25. • [ -5, -1, -1, 0, 1, 2, 6] 0+

    1 + 6 = 7 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  26. • [ -5, -1, -1, 0, 1, 2, 6] 0+

    1 + 2 = 3 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  27. • [ -5, -1, -1, 0, 1, 2, 6] 1

    + 2 + 6 = 9 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ
  28. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ഉং
  29. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং
  30. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং <= খᏐࣈ
  31. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ
  32. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ૊
  33. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ૊ <= ҠಈখᏐࣈ
  34. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ૊ <= ҠಈখᏐࣈ <= ҠಈେᏐࣈ
  35. class Solution1 { func threeSum1(_ nums: [Int]) -> [[Int]] {

    let n = nums.sorted() var i = n.startIndex var res:[[Int]] = [] while (i<n.count-2) { let a = n[i] var j = i+1 var k = n.count - 1 while (j < k) { let b = n[j] let c = n[k] let sum = a+b+c if(sum == 0) { res.append([n[i], n[j], n[k]]) } if(sum<=0) {while(j<k && n[j] == b) {j+=1} } if(sum>=0) {while(j<k && n[k] == c) {k-=1} } } while(i < n.count && n[i] == a) { i+=1 } } return res } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ૊ <= ҠಈখᏐࣈ <= ҠಈେᏐࣈ <= Ҡಈჩߟᴍ
  36. Q&A