LeetCode Arrays 101

Max Consecutive Ones

1
2
3
4
5
class Solution {
func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
return nums.split(separator: 0).map {$0.count}.max() ?? 0
}
}

Find Numbers with Even Number of Digits

1
2
3
4
5
class Solution {
func findNumbers(_ nums: [Int]) -> Int {
return nums.filter({String($0).count & 1 == 0 }).count
}
}

Squares of a Sorted Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
func sortedSquares(_ 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
class Solution {
func duplicateZeros(_ 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
}
}

Merge Sorted Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
func merge(_ 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
class Solution {
func removeElement(_ 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
class Solution {
func removeDuplicates(_ nums: inout [Int]) -> Int {
var i = 0
for j in 0..<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
class Solution {
func checkIfExist(_ arr: [Int]) -> Bool {
var dict = Dictionary<Int, Int>()
for i in arr {
dict[i] = (dict[i] ?? 0) + 1
}
for i in arr {
if let v = dict[i*2], v >= ((i==0) ? 2 : 1) {
return true
}
}
return false
}
}

Valid Mountain Array

1
2
3
4
5
6
7
8
9
class Solution {
func validMountainArray(_ 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 { return false }
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
class Solution {
func replaceElements(_ 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
class Solution {
func removeDuplicates(_ nums: inout [Int]) -> Int {
var i = 0
for j in 0..<nums.count {
if nums[i] != nums[j] {
i += 1
nums[i] = nums[j]
}
}
return nums.isEmpty ? 0 : i+1
}
}

Move Zeroes

1
2
3
4
5
class Solution {
func moveZeroes(_ nums: inout [Int]) {
nums = Array(nums.split(separator: 0).joined()) + Array(nums.filter({$0 == 0}))
}
}

Sort Array By Parity

1
2
3
4
5
6
7
8
9
class Solution {
func sortArrayByParity(_ A: [Int]) -> [Int] {
var even = [Int], odd = [Int]
for i in A {
i & 1 == 0 ? (even.append(i)) : (odd.append(i))
}
return even + odd
}
}

Squares of a Sorted Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
func sortedSquares(_ 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
}
}

Height Checker

1
2
3
4
5
class Solution {
func heightChecker(_ heights: [Int]) -> Int {
return zip(heights, heights.sorted()).reduce(0) { return $0 + ($1.0 == $1.1 ? 0 : 1) }
}
}

Max Consecutive Ones II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
func findMaxConsecutiveOnes(_ 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)
}
return min(maxZ, nums.count)
}
}

Third Maximum Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
func thirdMax(_ 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
class Solution {
func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
var nums = nums, ans = [Int]
for i in 0 ..< nums.count {
let index = abs(nums[i])-1
if nums[index] > 0 { nums[index] *= -1 }
}
for i in 0..<nums.count {
if nums[i] > 0 { ans.append(i+1) }
}
return ans
}
}

Translated by gpt-3.5-turbo