Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions Array/RemoveDuplicatesFromSortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
*/

class RemoveDuplicatesFromSortedArray {
func removeDuplicates(inout nums: [Int]) -> Int {
guard nums.count > 0 else {
return 0
}

var index = 0
func removeDuplicates(_ nums: inout [Int]) -> Int {
if nums.isEmpty { return 0 }

for num in nums where num != nums[index] {
index += 1
nums[index] = num
var i = 0
for j in 1..<nums.count {
if nums[i] != nums[j] {
nums[i + 1] = nums[j]
i += 1
}
}

return index + 1
return i + 1
}
}
}
6 changes: 3 additions & 3 deletions Array/RemoveElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

class RemoveElement {
func removeElement(inout nums: [Int], _ val: Int) -> Int {
nums = nums.filter { (num) in num != val }
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
nums = nums.filter { $0 != val }
return nums.count
}
}
}
8 changes: 4 additions & 4 deletions Stack/ValidParentheses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ class ValidParentheses {
if char == "(" || char == "[" || char == "{" {
stack.append(char)
} else if char == ")" {
guard stack.count != 0 && stack.removeLast() == "(" else {
guard stack.popLast() == "(" else {
return false
}
} else if char == "]" {
guard stack.count != 0 && stack.removeLast() == "[" else {
guard stack.popLast() == "[" else {
return false
}
} else if char == "}" {
guard stack.count != 0 && stack.removeLast() == "{" else {
guard stack.popLast() == "{" else {
return false
}
}
}

return stack.isEmpty
}
}
}