Posted onEdited onDisqus: Word count in article: 6.9kReading time ≈6 mins.
Binary Search
1 2 3 4 5 6 7 8 9 10 11 12
classSolution{ funcsearch(_nums: [Int], _target: Int) -> Int { var i =0, j = nums.count-1 while i <= j { let mid = i + (j-i)/2 if nums[mid] == target { return mid } elseif nums[mid] < target { i = mid+1 } else { j = mid-1 } } return-1 } }
Sqrt(x)
1 2 3 4 5 6 7 8 9 10 11 12
classSolution{ funcmySqrt(_x: Int) -> Int { let target =Int(sqrt(Double(x))) var i =0, j = x while i <= j { let mid = i + (j-i)>>1 if mid == target { return mid } mid < target ? (i=mid+1) : (j=mid-1) } return i } }
Guess Number Higher or Lower
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution : GuessGame{ funcguessNumber(_n: Int) -> Int { var i =1, j = n while i <= j { let mid = i + (j-i)>>1 switch guess(mid) { case-1: j = mid-1 case1: i = mid+1 case0: return mid default: break } } return i } }
Search in Rotated Sorted Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution{ funcsearch(_nums: [Int], _target: Int) -> Int { var i =0, j = nums.count-1 while i <= j { let mid = i + (j - i)/2 if nums[mid] == target { return mid } elseif nums[mid] >= nums[i] { (target >= nums[i] && target < nums[mid]) ? (j = mid-1) : (i = mid+1) } else { (target <= nums[j] && target > nums[mid]) ? (i = mid+1) : (j = mid-1) } } return-1 } }
First Bad Version
1 2 3 4 5 6 7 8 9 10
classSolution: VersionControl{ funcfirstBadVersion(_n: Int) -> Int { var i =1, j = n while i <= j { let h = i + (j-i)/2 isBadVersion(h) ? (j = h-1) : (i = h +1) } return i } }
Find Peak Element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution{ funcfindPeakElement(_nums: [Int]) -> Int { var l =0, r = nums.count-1 while l < r { let mid = l+(r-l)/2 nums[mid] > nums[mid+1] ? (r = mid) : (l = mid+1) } return l } funcfindPeakElement(_nums: [Int]) -> Int { funcsearch(_l: Int, _r: Int) -> Int { if (l == r) { return l } let mid = l + (r-l)/2 return (nums[mid] > nums[mid+1]) ? search(l, mid) : search(mid+1, r) } return search(0, nums.count-1) } }
Find Minimum in Rotated Sorted Array
1 2 3 4 5 6 7 8 9 10
classSolution{ funcfindMin(_nums: [Int]) -> Int { var l =0, r = nums.count-1 while l < r { let mid = l+(r-l)/2 nums[mid] > nums[r] ? (l = mid+1) : (r = mid) } return nums[l] } }
Search for a Range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution{ funcsearchRange(_nums: [Int], _target: Int) -> [Int] { if nums.isEmpty { return [-1, -1] } var i =0, j = nums.count-1 while i < j { let mid = i+(j-i)/2 if nums[mid] == target { i = mid; j = mid } elseif nums[mid] < target { i = mid+1 } else { j = mid-1 } } while i-1>=0, nums[i-1] == target { i -=1 } while j+1< nums.count, nums[j+1] == target { j +=1 } return nums[i] == target ? [i, j] : [-1, -1] } }
Find K Closest Elements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution{ funcfindClosestElements(_arr: [Int], _k: Int, _x: Int) -> [Int] { if arr.isEmpty { return [] } var i =0, j = arr.count, ans = [Int] while i < j { let mid = i+(j-i)/2 if arr[mid] == x { i = mid } arr[mid] < x ? (i = mid+1) : (j = mid-1) } j = i+k < arr.count ? i+k : arr.count-1 i = (i-k) >=0? (i-k) : 0 ans =Array(arr[i...j]) while ans.count > k { abs(x-ans.first!) <=abs(x-ans.last!) ? ans.removeLast() : ans.removeFirst() } return ans } }
Closest Binary Search Tree Value
1 2 3 4 5 6 7 8 9 10
classSolution{ funcclosestValue(_root: TreeNode?, _target: Double) -> Int { var p = root, v = p!.val while p !=nil { ifabs(Double(p!.val)-target) <abs(Double(v)-target) { v = p!.val } p = target <Double(p!.val) ? p!.left : p!.right } return v } }
Search in a Sorted Array of Unknown Size
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution{ funcsearch(_reader: ArrayReader, _target: Int) -> Int { if reader.get(0) == target { return0 } var i =0, j =1 while reader.get(j) < target { i = j; j <<=1 } while i <= j { let mid = i+(j-i)>>1, v = reader.get(mid) if v == target { return mid } v < target ? (i=mid+1) : (j=mid-1) } return-1 } }
Pow(x, n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution{ funcmyPow(_x: Double, _n: Int) -> Double { var dict = [0: 1.0, 1: x] funcfastPow(_x: Double, _n: Int) -> Double { iflet v = dict[n] { return v }
let h = fastPow(x, n/2), v = n&1==0? h*h : x*h*h dict[n] = v return v }
let v = fastPow(x, abs(n)) return n <0?1/v : v } }
Valid Perfect Square
1 2 3 4 5 6 7 8 9
classSolution{ funcisPerfectSquare(_num: Int) -> Bool { var x = (num+1)/2 while x*x > num { x = (x + num/x) /2 } return x*x == num } }
Find Smallest Letter Greater Than Target
1 2 3 4 5 6 7 8
classSolution{ funcnextGreatestLetter(_letters: [Character], _target: Character) -> Character { for c in letters { if c > target { return c } } return letters.first! } }
Find Minimum in Rotated Sorted Array
1 2 3 4 5 6 7 8 9 10
classSolution{ funcfindMin(_nums: [Int]) -> Int { var l =0, r = nums.count-1 while l < r { let mid = l+(r-l)/2 nums[mid] >= nums[r] ? (l = mid+1) : (r = mid) } return nums[l] } }
Find Minimum in Rotated Sorted Array II
1 2 3 4 5 6 7 8 9 10 11 12
classSolution{ funcfindMin(_nums: [Int]) -> Int { var l =0, r = nums.count-1 while l < r { let mid = l+(r-l)/2 if nums[mid] > nums[r] { l = mid+1 } elseif nums[mid] < nums[r] { r = mid } else { r -=1 } } return nums[l] } }
classSolution{ funcintersect(_nums1: [Int], _nums2: [Int]) -> [Int] { var dict = [Int: Int]() var ans = [Int]() for n in nums1 { dict[n, default: 0] +=1 } for n in nums2 { iflet v = dict[n], v >0 { ans.append(n) dict[n] = v -1 } } return ans } }
Two Sum II - Input array is sorted
1 2 3 4 5 6 7 8 9 10 11
classSolution{ functwoSum(_numbers: [Int], _target: Int) -> [Int] { var i =0, j = numbers.count-1 while i < j { let v = numbers[i]+numbers[j] if v == target { break } v < target ? (i+=1) : (j-=1) } return [i+1, j+1] } }
Find the Duplicate Number
1 2 3 4 5 6 7 8 9 10
classSolution{ funcfindDuplicate(_nums: [Int]) -> Int { varset=Set<Int>(), i =0 while i < nums.count { ifset.contains(nums[i]) { break } set.insert(nums[i]); i+=1 } return nums[i] } }
Median of Two Sorted Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution{ funcfindMedianSortedArrays(_nums1: [Int], _nums2: [Int]) -> Double { var nums = nums1+nums2 var i =0, j =0, k =0 while i < nums1.count, j < nums2.count { nums1[i] < nums2[j] ? (nums[k] = nums1[i], i+=1) : (nums[k] = nums2[j], j+=1); k+=1 } while i < nums1.count { nums[k] = nums1[i]; i+=1; k+=1 } while j < nums2.count { nums[k] = nums2[j]; j+=1; k+=1 }
let h = nums.count/2 return nums.count&1==1?Double(nums[h]) : Double(nums[h-1]+nums[h])/2.0 } }
Find K-th Smallest Pair Distance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution{ funcsmallestDistancePair(_nums: [Int], _k: Int) -> Int { let nums = nums.sorted() var l =0, h = nums.last!-nums.first! while l < h { let m = (l+h)/2 var count =0, l1 =0 for r1 in0..<nums.count { while (nums[r1]-nums[l1]) > m { l1 +=1 } count += r1-l1 } count >= k ? (h = m): (l = m+1) } return l } }