classSolution{ funcsortedSquares(_A: [Int]) -> [Int] { var i =0, j =0, k =0, ans =A while j <A.count, A[j] <0 { j +=1 } i = j -1
while i >=0, j <A.count { let (ai, aj) = (A[i]*A[i], A[j]*A[j]) ai < aj ? (ans[k] = ai, i -=1) : (ans[k] = aj, j +=1) k +=1 } if i >=0 { ans[k...] =A[...i].reversed().map {$0*$0}.suffix(from: 0) } if j <A.count { ans[k...] =A[k...].map { $0*$0 }.suffix(from: 0) } return ans } }
Duplicate Zeros
1 2 3 4 5 6 7 8 9 10 11
classSolution{ funcduplicateZeros(_arr: inout [Int]) { var na =Array(repeating: 0, count: arr.count), i =0, j =0 while j < na.count { na[j] = arr[i] if arr[i] ==0, j < na.count-1 { j +=1; na[j] =0 } i +=1; j +=1 } arr = na } }
classSolution{ funcmerge(_nums1: inout [Int], _m: Int, _nums2: [Int], _n: Int) { let nums0 = nums1[..<m] var i =0, j =0, k =0 while i < nums0.count, j < nums2.count { if nums0[i] < nums2[j] { nums1[k] = nums0[i] i +=1 } else { nums1[k] = nums2[j] j +=1 } k +=1 } while i < nums0.count { nums1[k] = nums0[i] i +=1 k +=1 } while j < nums2.count { nums1[k] = nums2[j] j +=1 k +=1 } } }
Remove Element
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution{ funcremoveElement(_nums: inout [Int], _val: Int) -> Int { var i =0, j = nums.count-1 while i < j { if nums[i] == val { while nums[j] == val { j -=1 } nums.swapAt(i, j) } i +=1; } return i } }
Remove Duplicates from Sorted Array
1 2 3 4 5 6 7 8 9 10 11 12
classSolution{ funcremoveDuplicates(_nums: inout [Int]) -> Int { var i =0 for j in0..<nums.count { if nums[i] != nums[j] { i +=1 nums[i] = nums[j] } } return nums.isEmpty ?0 : i+1 } }
Check If N and Its Double Exist
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution{ funccheckIfExist(_arr: [Int]) -> Bool { var dict =Dictionary<Int, Int>() for i in arr { dict[i] = (dict[i] ??0) +1 } for i in arr { iflet v = dict[i*2], v >= ((i==0) ?2 : 1) { returntrue } } returnfalse } }
Valid Mountain Array
1 2 3 4 5 6 7 8 9
classSolution{ funcvalidMountainArray(_A: [Int]) -> Bool { var i =0 while i <A.count-1, A[i] <A[i+1] { i +=1 } if i ==0|| i ==A.count-1 { returnfalse } while i <A.count-1, A[i] >A[i+1] { i +=1 } return i ==A.count-1 } }
Replace Elements with Greatest Element on Right Side
1 2 3 4 5 6 7 8 9 10 11
classSolution{ funcreplaceElements(_arr: [Int]) -> [Int] { var i = arr.count-1, rMax =-1, newArr = arr while i >=0 { newArr[i] = rMax rMax =max(rMax, arr[i]) i -=1 } return newArr } }
Remove Duplicates from Sorted Array
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution{ funcremoveDuplicates(_nums: inout [Int]) -> Int { var i =0 for j in0..<nums.count { if nums[i] != nums[j] { i +=1 nums[i] = nums[j] } } return nums.isEmpty ?0 : i+1 } }
classSolution{ funcsortArrayByParity(_A: [Int]) -> [Int] { var even = [Int], odd = [Int] for i inA { i &1==0? (even.append(i)) : (odd.append(i)) } return even + odd } }
classSolution{ funcsortedSquares(_A: [Int]) -> [Int] { var i =0, j =0, k =0, ans =A while j <A.count, A[j] <0 { j +=1 } i = j -1
while i >=0, j <A.count { let (ai, aj) = (A[i]*A[i], A[j]*A[j]) ai < aj ? (ans[k] = ai, i -=1) : (ans[k] = aj, j +=1) k +=1 } if i >=0 { ans[k...] =A[...i].reversed().map {$0*$0}.suffix(from: 0) } if j <A.count { ans[k...] =A[k...].map { $0*$0 }.suffix(from: 0) } return ans } }
classSolution{ funcfindMaxConsecutiveOnes(_nums: [Int]) -> Int { var z0 =0, z1 =0, maxZ =0 for i in nums { if i ==1 { z1 +=1 } else { z0 = z1 z1 =0 } maxZ =max(maxZ, z0+z1+1) } returnmin(maxZ, nums.count) } }
Third Maximum Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution{ functhirdMax(_nums: [Int]) -> Int { var maxs = [Int.min, Int.min, Int.min] for n in nums { if n > maxs[2], maxs.firstIndex(of: n) ==nil { maxs[2] = n if maxs[2] > maxs[1] { maxs.swapAt(2, 1) if maxs[1] > maxs[0] { maxs.swapAt(1, 0) } } } } return maxs[2] ==Int.min ? maxs[0] : maxs[2] } }
Find All Numbers Disappeared in an Array
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution{ funcfindDisappearedNumbers(_nums: [Int]) -> [Int] { var nums = nums, ans = [Int] for i in0..< nums.count { let index =abs(nums[i])-1 if nums[index] >0 { nums[index] *=-1 } } for i in0..<nums.count { if nums[i] >0 { ans.append(i+1) } } return ans } }