Share
Embed
Transcript
43 slides
-
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 -
• You may assume that each input would have exactly
one solution. • 㟬ՄҎ၊ઃ༌ೖతArray ။༗߶ҰݸҊɻ -
• ༌ೖ: nums = [2, 7, 11, 15], target =
9 • ༌ग़: [0,1] • nums[0] = 2, nums[1] = 7, 2 + 7 = 9 -
ࠎՍ class Solution { func twoSum(_ nums: [Int], _ target:
Int) -> [Int] { } } -
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 [] } } -
ԋࢉ๏ • Ұݸݸ߹ຫຫࢼ => ᙛ༗ N ݸᏐࣈత࣌ީɼबཁ ࢼ N *
N ݸ߹ • ଞ၇Ճىိɼ༗ᔒ༗ԙඪᏐࣈ • ૬తबճၚҊ -
ԋࢉ๏ v2 • Ұݸݸ߹ຫຫࢼ • Ꮠࣈ૬ሣጯతindex ଘ౸ dictionary ཫ •
ሣԙ㑌ҰݸᏐࣈ Xɼፙ (ඪᏐࣈ - X) ༗ᔒ༗ࡏ dictionary ཫɼࣕ㠥ҰݸᏐࣈత index X త index ෆಉɻ • ޭతबճၚҊ • ༗ N ݸᏐࣈతཁࢉ N + N = 2N ࣍ -
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 [] } } -
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) -
• Note: The solution set must not contain duplicate triplets.
• උḼɿղෆೳ༗ॏෳత߹ -
• ༌ೖ: nums = [-1, 0, 1, 2, -1, -4]
• ༌ग़: [ [-1,0,1], [-1,-1,2] ] • -1+0+1 = 0 , -1 + (-1) + 2 = 0 -
• [-1, 0, 1, 2, -1, -4] => [ -4,
-1, -1, 0, 1, 2 ] -
• [ -4, -1, -1, 0, 1, 2 ] -4
+ (-1) + 2 = -3 < 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -4, -1, -1, 0, 1, 2 ] -4
+ 0 + 2 = -2 < 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -4, -1, -1, 0, 1, 2 ] -4
+ 1 + 2 = -1 < 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -4, -1, -1, 0, 1, 2 ] -1
+ (-1) + 2 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -4, -1, -1, 0, 1, 2 ] -1
+ 0 + 1 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -4, -1, -1, 0, 1, 2 ] 0
+ 1 + 2 = 3 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [-5, 0, -1, 2, -1, 6, 1] => [
-5, -1, -1, 0, 1, 2, 6] -
• [ -5, -1, -1, 0, 1, 2, 6] -5
+ (-1) + 6 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] -5
+ 0 + 6 = 1 > 0 ჩߟᴍ খᏐࣈେᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] -5
+ 0 + 2 = -3 < 0 ჩߟᴍ খᏐࣈେᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] -5
+ 1 + 2 = -2 < 0 ჩߟᴍ খᏐࣈେᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] -1+
(-1) + 6 = 4 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] -1+
(-1) + 2 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] -1+
0+ 1 = 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] 0+
1 + 6 = 7 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] 0+
1 + 2 = 3 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
• [ -5, -1, -1, 0, 1, 2, 6] 1
+ 2 + 6 = 9 > 0 ჩߟᴍ খᏐࣈ େᏐࣈ -
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 } } <= ഉং -
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 } } <= ჩߟᴍ <= ഉং -
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 } } <= ჩߟᴍ <= ഉং <= খᏐࣈ -
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 } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ -
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 } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ -
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 } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ <= ҠಈখᏐࣈ -
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 } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ <= ҠಈখᏐࣈ <= ҠಈେᏐࣈ -
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 } } <= ჩߟᴍ <= ഉং <= খᏐࣈ <= େᏐࣈ <= ፙ౸Ұ <= ҠಈখᏐࣈ <= ҠಈେᏐࣈ <= Ҡಈჩߟᴍ